Skip to content

fix(completions): skip the directive info line when stderr is stdout - #2513

Open
Shub3am wants to merge 1 commit into
spf13:mainfrom
Shub3am:fix/completion-directive-info-on-stdout
Open

Shub3am wants to merge 1 commit into
spf13:mainfrom
Shub3am:fix/completion-directive-info-on-stdout

Conversation

@Shub3am

@Shub3am Shub3am commented Oct 1, 2026

Copy link
Copy Markdown

Fixes #1287

If a program sends stderr to stdout (say SetOut(os.Stdout) plus SetErr(os.Stdout), or the old SetOutput(os.Stdout)), __complete prints the "Completion ended with directive" line after :<directive>. The shell scripts read the last stdout line as the directive, so completion breaks.

I went with what @Luap99 suggested in the thread: skip that line when ErrOrStderr() is os.Stdout. Tests that point both streams at the same buffer still get the line, so none of the existing asserts change.

Repro with a tiny program that does SetErr(os.Stdout) and has a flag completion func:

before

$ go run . __complete --output "" 2>/dev/null
json
table
yaml
:0
Completion ended with directive: ShellCompDirectiveDefault

after

$ go run . __complete --output "" 2>/dev/null
json
table
yaml
:0

Added TestCompletionInfoNotPrintedWhenStderrIsStdout, which swaps os.Stdout for a pipe. It fails without the change and passes with it. go test ./... and golangci-lint run are clean.

The shell scripts parse the last stdout line of __complete as the
directive. When a program sends stderr to stdout, for example with
SetErr(os.Stdout), the "Completion ended with directive" line lands
after :<directive> and breaks completion.

Fixes spf13#1287
@CLAassistant

CLAassistant commented Oct 1, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@Shub3am
Shub3am force-pushed the fix/completion-directive-info-on-stdout branch from 0de86a0 to 3339728 Compare October 1, 2026 06:59
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

Successfully merging this pull request may close these issues.

RegisterFlagCompletionFunc writes what looks like debug info to screen

2 participants