fix: make bash completion work without the bash-completion package#355
Open
gtsiolis wants to merge 1 commit into
Open
fix: make bash completion work without the bash-completion package#355gtsiolis wants to merge 1 commit into
gtsiolis wants to merge 1 commit into
Conversation
Cobra's generated bash completion script calls _get_comp_words_by_ref from the bash-completion package on both of its init paths, so on stock macOS (bash 3.2, no bash-completion) every Tab press failed with "_get_comp_words_by_ref: command not found" (DEVX-950). Prepend a guarded pure-bash fallback to the generated script (defined only when the package is absent, the git-completion.bash approach). The fallback re-joins COMP_WORDS pieces split at COMP_WORDBREAKS separators using COMP_LINE to recover adjacency, matching the package's reassembly semantics, and stays bash 3.2 compatible. Both the fallback and Cobra's script body are generated against the writer resolved at execution time; delegating to Cobra's own RunE would write the body to the writer captured at InitDefaultCompletionCmd time, splitting the two halves across destinations when SetOut is called after NewRootCmd. Also replace the bash completion help text: the default recommended 'source <(lstk completion bash)', which is a silent no-op on bash 3.2 — recommend 'eval "$(lstk completion bash)"' instead.
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.
Fixes DEVX-950 (reported by DevRel).
Problem
On stock macOS (bash 3.2.57, no
bash-completionpackage) every Tab press failed withand produced zero completions. Cobra's generated bash script depends on that function from the bash-completion package on both of its init paths — the "Mac fallback" it embeds assumes Homebrew's legacy bash-completion v1.3 is installed, and a truly stock Mac has no bash-completion at all. (Upstream treats this as a documented dependency, see spf13/cobra#2213; kubectl/gh/docker all answer with "install bash-completion" docs.) Zsh was unaffected because Cobra's zsh script is self-contained.
Fix
_get_comp_words_by_reffallback to the generated script (cmd/completion.go), defined only when the bash-completion package is absent — the approach git ships ingit-completion.bash, which is why git completion works on stock Macs. Unlike git's fallback, it usesCOMP_LINEto recover word adjacency, matching the real package's reassembly semantics for shapes like--config=xvs--config= x. The body is bash 3.2 compatible.SetOutis called afterNewRootCmd).source <(lstk completion bash), which is a silent no-op on bash 3.2 (sourcing a process substitution reads nothing there); recommendeval "$(lstk completion bash)"instead, which works on every bash.When the bash-completion package is installed, the guard skips the fallback and behavior is unchanged. Zsh/fish/powershell output is byte-identical to before.
Testing
--noprofile --norc, minimal env — exactly the stock-macOS condition, reproducible on any CI OS): completion works without the package, whitespace-vs-adjacent wordbreak reassembly parity (4 fixtures), and the fallback yielding to an installed package. All failed before the fix with the exact reported error.--no-descriptionspassthrough./bin/bash3.2.57 and Homebrew bash 5.2 in a clean env:eval "$(lstk completion bash)"+ Tab completeslstk st→start status stop, including--flag=valueandpod:word shapes.