From d60d1c5cdc04cfd6f2ddf5ff222216b6cd8d3ac7 Mon Sep 17 00:00:00 2001 From: Ed Morley <501702+edmorley@users.noreply.github.com> Date: Tue, 10 Sep 2024 15:51:55 +0000 Subject: [PATCH] Fix latest version resolution when using `python-` prefix (#3056) Fixes use of version specifiers like `python-3.12`, which: - have an explicit `python-` prefix - are using a major version alias that has to be resolved to an exact version. Also simplified the conditional for the already working case, since it had two branches that were virtually identical. --- libexec/pyenv-version-name | 10 +++++----- test/version-name.bats | 7 +++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/libexec/pyenv-version-name b/libexec/pyenv-version-name index 52d7f8d7..73093b31 100755 --- a/libexec/pyenv-version-name +++ b/libexec/pyenv-version-name @@ -30,11 +30,11 @@ OLDIFS="$IFS" { IFS=: any_not_installed=0 for version in ${PYENV_VERSION}; do - if version_exists "$version" || [ "$version" = "system" ]; then - versions=("${versions[@]}" "${version}") - elif version_exists "${version#python-}"; then - versions=("${versions[@]}" "${version#python-}") - elif resolved_version="$(pyenv-latest -b "$version")"; then + # Remove the explicit 'python-' prefix from versions like 'python-3.12'. + normalised_version="${version#python-}" + if version_exists "${normalised_version}" || [ "$version" = "system" ]; then + versions=("${versions[@]}" "${normalised_version}") + elif resolved_version="$(pyenv-latest -b "${normalised_version}")"; then versions=("${versions[@]}" "${resolved_version}") else echo "pyenv: version \`$version' is not installed (set by $(pyenv-version-origin))" >&2 diff --git a/test/version-name.bats b/test/version-name.bats index 20e2de3b..e3c9a6a4 100644 --- a/test/version-name.bats +++ b/test/version-name.bats @@ -120,3 +120,10 @@ OUT assert_success assert_output "2.7.11" } + +@test "pyenv-latest fallback with prefix in name" { + create_version "3.12.6" + PYENV_VERSION="python-3.12" run pyenv-version-name + assert_success + assert_output "3.12.6" +}