From c20fc7bd6aabfe7fa43372e1b07381dd03df2552 Mon Sep 17 00:00:00 2001 From: native-api Date: Sun, 22 Jan 2023 18:22:26 +0300 Subject: [PATCH] Fix `pyenv which` to support auto-resolved prefixes (#2601) * Resolve version name that hooks see * Avoid a 2nd iteration over configured versions --- libexec/pyenv-which | 23 ++++++++++++----------- test/which.bats | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/libexec/pyenv-which b/libexec/pyenv-which index 88fc447b..29b44318 100755 --- a/libexec/pyenv-which +++ b/libexec/pyenv-which @@ -47,12 +47,20 @@ OLDIFS="$IFS" IFS=: versions=(${PYENV_VERSION:-$(pyenv-version-name)}) IFS="$OLDIFS" +declare -a nonexistent_versions + for version in "${versions[@]}" "$system"; do if [ "$version" = "system" ]; then PATH="$(remove_from_path "${PYENV_ROOT}/shims")" PYENV_COMMAND_PATH="$(command -v "$PYENV_COMMAND" || true)" else - PYENV_COMMAND_PATH="${PYENV_ROOT}/versions/${version}/bin/${PYENV_COMMAND}" + # $version may be a prefix to be resolved by pyenv-latest + version_path="$(pyenv-prefix "${version}" 2>/dev/null)" || \ + { nonexistent_versions+=("$version"); continue; } + # resolve $version for hooks + version="$(basename "$version_path")" + PYENV_COMMAND_PATH="$version_path/bin/${PYENV_COMMAND}" + unset version_path fi if [ -x "$PYENV_COMMAND_PATH" ]; then break @@ -69,17 +77,10 @@ done if [ -x "$PYENV_COMMAND_PATH" ]; then echo "$PYENV_COMMAND_PATH" else - any_not_installed=0 - for version in "${versions[@]}"; do - if [ "$version" = "system" ]; then - continue - fi - if ! [ -d "${PYENV_ROOT}/versions/${version}" ]; then + if (( ${#nonexistent_versions[@]} )); then + for version in "${nonexistent_versions[@]}"; do echo "pyenv: version \`$version' is not installed (set by $(pyenv-version-origin))" >&2 - any_not_installed=1 - fi - done - if [ "$any_not_installed" = 1 ]; then + done exit 1 fi diff --git a/test/which.bats b/test/which.bats index 4fc5eecb..193b95a4 100644 --- a/test/which.bats +++ b/test/which.bats @@ -136,3 +136,22 @@ SH PYENV_VERSION= run pyenv-which python assert_success "${PYENV_ROOT}/versions/3.4/bin/python" } + +@test "resolves pyenv-latest prefixes" { + create_executable "3.4.2" "python" + + PYENV_VERSION=3.4 run pyenv-which python + assert_success "${PYENV_ROOT}/versions/3.4.2/bin/python" +} + +@test "hooks get resolved version name" { + create_hook which echo.bash <