fix(pyenv): fix pyenv PATH settings with a warning (#9935)
This change fixes the setting of $PATH for pyenv and its shims, while warning
the user about non-interactive shells.
Co-authored-by: Marc Cornellà <hello@mcornella.com>
Diff
1diff --git a/plugins/pyenv/pyenv.plugin.zsh b/plugins/pyenv/pyenv.plugin.zsh
2index 82ba6ff8c59724bfaa6c6a8a46ba7fb8f5e46605..fedde634211198960be1b4b5c4b36555ae832445 100644
3--- a/plugins/pyenv/pyenv.plugin.zsh
4+++ b/plugins/pyenv/pyenv.plugin.zsh
5@@ -3,44 +3,67 @@
6 7 # Load pyenv only if command not already available
8 if command -v pyenv &> /dev/null && [[ "$(uname -r)" != *icrosoft* ]]; then
9- FOUND_PYENV=1
10+ FOUND_PYENV=1
11 else
12- FOUND_PYENV=0
13+ FOUND_PYENV=0
14 fi
1516+# Look for pyenv and try to load it (will only work on interactive shells)
17 if [[ $FOUND_PYENV -ne 1 ]]; then
18- pyenvdirs=("$HOME/.pyenv" "/usr/local/pyenv" "/opt/pyenv" "/usr/local/opt/pyenv")
19- for dir in $pyenvdirs; do
20- if [[ -d $dir/bin ]]; then
21- export PATH="$PATH:$dir/bin"
22- FOUND_PYENV=1
23- break
24- fi
25- done
26-fi
27+ pyenvdirs=("$HOME/.pyenv" "/usr/local/pyenv" "/opt/pyenv" "/usr/local/opt/pyenv")
28+ for dir in $pyenvdirs; do
29+ if [[ -d "$dir/bin" ]]; then
30+ FOUND_PYENV=1
31+ break
32+ fi
33+ done
3435-if [[ $FOUND_PYENV -ne 1 ]]; then
36+ if [[ $FOUND_PYENV -ne 1 ]]; then
37 if (( $+commands[brew] )) && dir=$(brew --prefix pyenv 2>/dev/null); then
38- if [[ -d $dir/bin ]]; then
39- export PATH="$PATH:$dir/bin"
40- FOUND_PYENV=1
41- fi
42+ if [[ -d "$dir/bin" ]]; then
43+ FOUND_PYENV=1
44+ fi
45 fi
46+ fi
47+
48+ # If we found pyenv, load it but show a caveat about non-interactive shells
49+ if [[ $FOUND_PYENV -eq 1 ]]; then
50+ cat <<EOF
51+Found pyenv, but it is badly configured. pyenv might not work for
52+non-interactive shells (for example, when run from a script).
53+${bold_color}
54+To fix this message, add these lines to the '.profile' and '.zprofile' files
55+in your home directory:
56+
57+export PYENV_ROOT="${dir/#$HOME/\$HOME}"
58+export PATH="\$PYENV_ROOT/bin:\$PATH"
59+eval "\$(pyenv init --path)"
60+${reset_color}
61+For more info go to https://github.com/pyenv/pyenv/#installation.
62+EOF
63+
64+ # Configuring in .zshrc only makes pyenv available for interactive shells
65+ export PYENV_ROOT=$dir
66+ export PATH="$PYENV_ROOT/bin:$PATH"
67+ eval "$(pyenv init --path)"
68+ fi
69 fi
7071 if [[ $FOUND_PYENV -eq 1 ]]; then
72- eval "$(pyenv init - --no-rehash zsh)"
73- if (( $+commands[pyenv-virtualenv-init] )); then
74- eval "$(pyenv virtualenv-init - zsh)"
75- fi
76- function pyenv_prompt_info() {
77- echo "$(pyenv version-name)"
78- }
79+ eval "$(pyenv init - --no-rehash zsh)"
80+
81+ if (( ${+commands[pyenv-virtualenv-init]} )); then
82+ eval "$(pyenv virtualenv-init - zsh)"
83+ fi
84+
85+ function pyenv_prompt_info() {
86+ echo "$(pyenv version-name)"
87+ }
88 else
89- # fallback to system python
90- function pyenv_prompt_info() {
91- echo "system: $(python -V 2>&1 | cut -f 2 -d ' ')"
92- }
93+ # Fall back to system python
94+ function pyenv_prompt_info() {
95+ echo "system: $(python -V 2>&1 | cut -f 2 -d ' ')"
96+ }
97 fi
9899 unset FOUND_PYENV pyenvdirs dir