Skip to content

fix(completion): use greedy %% for colon parsing in bash completion template#2336

Open
TimSoethout wants to merge 1 commit into
urfave:mainfrom
TimSoethout:fix/bash-completions-colon
Open

fix(completion): use greedy %% for colon parsing in bash completion template#2336
TimSoethout wants to merge 1 commit into
urfave:mainfrom
TimSoethout:fix/bash-completions-colon

Conversation

@TimSoethout
Copy link
Copy Markdown
Contributor

What type of PR is this?

  • bug fix

What this PR does / why we need it

The bash autocomplete template in autocomplete/bash_autocomplete uses fmt.Sprintf for rendering. The line that extracts the token from token:description format had:

token="${line%%:*}"

Because fmt.Sprintf interprets %% as an escape for a literal %, the rendered output becomes ${line%:*} (single %). In bash, ${line%:*} removes the shortest suffix matching :* — i.e. it strips from the last colon. This is wrong when descriptions contain colons (e.g. export:Export configs such as: compose-config), causing the description text to leak into compgen -W and produce spurious completions.

The fix changes %% to %%%% in the template so that after fmt.Sprintf, the rendered script contains %% (greedy removal from the first colon):

# template:
token="${line%%%%:*}"
# rendered:
token="${line%%:*}"

Which issue(s) this PR fixes

Fixes #2335

Testing

Added TestCompletionBashGreedyColonParsing regression test that renders the bash completion template and verifies the output contains ${line%%:*} (greedy) and not ${line%:*} (non-greedy).

$ go test -run TestCompletionBash -v .
=== RUN   TestCompletionBashNoShebang
--- PASS: TestCompletionBashNoShebang (0.00s)
=== RUN   TestCompletionBashGreedyColonParsing
--- PASS: TestCompletionBashGreedyColonParsing (0.00s)
PASS

Release Notes

Fixed bash completion producing spurious entries when command descriptions contain colons.
@TimSoethout TimSoethout requested a review from a team as a code owner May 21, 2026 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant