Fix bash completion failing on stock macOS without bash-completion#355
Fix bash completion failing on stock macOS without bash-completion#355gtsiolis wants to merge 2 commits into
Conversation
d623367 to
8293946
Compare
8293946 to
40b499d
Compare
|
Commenting for reach as this would be nice to merge before Thursday's release. |
43dd2b6 to
87ca9d7
Compare
|
Currently reviewing and testing 👍🏻 Will merge soon if all is good. |
|
@gtsiolis as far as I understand, this PR isn't enough to enable the autocompletion for |
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.
87ca9d7 to
f71375b
Compare
|
You're right, @skyrpex! This is only fixing the bug mentioned in the linked discussion in the issue. More specifically, on stock macOS bash the generated script was crashing. This makes the script work when loaded, it doesn't turn completion on by default. Small nuance: The enablement piece you're describing is already tracked in PRO-245, which I opened some months ago to add a prompt for completions during install. That relies on DEVX-877 (curl install script) as the natural place to wire it in. I'd keep those separate from this fix. What do you think? |
|
Thanks, it makes perfect sense with that piece of context! |
|
Pushed |
Motivation
On stock macOS (bash 3.2.57, no
bash-completionpackage) the generated bash completion script failed on every Tab with_get_comp_words_by_ref: command not foundand produced zero completions, even when loaded correctly. Cobra's script depends on that function from the bash-completion package on both of its init paths, and a stock Mac has neither. Zsh was unaffected because Cobra's zsh script is self-contained.The
completion bash --helptext also recommendedsource <(lstk completion bash), which is a silent no-op on bash 3.2.Changes
_get_comp_words_by_reffallback to the generated script (cmd/completion.go), defined only when the bash-completion package is absent. This is the pattern git ships ingit-completion.bash. It usesCOMP_LINEto recover word adjacency so--flag=valueandpod:shapes complete correctly, and stays bash 3.2 compatible.eval "$(lstk completion bash)", which works on every bash, and document per-shell setup.SetOutafterNewRootCmddoes not split them across writers.When the bash-completion package is installed, the guard skips the fallback and behavior is unchanged. Zsh, fish, and PowerShell output is byte-identical to before.
Scope
This makes the generated script work when loaded. It does not enable completion by default, which still depends on the install method and shell. Auto-enabling completion during install is tracked separately.
Tests
--noprofile --norc, no bash-completion): completion works without the package, wordbreak reassembly parity across 4 fixtures, and the fallback yielding to an installed package. All failed before the fix with the reported error.--no-descriptionspassthrough./bin/bash3.2.57 and Homebrew bash 5.2 in a clean environment.Closes DEVX-950