fix: keep completion subcommand order deterministic in help output#2374
Open
suzuki-shunsuke wants to merge 2 commits into
Open
fix: keep completion subcommand order deterministic in help output#2374suzuki-shunsuke wants to merge 2 commits into
suzuki-shunsuke wants to merge 2 commits into
Conversation
The completion command exposes each shell (bash, zsh, fish, pwsh) as a subcommand. These were generated by ranging over the shellCompletions map, whose iteration order Go randomizes per process. As a result the order of subcommands in `completion --help` changed between runs, which broke downstream projects that generate and commit documentation from --help output. Iterate over an explicitly ordered slice of shell names so the listing is stable (bash, zsh, fish, pwsh) on every run. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
abitrolly
reviewed
Jun 26, 2026
| // subcommands appear in help output. Iterating shellCompletions directly | ||
| // would use Go's randomized map order, making the listing nondeterministic. | ||
| // Keep this in sync with shellCompletions. | ||
| completionShells = []string{"bash", "zsh", "fish", "pwsh"} |
Contributor
There was a problem hiding this comment.
Is it possible to get sorted list instead of maintaining hardcoded indexes?
Contributor
Author
There was a problem hiding this comment.
In case of the dictionary order, the order is changed.
- bash
- fish
- pwsh
- zsh
Is that okay?
I think the current order is more natural.
- bash
- zsh
- fish
- pwsh
https://cli.urfave.org/v3/examples/completions/shell-completions/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What type of PR is this?
What this PR does / why we need it:
The
completioncommand exposes each shell as its own subcommand (bash,zsh,fish,pwsh), introduced in #2279 (released in v3.9.1). The subcommands were generated by ranging over theshellCompletionsmap inbuildCompletionCommand:Go randomizes map iteration order per process, so the order of these subcommands — and therefore the
COMMANDS:section ofcompletion --help— changed between runs. This breaks downstream projects that generate documentation from--helpoutput and commit it via CI: the generated docs flip-flop depending on which run produced them, creating spurious diffs and noise commits. Real-world impact: suzuki-shunsuke/ghtkn#454Changes:
completion.go: addcompletionShells = []string{"bash", "zsh", "fish", "pwsh"}, an explicitly ordered list of the supported shells, and iterate it (looking the renderer up inshellCompletions) instead of ranging over the map. The map stays as the renderer registry; the slice fixes the display order. The order is now deterministic on every run.completion_test.go: addTestCompletionSubcommandOrder, which asserts the subcommand order is alwaysbash, zsh, fish, pwshand that every shell inshellCompletionsis also present incompletionShells(so the two cannot silently drift).TestCompletionShellRenderErrorandTestCompletionShellWriteErrorinject a custom shell intoshellCompletions; they now also register it incompletionShells(with restore) so the injected shell still becomes a subcommand.Which issue(s) this PR fixes:
Fixes #2373
Special notes for your reviewer:
The display order is fixed to
bash, zsh, fish, pwsh. If you would prefer alphabetical order instead, that is a one-line change tocompletionShells.Testing
go test ./...passes.go testinvocations (separate processes), where the subcommand order previously varied.Release Notes