Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bash autocompletion broken when using SetOutput() #2214

Closed
thediveo opened this issue Jan 4, 2025 · 3 comments
Closed

bash autocompletion broken when using SetOutput() #2214

thediveo opened this issue Jan 4, 2025 · 3 comments

Comments

@thediveo
Copy link

thediveo commented Jan 4, 2025

  • go.mod: github.com/spf13/cobra v1.8.1
  • Ubuntu 24.10
  • bash "version 5.2.32(1)-release (x86_64-pc-linux-gnu)"

Noticed the problem both in my failing unit tests as well as on the CLI itself. For testing and as an example I use my https://github.com/thediveo/enumflag/tree/master/test/enumflag-testing. Building enumflag-test and then in the CLI first entering the binary name and then TAB-TAB yields:

/tmp/test $ ./enumflag-testing 
:4
Completion ended with directive
completion                       (Generate the autocompletion script for the specified shell)
help                             (Help about any command)
test

I see this same problem also in an Ubuntu 24.04.1LTS-based devcontainer with bash "version 5.2.21(1)-release (x86_64-pc-linux-gnu)".

Maybe #2213 might be related?

@marckhouzam
Copy link
Collaborator

This is because you use SetOutput() which is deprecated. This function redirects both the output and error streams, and the completion logic prints stuff to the error stream, which is why you are seeing it on your screen.

cobra/command.go

Lines 284 to 285 in 0745e55

// Deprecated: Use SetOut and/or SetErr instead
func (c *Command) SetOutput(output io.Writer) {

You should not redirect the error stream to stdout for the __complete command.

@marckhouzam marckhouzam changed the title bash autocompletion broken bash autocompletion broken when using SetOutput() Jan 4, 2025
@thediveo
Copy link
Author

thediveo commented Jan 4, 2025

many thanks for the quick help!

@marckhouzam
Copy link
Collaborator

You weren’t the first to hit this problem 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants