Skip to content

Conversation

stephane-chazelas
Copy link

@stephane-chazelas stephane-chazelas commented Aug 24, 2025

Fixes #13555

Like in the Korn shell, "${COMP_WORDS[*]}" in bash joins the elements of the array with the first character of $IFS, and $(...) and $1 unquoted cause their expansion to be split on characters of $IFS (and be subjected to globbing).

Here, we want the COMP_WORDS elements to be joined with space characters and $(...) to be split on space (and possibly tab and newline in the future) like with the default value of $IFS (or when $IFS is unset).

So we set IFS locally to space+tab+newline (better than local IFS alone which would only make IFS unset in the function if the localvar_inherit option was not enabled (bash has no equivalent of zsh's emulate -L zsh to have default option settings locally in a function)).

Globbing is still performed on the resulting words (unless the user enabled the noglob option), which is still a problem for instance when we offer file or directory completions when files/directories paths contain wildcard characters, but then again at the moment any character special in the syntax of the shell (including wildcards, space, ;|()<> etc) are a problem anyway as we don't quote them like bash's default file completion does, so it's probably not worth fixing until that other issue is addressed. At the moment pip's file completion only works properly on very tame file names.

The ash-style local -; set -o noglob which would be the obvious fix for that wouldn't work with the ancient version of bash found on macos.

Like in the Korn shell, "${COMP_WORDS[*]}" in bash joins the elements of
the array with the first character of $IFS, and $(...) and $1 unquoted
cause their expansion to be split on characters of $IFS (and be
subjected to globbing).

Here, we want the COMP_WORDS elements to be joined with space characters
and $(...) to be split on space (and possibly tab and newline in the
future) like with the default value of $IFS (or when $IFS is unset).

So we set IFS locally to space+tab+newline (better than "local IFS"
alone which would only make IFS unset in the function if the
localvar_inherit option was not enabled (bash has no equivalent of
zsh's "emulate -L zsh" to have default option settings locally in a
function)).

Globbing is still performed on the resulting words (unless the user
enabled the noglob option), which is still a problem for instance when
we offer file or directory completions when files/directories paths
contain wildcard characters, but then again at the moment any character
special in the syntax of the shell (including wildcards, space, ;|()<>
etc) are a problem anyway as we don't quote them like bash's default
file completion does, so it's probably not worth fixing until that other
issue is addressed. At the moment pip's file completion only works
properly on very tame file names.

The ash-style "local -; set -o noglob" which would be the obvious fix
for that wouldn't work with the ancient version of bash found on macos.
@notatallshaw
Copy link
Member

@stephane-chazelas thanks for submitting for this PR to pip. Please be aware that all pip maintainers are volunteers so it make take a while for someone to review this PR.

@stephane-chazelas
Copy link
Author

For the record, this PR is to address #13555 (it's in the branch name and subject of the PR but it doesn't look like github has done the linking)

@notatallshaw
Copy link
Member

For the record, this PR is to address #13555 (it's in the branch name and subject of the PR but it doesn't look like github has done the linking)

I've edited your post to add:

Fixes #13555

That's how I know to link PRs to issues in a way GitHub understands

@ichard26
Copy link
Member

ichard26 commented Sep 2, 2025

I'm happy to review this since I seem to be the only person with Linux shell completion experience on the core team, but I have extremely limited availability. I'll try my best to review this within two weeks.

@ichard26 ichard26 self-requested a review September 2, 2025 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pip bash completion when IFS doesn't have its default value
3 participants