summaryrefslogtreecommitdiff
path: root/plugins/pyenv/pyenv.plugin.zsh
diff options
context:
space:
mode:
authorMarc Cornellà <hello@mcornella.com>2021-09-03 12:44:01 +0200
committerMarc Cornellà <hello@mcornella.com>2021-09-03 12:47:31 +0200
commitc08fb77c2fc62ba8df6ad690df0e1d5c9f24c075 (patch)
tree2f7d5cfed947892743bde3985a9a3bb113215e8a /plugins/pyenv/pyenv.plugin.zsh
parent53b54f5faa271ee90ec31fcffb559541c82b9703 (diff)
downloadzsh-c08fb77c2fc62ba8df6ad690df0e1d5c9f24c075.tar.gz
zsh-c08fb77c2fc62ba8df6ad690df0e1d5c9f24c075.tar.bz2
zsh-c08fb77c2fc62ba8df6ad690df0e1d5c9f24c075.zip
fix(pyenv): properly load pyenv shims and warn of broken configuration
Closes #10133 Co-authored-by: Chloé “Matcha” <chloe.desoutter@gmail.com>
Diffstat (limited to 'plugins/pyenv/pyenv.plugin.zsh')
-rw-r--r--plugins/pyenv/pyenv.plugin.zsh48
1 files changed, 33 insertions, 15 deletions
diff --git a/plugins/pyenv/pyenv.plugin.zsh b/plugins/pyenv/pyenv.plugin.zsh
index bcf80a6a2..f55701900 100644
--- a/plugins/pyenv/pyenv.plugin.zsh
+++ b/plugins/pyenv/pyenv.plugin.zsh
@@ -1,3 +1,22 @@
+pyenv_config_warning() {
+ local reason="$1"
+ local pyenv_root="${PYENV_ROOT/#$HOME/\$HOME}"
+ cat >&2 <<EOF
+Found pyenv, but it is badly configured ($reason). pyenv might not
+work correctly for non-interactive shells (for example, when run from a script).
+${(%):-"%B%F{yellow}"}
+To fix this message, add these lines to the '.profile' and '.zprofile' files
+in your home directory:
+${(%):-"%f"}
+export PYENV_ROOT="$pyenv_root"
+export PATH="\$PYENV_ROOT/bin:\$PATH"
+eval "\$(pyenv init --path)"
+${(%):-"%F{yellow}"}
+You'll need to restart your user session for the changes to take effect.${(%):-%b%f}
+For more information go to https://github.com/pyenv/pyenv/#installation.
+EOF
+}
+
# This plugin loads pyenv into the current shell and provides prompt info via
# the 'pyenv_prompt_info' function. Also loads pyenv-virtualenv if available.
@@ -30,35 +49,33 @@ if [[ $FOUND_PYENV -ne 1 ]]; then
# If we found pyenv, load it but show a caveat about non-interactive shells
if [[ $FOUND_PYENV -eq 1 ]]; then
- cat >&2 <<EOF
-Found pyenv, but it is badly configured. pyenv might not work for
-non-interactive shells (for example, when run from a script).
-${bold_color}
-To fix this message, add these lines to the '.profile' and '.zprofile' files
-in your home directory:
-
-export PYENV_ROOT="${dir/#$HOME/\$HOME}"
-export PATH="\$PYENV_ROOT/bin:\$PATH"
-eval "\$(pyenv init --path)"
-
-You'll need to restart your user session for the changes to take effect.${reset_color}
-For more info go to https://github.com/pyenv/pyenv/#installation.
-EOF
-
# Configuring in .zshrc only makes pyenv available for interactive shells
export PYENV_ROOT="$dir"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
+
+ # Show warning due to bad pyenv configuration
+ pyenv_config_warning 'pyenv command not found in $PATH'
fi
fi
if [[ $FOUND_PYENV -eq 1 ]]; then
+ # Setup $PYENV_ROOT if not already set
if [[ -z "$PYENV_ROOT" ]]; then
export PYENV_ROOT="$(pyenv root)"
+ pyenv_config_warning 'missing $PYENV_ROOT'
+ fi
+
+ # Add pyenv shims to $PATH if not already added
+ if [[ -z "${path[(Re)$PYENV_ROOT/shims]}" ]]; then
+ eval "$(pyenv init --path)"
+ pyenv_config_warning 'missing pyenv shims in $PATH'
fi
+ # Load pyenv
eval "$(pyenv init - --no-rehash zsh)"
+ # If pyenv-virtualenv exists, load it
if [[ -d "$PYENV_ROOT/plugins/pyenv-virtualenv" ]]; then
eval "$(pyenv virtualenv-init - zsh)"
fi
@@ -74,3 +91,4 @@ else
fi
unset FOUND_PYENV pyenvdirs dir
+unfunction pyenv_config_warning