c08fb77c2fc62ba8df6ad690df0e1d5c9f24c075

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

fix(pyenv): properly load pyenv shims and warn of broken configuration

Closes #10133

Co-authored-by: Chloé “Matcha” <chloe.desoutter@gmail.com>

Diff

 1diff --git a/plugins/pyenv/pyenv.plugin.zsh b/plugins/pyenv/pyenv.plugin.zsh
 2index bcf80a6a23219dc77e091b2c91e2470e53917c6d..f55701900196e882178adaf87560abfff965f8f4 100644
 3--- a/plugins/pyenv/pyenv.plugin.zsh
 4+++ b/plugins/pyenv/pyenv.plugin.zsh
 5@@ -1,3 +1,22 @@
 6+pyenv_config_warning() {
 7+  local reason="$1"
 8+  local pyenv_root="${PYENV_ROOT/#$HOME/\$HOME}"
 9+  cat >&2 <<EOF
10+Found pyenv, but it is badly configured ($reason). pyenv might not
11+work correctly for non-interactive shells (for example, when run from a script).
12+${(%):-"%B%F{yellow}"}
13+To fix this message, add these lines to the '.profile' and '.zprofile' files
14+in your home directory:
15+${(%):-"%f"}
16+export PYENV_ROOT="$pyenv_root"
17+export PATH="\$PYENV_ROOT/bin:\$PATH"
18+eval "\$(pyenv init --path)"
19+${(%):-"%F{yellow}"}
20+You'll need to restart your user session for the changes to take effect.${(%):-%b%f}
21+For more information go to https://github.com/pyenv/pyenv/#installation.
22+EOF
23+}
24+
25 # This plugin loads pyenv into the current shell and provides prompt info via
26 # the 'pyenv_prompt_info' function. Also loads pyenv-virtualenv if available.
27 
28@@ -30,35 +49,33 @@ if [[ $FOUND_PYENV -ne 1 ]]; then
29 
30   # If we found pyenv, load it but show a caveat about non-interactive shells
31   if [[ $FOUND_PYENV -eq 1 ]]; then
32-    cat >&2 <<EOF
33-Found pyenv, but it is badly configured. pyenv might not work for
34-non-interactive shells (for example, when run from a script).
35-${bold_color}
36-To fix this message, add these lines to the '.profile' and '.zprofile' files
37-in your home directory:
38-
39-export PYENV_ROOT="${dir/#$HOME/\$HOME}"
40-export PATH="\$PYENV_ROOT/bin:\$PATH"
41-eval "\$(pyenv init --path)"
42-
43-You'll need to restart your user session for the changes to take effect.${reset_color}
44-For more info go to https://github.com/pyenv/pyenv/#installation.
45-EOF
46-
47     # Configuring in .zshrc only makes pyenv available for interactive shells
48     export PYENV_ROOT="$dir"
49     export PATH="$PYENV_ROOT/bin:$PATH"
50     eval "$(pyenv init --path)"
51+
52+    # Show warning due to bad pyenv configuration
53+    pyenv_config_warning 'pyenv command not found in $PATH'
54   fi
55 fi
56 
57 if [[ $FOUND_PYENV -eq 1 ]]; then
58+  # Setup $PYENV_ROOT if not already set
59   if [[ -z "$PYENV_ROOT" ]]; then
60     export PYENV_ROOT="$(pyenv root)"
61+    pyenv_config_warning 'missing $PYENV_ROOT'
62+  fi
63+
64+  # Add pyenv shims to $PATH if not already added
65+  if [[ -z "${path[(Re)$PYENV_ROOT/shims]}" ]]; then
66+    eval "$(pyenv init --path)"
67+    pyenv_config_warning 'missing pyenv shims in $PATH'
68   fi
69 
70+  # Load pyenv
71   eval "$(pyenv init - --no-rehash zsh)"
72 
73+  # If pyenv-virtualenv exists, load it
74   if [[ -d "$PYENV_ROOT/plugins/pyenv-virtualenv" ]]; then
75     eval "$(pyenv virtualenv-init - zsh)"
76   fi
77@@ -74,3 +91,4 @@ else
78 fi
79 
80 unset FOUND_PYENV pyenvdirs dir
81+unfunction pyenv_config_warning