From f22f8cc1a463c207f8ca404d0101f482243efbb9 Mon Sep 17 00:00:00 2001 From: Hridhin Varkey Joseph <318277129+hridhinvarkeyjoseph@users.noreply.github.com> Date: Wed, 2 Dec 2026 20:01:50 +0530 Subject: [PATCH] [Perf] `nvm_version`: resolve an exact version that is not installed without `nvm_ls` When `default` points to an exact version that is not installed, every shell start listed all installed versions twice just to print `N/A`. Co-authored-by: Hridhin Varkey Joseph <318277129+hridhinvarkeyjoseph@users.noreply.github.com> Co-authored-by: Jordan Harband --- nvm.sh | 33 +++++++++++-- .../Unit tests/nvm_is_missing_exact_version | 49 +++++++++++++++++++ test/fast/Unit tests/nvm_version | 10 ++-- ...for an exact version that is not installed | 49 +++++++++++++++++++ 4 files changed, 131 insertions(+), 10 deletions(-) create mode 100755 test/fast/Unit tests/nvm_is_missing_exact_version create mode 100755 test/fast/Unit tests/nvm_version skips nvm_ls for an exact version that is not installed diff --git a/nvm.sh b/nvm.sh index 233be703..32349bc4 100755 --- a/nvm.sh +++ b/nvm.sh @@ -514,6 +514,25 @@ nvm_is_installed_exact_version() { nvm_is_version_installed "${1}" && nvm_is_version_selected "${1}" } +# An exact release `vX.Y.Z` that is not installed, as node or io.js, can only +# resolve to `N/A`; checking for it directly avoids listing every installed version. +nvm_is_missing_exact_version() { + case "${1-}" in + v*) ;; + *) return 1 ;; + esac + case "${1#v}" in + *[!0-9.]* | .* | *. | *..* | *.*.*.*) return 1 ;; + *.*.*) ;; + *) return 1 ;; + esac + # an alias with this exact name takes precedence, as it does in `nvm_version` + if [ -e "$(nvm_alias_path)/${1}" ]; then + return 1 + fi + ! nvm_is_version_installed "${1}" && ! nvm_is_version_installed "$(nvm_iojs_prefix)-${1}" +} + nvm_is_version_installed() { if [ -z "${1-}" ]; then return 1 @@ -1491,14 +1510,18 @@ nvm_version() { NVM_ONLY_SELECTED="${NVM_ONLY_ALIAS}" fi - # Fast path: an exact, fully-qualified version that is installed resolves to - # itself, unless an alias of the same name takes precedence. Short-circuiting - # it here avoids the `nvm_ls` subprocess pipeline on shell startup; every - # other pattern falls through to `nvm_ls` unchanged. + # Fast path: an exact, fully-qualified version resolves to itself if it is + # installed, and to `N/A` if it is not, unless an alias of the same name takes + # precedence. Short-circuiting it here avoids the `nvm_ls` subprocess pipeline + # on shell startup; every other pattern falls through to `nvm_ls` unchanged. if [ -z "${NVM_LTS_FILTER}" ] && nvm_is_installed_exact_version "${NVM_PATTERN}"; then nvm_echo "${NVM_PATTERN}" return 0 fi + if nvm_is_missing_exact_version "${NVM_PATTERN}"; then + nvm_echo 'N/A' + return 3 + fi # Fast path: once the newest installed version is 1.0 or later, `stable` is # that version, so one listing is enough; `nvm_ls stable` lists the installed @@ -7484,7 +7507,7 @@ nvm() { nvm_echo nvm_err nvm_grep nvm_cd nvm_cd_root nvm_npm \ nvm_die_on_prefix nvm_get_make_jobs nvm_get_minor_version \ nvm_has_solaris_binary nvm_is_merged_node_version \ - nvm_is_natural_num nvm_is_installed_exact_version nvm_is_version_installed nvm_validate_install \ + nvm_is_natural_num nvm_is_installed_exact_version nvm_is_missing_exact_version nvm_is_version_installed nvm_validate_install \ nvm_install_lock_name nvm_acquire_install_lock nvm_release_install_lock \ nvm_list_aliases nvm_make_alias nvm_print_alias_file nvm_print_alias_path \ nvm_print_default_alias nvm_print_formatted_alias nvm_resolve_local_alias \ diff --git a/test/fast/Unit tests/nvm_is_missing_exact_version b/test/fast/Unit tests/nvm_is_missing_exact_version new file mode 100755 index 00000000..06e0d7f1 --- /dev/null +++ b/test/fast/Unit tests/nvm_is_missing_exact_version @@ -0,0 +1,49 @@ +#!/bin/sh + +cleanup () { + rm -rf "$NVM_DIR" + unset -f die cleanup + unset NVM_DIR NODE_PATH +} +die () { echo "$@" ; cleanup ; exit 1; } + +: nvm.sh +\. ../../../nvm.sh + +set -ex + +NVM_DIR=$(mktemp -d) +NODE_PATH="$NVM_DIR/versions/node" +mkdir -p "$NODE_PATH" "$NVM_DIR/alias" + +# nvm_is_missing_exact_version is available +type nvm_is_missing_exact_version > /dev/null 2>&1 || die 'nvm_is_missing_exact_version is not available' + +# no parameter is not a missing exact version +! nvm_is_missing_exact_version || die 'no parameter should fail' + +# an exact version that is not installed is missing +nvm_is_missing_exact_version 'v26.14.0' || die 'v26.14.0 is not installed and should succeed' + +# an installed exact version is not missing +mkdir -p "$NODE_PATH/v26.14.0/bin" +touch "$NODE_PATH/v26.14.0/bin/node" +chmod +x "$NODE_PATH/v26.14.0/bin/node" +! nvm_is_missing_exact_version 'v26.14.0' || die 'v26.14.0 is installed and should fail' + +# an installed io.js version with the same number is not missing +mkdir -p "$NVM_DIR/versions/io.js/v3.3.1/bin" +touch "$NVM_DIR/versions/io.js/v3.3.1/bin/node" +chmod +x "$NVM_DIR/versions/io.js/v3.3.1/bin/node" +! nvm_is_missing_exact_version 'v3.3.1' || die 'iojs-v3.3.1 is installed, so v3.3.1 should fail' + +# an alias with the exact name takes precedence +echo 'v26.14.0' > "$NVM_DIR/alias/v27.0.0" +! nvm_is_missing_exact_version 'v27.0.0' || die 'an alias named v27.0.0 should fail' + +# anything that is not a plain vX.Y.Z is left to the full lookup +for VERSION in 26.14.0 v26 v26.14 v26.14.0.1 v26..0 v26.14. v26.14.0-rc.1 'lts/*' node iojs-v3.3.2 'v1.2.3 '; do + ! nvm_is_missing_exact_version "$VERSION" || die "$VERSION should fail" +done + +cleanup diff --git a/test/fast/Unit tests/nvm_version b/test/fast/Unit tests/nvm_version index ec49f216..d0039e83 100755 --- a/test/fast/Unit tests/nvm_version +++ b/test/fast/Unit tests/nvm_version @@ -60,13 +60,13 @@ EXPECTED_OUTPUT="v1.2.3" try nvm_version v1.2.3-rc.1 [ "_$CAPTURED_STDOUT" = "_v1.2.3-rc.1" ] || die '"nvm_version v1.2.3-rc.1" did not return the version itself; got '"$CAPTURED_STDOUT" -# an exact version that is not installed still falls back to "nvm_ls" +# an exact version that is not installed is N/A without forking "nvm_ls" nvm_is_version_installed() { return 1; } -nvm_ls() { echo "pattern: $1"; } +nvm_ls() { die '"nvm_version" must not call "nvm_ls" for an exact version that is not installed'; } try nvm_version v1.2.3 -EXPECTED_OUTPUT="pattern: v1.2.3" -[ "_$CAPTURED_STDOUT" = "_$EXPECTED_OUTPUT" ] || die '"nvm_version v1.2.3" did not fall back to "nvm_ls"; got '"$CAPTURED_STDOUT" -[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die '"nvm_version v1.2.3" did not exit 0; got '"$CAPTURED_EXIT_CODE" +EXPECTED_OUTPUT="N/A" +[ "_$CAPTURED_STDOUT" = "_$EXPECTED_OUTPUT" ] || die '"nvm_version v1.2.3" did not return N/A; got '"$CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die '"nvm_version v1.2.3" did not exit 3; got '"$CAPTURED_EXIT_CODE" # only exact, fully-qualified versions take the fast path nvm_is_version_installed() { die '"nvm_version" must not call "nvm_is_version_installed" for non-exact versions'; } diff --git a/test/fast/Unit tests/nvm_version skips nvm_ls for an exact version that is not installed b/test/fast/Unit tests/nvm_version skips nvm_ls for an exact version that is not installed new file mode 100755 index 00000000..267c1e83 --- /dev/null +++ b/test/fast/Unit tests/nvm_version skips nvm_ls for an exact version that is not installed @@ -0,0 +1,49 @@ +#!/bin/sh + +die () { echo "$@" ; cleanup ; exit 1; } +cleanup () { + unset -f nvm_ls + rm -rf "${NVM_DIR}" + NVM_DIR="${ORIG_NVM_DIR}" +} + +: nvm.sh +\. ../../../nvm.sh + +\. ../../common.sh + +ORIG_NVM_DIR="${NVM_DIR}" +NVM_DIR="$(mktemp -d)" +mkdir -p "${NVM_DIR}/alias" "${NVM_DIR}/versions/io.js/v3.3.1/bin" +printf '#!/bin/sh\n' > "${NVM_DIR}/versions/io.js/v3.3.1/bin/node" +chmod +x "${NVM_DIR}/versions/io.js/v3.3.1/bin/node" + +nvm_ls() { echo "nvm_ls: $1"; } + +try nvm_version v9.9.9 +[ "_${CAPTURED_STDOUT}" = '_N/A' ] || die "missing exact version: expected >N/A<; got >${CAPTURED_STDOUT}<" +[ "_${CAPTURED_EXIT_CODE}" = '_3' ] || die "missing exact version: expected exit code 3; got ${CAPTURED_EXIT_CODE}" + +try nvm_version '--lts v9.9.9' +[ "_${CAPTURED_STDOUT}" = '_N/A' ] || die "missing exact version after --lts: expected >N/A<; got >${CAPTURED_STDOUT}<" +[ "_${CAPTURED_EXIT_CODE}" = '_3' ] || die "missing exact version after --lts: expected exit code 3; got ${CAPTURED_EXIT_CODE}" + +echo v9.9.9 > "${NVM_DIR}/alias/default" +try nvm_resolve_local_alias default +[ "_${CAPTURED_STDOUT}" = '_N/A' ] || die "default alias to a missing exact version: expected >N/A<; got >${CAPTURED_STDOUT}<" +[ "_${CAPTURED_EXIT_CODE}" = '_3' ] || die "default alias to a missing exact version: expected exit code 3; got ${CAPTURED_EXIT_CODE}" + +try nvm_explain_excluded_version v9.9.9 +[ "_${CAPTURED_STDOUT}" = '_' ] || die "explaining a missing exact version: expected no output; got >${CAPTURED_STDOUT}<" +[ "_${CAPTURED_EXIT_CODE}" = '_1' ] || die "explaining a missing exact version: expected exit code 1; got ${CAPTURED_EXIT_CODE}" + +# an io.js version with the same number is installed, so the full lookup finds it +try nvm_version v3.3.1 +[ "_${CAPTURED_STDOUT}" = '_nvm_ls: v3.3.1' ] || die "io.js version: expected the full lookup; got >${CAPTURED_STDOUT}<" + +# an alias with the exact name takes precedence +echo v3.3.1 > "${NVM_DIR}/alias/v9.9.9" +try nvm_version v9.9.9 +[ "_${CAPTURED_STDOUT}" = '_nvm_ls: v9.9.9' ] || die "alias named like a version: expected the full lookup; got >${CAPTURED_STDOUT}<" + +cleanup