Cobra: Fish completions with `:4` and `:0` as options

Created on 12 Nov 2020  路  6Comments  路  Source: spf13/cobra

We recently enabled completions on goreleaser, and it works pretty well.

One of the issues we found, though, is that on fish it returns :4 as an option, e.g.:

image

If I choose :4 and keep pressing TAB, it will keep completing :0 forever:

image

Same happens for subcommands, e.g.:

image

I think it might be related to the fact that we do some "hacky" stuff to make release be the default command instead of root (is there a better way of doing that?): https://github.com/goreleaser/goreleaser/blob/master/cmd/root.go#L30-L32

Anyway, not sure how much of this is a bug, how much is expected and how much is us doing things wrong.

Most helpful comment

Fix posted in #1284

All 6 comments

This sounds a lot like the problem described starting in this comment: https://github.com/spf13/cobra/pull/899#issuecomment-659283479

I hope it helps.

@caarlos0 did you find the problem?

I have not 馃様

@caarlos0 I had a look. goreleaser always prints an empty line at the very end of its output:
https://github.com/goreleaser/goreleaser/blob/f35534d9d7cbc2d69fcbd4edb78a5d668357fa60/cmd/root.go#L23

I don't believe the completion script is ready for that. The completion script actually calls goreleaser __complete <args> (for example, for goreleaser completion <TAB> it will call goreleaser __complete completion "") and parses the output. But the script expects the last line to have a special meaning (the completion directive). In this case, the extra empty line confuses the script. Not sure why it works for zsh. I'll dig deeper and post a PR to handle this case.

Until there is a fix in Cobra, what you could do is avoid printing that empty line when the command is __complete.

yes, that was it! I would never guess hehe

Thanks @marckhouzam 馃殌

Fix posted in #1284

Was this page helpful?
0 / 5 - 0 ratings