Skip to content

fix(complete): Handle COMP_WORDBREAKS colon splitting in bash#6311

Closed
casey wants to merge 2 commits intoclap-rs:masterfrom
casey:suppress-bash-completion-colon-splitting
Closed

fix(complete): Handle COMP_WORDBREAKS colon splitting in bash#6311
casey wants to merge 2 commits intoclap-rs:masterfrom
casey:suppress-bash-completion-colon-splitting

Conversation

@casey
Copy link
Copy Markdown
Contributor

@casey casey commented Mar 22, 2026

Fixes completion of values containing : in Bash with the unstable-dynamic completion engine.

I ran into this while migrating just to use the new dynamic completion engine. Paths to recipes in submodules contain colons foo::bar, and would break with the new completion engine. We patched the old bash completion script to work around this, but that was a nightmare to maintain, so I'm hoping to upstream this.

I think this should be the default behavior, since splitting on : is not desirable, and no other shell does it.

Bash's COMP_WORDBREAKS includes : by default, which causes COMP_WORDS to split tokens like foo::bar into ["foo", ":", ":", "bar"]. This breaks completion for any value containing colons.

Use _get_comp_words_by_ref -n : to get unsplit words, and __ltrim_colon_completions to trim the colon prefix from COMPREPLY so bash replaces text correctly.

_get_comp_words_by_ref and __ltrim_colon_completions are from bash-completion, a pretty commonly available package.

Both calls are guarded so the script falls back to the current behavior when bash-completion is not installed.

This was manually test on MacOS using bash and bash-completion@2 installed with homebrew, and it was able to complete just foo::<tab> with just foo::bar, which fails without this patch.

Bash's COMP_WORDBREAKS includes `:` by default, which causes COMP_WORDS to split tokens like `foo::bar` into `["foo", ":", ":", "bar"]`. This breaks completion for any value containing colons. Use `_get_comp_words_by_ref -n :` from the bash-completion package to get unsplit words, and `__ltrim_colon_completions` to trim the colon prefix from COMPREPLY so bash replaces text correctly. Both calls are guarded so the script falls back to the current behavior when bash-completion is not installed.
@casey casey force-pushed the suppress-bash-completion-colon-splitting branch from 4988f2e to 426e0c2 Compare March 22, 2026 00:40
@casey
Copy link
Copy Markdown
Contributor Author

casey commented Mar 22, 2026

Sorry, I ran the tests locally, but didn't run into this error. I'm going to convert this PR to a draft while I debug.

@casey casey marked this pull request as draft March 22, 2026 00:53
@epage
Copy link
Copy Markdown
Member

epage commented Mar 23, 2026

Overall, we are wanting to disable COMP_WORDBREAKS. We are discussing this in #6280. I'd recommend posting your proposed solution there. Would also be good to link to any relevant documentation. For example, is _get_comp_words_by_ref an internal function from bash-completions or a provided API?

@casey
Copy link
Copy Markdown
Contributor Author

casey commented Mar 28, 2026

I don't think this is the right approach anymore. The functions this PR depend on are from bash-complete, and ideally completions would work without any third party package.

For now, in just, I wound up pinning clap and patching the bash registration script in casey/just#3205. I don't think that this code is suitable for upstreaming, since it only reassembles words split on :, instead of consulting the contents of COMP_WORDBREAKS.

@casey casey closed this Mar 28, 2026
@casey casey deleted the suppress-bash-completion-colon-splitting branch March 28, 2026 01:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants