bash version detection is locale independent#1942
Conversation
|
Please rebase to 8.0.x and add a changelog entry. |
Done. |
| output = subprocess.run(["bash", "--version"], stdout=subprocess.PIPE) | ||
| match = re.search(r"version (\d)\.(\d)\.\d", output.stdout.decode()) | ||
| output = subprocess.run( | ||
| ["bash", "-c", "echo ${BASH_VERSION}"], stdout=subprocess.PIPE |
There was a problem hiding this comment.
Please note that this does not always return the version of bash where autocompletion has been invoked from.
Here's an example, taken from one macOS system:
$ which -a bash
/usr/local/bin/bash
/bin/bash
$ help | head -1
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
$ bash -c 'echo ${BASH_VERSION}'
4.4.23(1)-release
$ echo $BASH_VERSION
3.2.57(1)-release
Here, /bin/bash is the OS-provided bash 3.2.57, while /usr/local/bin/bash is bash 4.4.23 installed using Homebrew. The user profile is configured with /bin/bash as the login shell.
I don't see a good way to resolve this issue other than to tell the user that such setup is "broken". I'm mentioning it anyway in case someone else has an idea.
(This PR is still an important improvement over the previous bash --version approach, which also suffers from the issue I mentioned. So this comment shouldn't be considered as a blocker for merging the PR.)
There was a problem hiding this comment.
If BASH_VERSION is available in the env, then couldn't we look at that directly?
There was a problem hiding this comment.
Unfortunately BASH_VERSION is not exported by default, so while
$ echo ${BASH_VERSION}
4.4.23(1)-releaseworks,
$ python -c 'import os; print(os.environ.get("BASH_VERSION"))'
Nonedoesn't. This can be worked around by doing export BASH_VERSION before invoking click for autocompletion, but I think this would be a breaking change.
There was a problem hiding this comment.
But the current code has the very same problem, as you initially mentioned @igrr, as it also just calls the first bash in $PATH.
There was a problem hiding this comment.
I agree with that and with your proposed solution @janLo, I only mentioned this (extra) issue in case anyone looking at this PR comes up with a way to deal it.
|
I would change it to that for now: output = subprocess.run(["bash", "--version"], stdout=subprocess.PIPE)
match = re.search(r"(\d+)\.(\d+)\.\d+", output.stdout.decode())This solves the locale dependent spelling of version ans also adds support for multi-digit versions (e.g. 5.12.23). Since this still uses So while this may not be the perfect solution, it will certainly improve the current state. :) |
|
Why not use the version variable. This would completely avoid future surprises that always happen when one tries to parse data out of human readable descriptions one does not control. The variable is at least clearly defined:
(From https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html) |
The `BASH_VERSION` variable holds the bash version number in a much better consumable and especially locale independent format than the `--version` flag.
The
BASH_VERSIONvariable holds the bash version number in a muchbetter consumable and especially locale independent format than the
--versionflag.This fixes #1940
Checklist:
CHANGES.rstsummarizing the change and linking to the issue... versionchanged::entries in any relevant code docs.pre-commithooks and fix any issues.pytestandtox, no tests failed.