From 62adf80079bd88bbb1e81664f44f7179d4e87322 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 20 Nov 2026 10:36:47 -0800 Subject: [PATCH] [Fix] `--lts=`: match codenames containing the name, not as a regex `nvm_ls_remote_index_tab` compared the codename with awk's `!~`, so the name was a regex: `--lts=.` matched every LTS line, and `--lts=i.on` or `--lts=(` didn't mean what they say. Use `index()` so a name still matches every codename that contains it, but literally, and do the same in `nvm_filter_lts_versions`, for `--lts=` in an alias. Locally, `--lts=` resolved the `lts/` alias, which only exists for full codenames, so `nvm use --lts=ir` failed where `nvm install --lts=ir` installed Iron. Add `nvm_resolve_lts_name`, which picks the newest codename in `$NVM_DIR/.lts-status` that contains the name, and resolve `lts/` with it in `nvm use`, `uninstall`, `exec`, `run`, and in `nvm install` for its offline lookup and the default and `--alias=` it writes. Without a recorded status, or when no codename contains the name, the name is used as given. Signed-off-by: Divya Chukkapalli <12871391+divya0795@users.noreply.github.com> Co-authored-by: Divya Chukkapalli <12871391+divya0795@users.noreply.github.com> Co-authored-by: Jordan Harband --- nvm.sh | 64 ++++++++++++++++-- test/fast/Aliases/an alias can store --lts | 8 +++ ...ck the newest codename containing the name | 66 +++++++++++++++++++ .../nvm install --alias, --default and --save | 26 ++++++++ ...emote LTS name is a substring, not a regex | 35 ++++++++++ test/fast/Unit tests/nvm_resolve_lts_name | 57 ++++++++++++++++ 6 files changed, 249 insertions(+), 7 deletions(-) create mode 100755 test/fast/Running 'nvm use', 'nvm exec', 'nvm run' and 'nvm uninstall' with '--lts=name' pick the newest codename containing the name create mode 100755 test/fast/Unit tests/nvm_ls_remote LTS name is a substring, not a regex create mode 100755 test/fast/Unit tests/nvm_resolve_lts_name diff --git a/nvm.sh b/nvm.sh index 87f3a9f7..776cdbd1 100755 --- a/nvm.sh +++ b/nvm.sh @@ -2888,8 +2888,8 @@ nvm_ensure_lts_status() { [ -f "$(nvm_lts_status_path)" ] } -# args: an LTS codename, or `*` for any -# Keeps the version lines on stdin whose recorded LTS status matches. +# args: an LTS name, or `*` for any +# Keeps the version lines on stdin whose recorded LTS codename contains the name. nvm_filter_lts_versions() { if ! nvm_ensure_lts_status; then nvm_err 'The LTS status of installed versions is not recorded yet; run `nvm ls-remote` to record it.' @@ -2903,7 +2903,50 @@ nvm_filter_lts_versions() { status[fields[1]] = fields[2]; } } - ($1 in status) && status[$1] != "-" && (lts == "*" || status[$1] == lts) { print } + ($1 in status) && status[$1] != "-" && (lts == "*" || (lts != "" && index(status[$1], lts))) { print } + ' +} + +# args: an LTS name +# Prints the newest recorded LTS codename that contains the name, like `--lts=` +# matches remotely, or the name itself when none does or LTS status isn't recorded. +nvm_resolve_lts_name() { + local NVM_LTS_NAME + NVM_LTS_NAME="${1-}" + case "${NVM_LTS_NAME}" in + '' | '*' | -[123456789]*) + nvm_echo "${NVM_LTS_NAME}" + return 0 + ;; + esac + if [ ! -f "$(nvm_lts_status_path)" ]; then + nvm_echo "${NVM_LTS_NAME}" + return 0 + fi + NVM_LTS_STATUS_PATH="$(nvm_lts_status_path)" NVM_LTS_NAME="${NVM_LTS_NAME}" command awk ' + function newer(a, b, x, y, i) { + split(substr(a, 2), x, "."); + split(substr(b, 2), y, "."); + for (i = 1; i <= 3; i++) { + if (x[i] + 0 != y[i] + 0) { return x[i] + 0 > y[i] + 0 } + } + return 0 + } + BEGIN { + record = ENVIRON["NVM_LTS_STATUS_PATH"]; + name = ENVIRON["NVM_LTS_NAME"]; + codename = name; + newest = ""; + while ((getline line < record) > 0) { + split(line, fields, " "); + if (fields[2] == "-" || !index(fields[2], name)) { continue } + if (newest == "" || newer(fields[1], newest)) { + newest = fields[1]; + codename = fields[2]; + } + } + print codename + } ' } @@ -3033,7 +3076,7 @@ EOF else { category = "lts" } if (!index(only, "," category ",")) { next } if (lts && $10 ~ /^\-?$/) { next } - if (lts && lts != "*" && tolower($10) !~ tolower(lts)) { next } + if (lts && lts != "*" && !index(tolower($10), tolower(lts))) { next } if ($10 !~ /^\-?$/) { if ($10 && $10 != prev) { print $1, $10, "*" @@ -5577,7 +5620,7 @@ nvm() { if [ "${LTS}" = '*' ]; then OFFLINE_PATTERN="$(nvm_resolve_alias 'lts/*' 2>/dev/null || nvm_echo)" else - OFFLINE_PATTERN="$(nvm_resolve_alias "lts/${LTS}" 2>/dev/null || nvm_echo)" + OFFLINE_PATTERN="$(nvm_resolve_alias "lts/$(nvm_resolve_lts_name "${LTS}")" 2>/dev/null || nvm_echo)" fi if [ -z "${OFFLINE_PATTERN}" ]; then nvm_err "LTS alias '${LTS}' not found locally. Run \`nvm ls-remote --lts\` first to populate LTS aliases." @@ -5759,6 +5802,11 @@ nvm() { FLAVOR="$(nvm_node_prefix)" fi + # the default and `--alias=` point at the LTS alias of the codename the name selected + if [ -n "${LTS-}" ]; then + LTS="$(nvm_resolve_lts_name "${LTS}")" + fi + EXIT_CODE=0 if nvm_is_version_installed "${VERSION}" && nvm_validate_install "${VERSION}"; then @@ -5972,7 +6020,7 @@ nvm() { VERSION="$(nvm_match_version "lts/${PATTERN##lts/}")" ;; --lts=*) - VERSION="$(nvm_match_version "lts/${PATTERN##--lts=}")" + VERSION="$(nvm_match_version "lts/$(nvm_resolve_lts_name "${PATTERN##--lts=}")")" ;; *) VERSION="$(nvm_version "${PATTERN}")" @@ -6180,6 +6228,7 @@ nvm() { NVM_ONLY_SELECTED="${NVM_ONLY_RESOLVED}" if [ -n "${NVM_LTS-}" ]; then + NVM_LTS="$(nvm_resolve_lts_name "${NVM_LTS}")" VERSION="$(nvm_match_version "lts/${NVM_LTS:-*}")" elif [ -z "${PROVIDED_VERSION-}" ]; then { PROVIDED_VERSION="$(NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version 3>&1 1>&4)"; } 4>&1 @@ -6469,6 +6518,7 @@ nvm() { local VERSION_SOURCE VERSION_SOURCE='' if [ "${NVM_LTS-}" != '' ]; then + NVM_LTS="$(nvm_resolve_lts_name "${NVM_LTS}")" provided_version="lts/${NVM_LTS:-*}" VERSION="${provided_version}" VERSION_SOURCE='lts' @@ -7089,7 +7139,7 @@ nvm() { nvm_is_iojs_version nvm_is_alias nvm_has_non_aliased \ nvm_ls_remote nvm_ls_remote_iojs nvm_ls_remote_index_tab nvm_get_remote_aliases \ nvm_ls nvm_remote_version nvm_remote_versions nvm_sort_versions \ - nvm_lts_status_path nvm_record_lts_status nvm_ensure_lts_status nvm_filter_lts_versions \ + nvm_lts_status_path nvm_record_lts_status nvm_ensure_lts_status nvm_filter_lts_versions nvm_resolve_lts_name \ nvm_is_alpha_version nvm_normalize_only nvm_resolve_only nvm_only_includes nvm_explain_excluded_version nvm_select_lts_from_nvmrc \ nvm_needs_lts_status nvm_filter_installed_versions nvm_is_version_selected \ nvm_install_binary nvm_install_source nvm_clang_version \ diff --git a/test/fast/Aliases/an alias can store --lts b/test/fast/Aliases/an alias can store --lts index bc1b7d2a..286fdeb8 100755 --- a/test/fast/Aliases/an alias can store --lts +++ b/test/fast/Aliases/an alias can store --lts @@ -47,6 +47,14 @@ nvm alias work --lts=jod 22 >/dev/null 2>&1 || die 'nvm alias work --lts=jod 22 nvm alias other --lts=iron 22 >/dev/null 2>&1 [ "_$(nvm version other)" = '_N/A' ] || die "no installed v22 is iron; got >$(nvm version other)<" +# an LTS name matches every codename containing it, but not as a regex +nvm alias some --lts=o node >/dev/null 2>&1 || die 'nvm alias some --lts=o node failed' +[ "_$(nvm version some)" = '_v24.11.0' ] || die "some should be the latest installed version whose codename contains o; got >$(nvm version some)<" +nvm alias some --lts=ir node >/dev/null 2>&1 || die 'nvm alias some --lts=ir node failed' +[ "_$(nvm version some)" = '_v20.19.0' ] || die "some should be the latest installed version whose codename contains ir; got >$(nvm version some)<" +nvm alias some --lts=. node >/dev/null 2>&1 || die 'nvm alias some --lts=. node failed' +[ "_$(nvm version some)" = '_N/A' ] || die "no codename contains a literal .; got >$(nvm version some)<" + OUTPUT="$(nvm unalias work)" [ "_${OUTPUT}" = '_Deleted alias work - restore it with `nvm alias "work" --lts=jod "22"`' ] || die "the restore hint should keep --lts=jod as its own argument; got >${OUTPUT}<" diff --git a/test/fast/Running 'nvm use', 'nvm exec', 'nvm run' and 'nvm uninstall' with '--lts=name' pick the newest codename containing the name b/test/fast/Running 'nvm use', 'nvm exec', 'nvm run' and 'nvm uninstall' with '--lts=name' pick the newest codename containing the name new file mode 100755 index 00000000..c564f060 --- /dev/null +++ b/test/fast/Running 'nvm use', 'nvm exec', 'nvm run' and 'nvm uninstall' with '--lts=name' pick the newest codename containing the name @@ -0,0 +1,66 @@ +#!/bin/sh + +ORIG_NVM_DIR="${NVM_DIR}" +TMP_DIR='' + +cleanup () { + case "${TMP_DIR}" in + /*) rm -rf "${TMP_DIR}" ;; + esac + NVM_DIR="${ORIG_NVM_DIR}" + export NVM_DIR +} + +die () { echo "$@" ; cleanup ; exit 1; } + +: nvm.sh +\. ../../nvm.sh + +\. ../common.sh + +TMP_DIR="$(mktemp -d)" +NVM_DIR="${TMP_DIR}/nvm" +export NVM_DIR +mkdir -p "${NVM_DIR}/alias/lts" || die 'could not create alias dir' +# `nvm exec` runs `$NVM_DIR/nvm-exec`, which sources the `nvm.sh` beside it +ln -s "${PWD}/../../nvm.sh" "${PWD}/../../nvm-exec" "${NVM_DIR}/" || die 'could not link nvm.sh and nvm-exec' + +for VERSION in v20.19.0 v22.11.0 v24.11.0; do + make_fake_node "${VERSION}" || die "fake node ${VERSION} could not be made" +done +row () { + printf '%s\t2024-01-01\tsrc\t-\t-\t-\t-\t-\t-\t%s\t-\n' "$1" "$2" +} +{ row v26.12.1 -; row v24.11.0 Krypton; row v24.0.0 -; row v22.11.0 Jod; row v20.19.0 Iron; } | nvm_record_lts_status || die 'could not record LTS status' +nvm_make_alias lts/iron v20.19.0 >/dev/null 2>&1 || die 'could not alias lts/iron' +nvm_make_alias lts/jod v22.11.0 >/dev/null 2>&1 || die 'could not alias lts/jod' +nvm_make_alias lts/krypton v24.11.0 >/dev/null 2>&1 || die 'could not alias lts/krypton' +nvm_make_alias 'lts/*' lts/krypton >/dev/null 2>&1 || die 'could not alias lts/*' + +# "o" is in Iron, Jod and Krypton; Krypton is the newest +nvm use --delete-prefix --lts=o >/dev/null 2>&1 || die '`nvm use --lts=o` failed' +[ "_$(nvm current)" = '_v24.11.0' ] || die "\`nvm use --lts=o\` should use the newest codename containing it, krypton; got >$(nvm current)<" +nvm use --delete-prefix --lts=ir >/dev/null 2>&1 || die '`nvm use --lts=ir` failed' +[ "_$(nvm current)" = '_v20.19.0' ] || die "\`nvm use --lts=ir\` should use iron; got >$(nvm current)<" +nvm use --delete-prefix --lts=jod >/dev/null 2>&1 || die '`nvm use --lts=jod` failed' +[ "_$(nvm current)" = '_v22.11.0' ] || die "\`nvm use --lts=jod\` should use jod; got >$(nvm current)<" + +# not a regex +try_err nvm use --delete-prefix --lts=. +[ "_${CAPTURED_EXIT_CODE}" != '_0' ] || die '`nvm use --lts=.` should fail' + +OUTPUT="$(nvm exec --lts=ir node 2>/dev/null)" || die '`nvm exec --lts=ir node` failed' +[ "_$(nvm_echo "${OUTPUT}" | command tail -1)" = '_v20.19.0' ] || die "\`nvm exec --lts=ir node\` should run iron; got >${OUTPUT}<" +case "${OUTPUT}" in + 'Running node LTS "iron" -> v20.19.0'*) ;; + *) die "\`nvm exec --lts=ir\` should name the codename it picked; got >${OUTPUT}<" ;; +esac + +OUTPUT="$(nvm run --silent --lts=kr 2>/dev/null)" || die '`nvm run --lts=kr` failed' +[ "_$(nvm_echo "${OUTPUT}" | command tail -1)" = '_v24.11.0' ] || die "\`nvm run --lts=kr\` should run krypton; got >${OUTPUT}<" + +nvm uninstall --lts=ir >/dev/null 2>&1 || die '`nvm uninstall --lts=ir` failed' +! nvm_is_version_installed v20.19.0 || die '`nvm uninstall --lts=ir` should uninstall iron' +nvm_is_version_installed v22.11.0 || die '`nvm uninstall --lts=ir` should keep jod' + +cleanup diff --git a/test/fast/Unit tests/nvm install --alias, --default and --save b/test/fast/Unit tests/nvm install --alias, --default and --save index 30e790ff..725ab5f5 100755 --- a/test/fast/Unit tests/nvm install --alias, --default and --save +++ b/test/fast/Unit tests/nvm install --alias, --default and --save @@ -72,6 +72,32 @@ nvm install -s --lts --default "${version}" > /dev/null 2>&1 || die "\`nvm insta OUTPUT="$(nvm_alias default)" [ "${OUTPUT}" = "${version}" ] || die "expected the default alias to be ${version} for \`--lts ${version}\`, got >${OUTPUT}<" +# `--lts=` aliases the newest codename containing the name +reset +printf 'v22.0.0 jod\n%s iron\nv18.0.0 hydrogen\n' "${version}" > "$(nvm_lts_status_path)" +nvm install -s --lts=ir --default > /dev/null 2>&1 || die '`nvm install -s --lts=ir --default` failed' +OUTPUT="$(nvm_alias default)" +[ "${OUTPUT}" = 'lts/iron' ] || die "expected the default alias to be lts/iron after \`--lts=ir\`, got >${OUTPUT}<" + +# again, now that the version is installed +nvm alias default v18.0.0 > /dev/null 2>&1 +nvm install -s --lts=ir --default > /dev/null 2>&1 || die '`nvm install -s --lts=ir --default` failed for an installed version' +OUTPUT="$(nvm_alias default)" +[ "${OUTPUT}" = 'lts/iron' ] || die "expected the default alias to be lts/iron for an installed version, got >${OUTPUT}<" +nvm install -s --lts=ir --alias=foo > /dev/null 2>&1 || die '`nvm install -s --lts=ir --alias=foo` failed for an installed version' +OUTPUT="$(nvm_alias foo)" +[ "${OUTPUT}" = 'lts/iron' ] || die "expected the foo alias to be lts/iron for an installed version, got >${OUTPUT}<" + +reset +nvm install -s --lts=ir --alias=foo > /dev/null 2>&1 || die '`nvm install -s --lts=ir --alias=foo` failed' +OUTPUT="$(nvm_alias foo)" +[ "${OUTPUT}" = 'lts/iron' ] || die "expected the foo alias to be lts/iron after \`--lts=ir\`, got >${OUTPUT}<" + +# offline, `--lts=` finds the installed version of that codename +nvm_echo "${version}" > "$(nvm_alias_path)/lts/iron" +OUTPUT="$(nvm_remote_version() { return 3; }; nvm install --offline --lts=ir 2>&1)" || die "\`nvm install --offline --lts=ir\` failed: >${OUTPUT}<" +rm -f "$(nvm_lts_status_path)" + reset nvm install --save "${version}" > /dev/null 2>&1 || die "\`nvm install --save ${version}\` failed" OUTPUT="$(cat .nvmrc 2> /dev/null)" diff --git a/test/fast/Unit tests/nvm_ls_remote LTS name is a substring, not a regex b/test/fast/Unit tests/nvm_ls_remote LTS name is a substring, not a regex new file mode 100755 index 00000000..c119d6ac --- /dev/null +++ b/test/fast/Unit tests/nvm_ls_remote LTS name is a substring, not a regex @@ -0,0 +1,35 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +: nvm.sh +\. ../../../nvm.sh + +# these mocks must not be recorded as the LTS status of $NVM_DIR's versions +nvm_record_lts_status() { :; } + +nvm_download() { + cat "$PWD/mocks/nodejs.org-dist-index.tab" +} + +\. ../../common.sh + +# no codename contains these characters literally, though most match them as a regex +for NVM_LTS in . 'i.on' 'ar.*' '(' '^i' 'n$'; do + try nvm_ls_remote + [ "_$CAPTURED_STDOUT" = "_N/A" ] || die "\`NVM_LTS='${NVM_LTS}' nvm_ls_remote\` should output N/A; got >$CAPTURED_STDOUT<" + [ "_$CAPTURED_EXIT_CODE" = "_3" ] || die "\`NVM_LTS='${NVM_LTS}' nvm_ls_remote\` should exit 3; got $CAPTURED_EXIT_CODE" +done +unset NVM_LTS + +# a name matches every codename that contains it +OUTPUT="$(NVM_LTS=on nvm_ls_remote)" +CODENAMES="$(nvm_echo "$OUTPUT" | command awk '{ print $2 }' | command sort -u | command tr '\n' ' ')" +[ "_$CODENAMES" = "_Argon Boron Carbon Iron " ] || die "\`NVM_LTS=on nvm_ls_remote\` should list the Argon, Boron, Carbon and Iron lines; got >$CODENAMES<" + +for NVM_LTS in iron ir; do + OUTPUT="$(nvm_ls_remote)" + [ -n "$OUTPUT" ] || die "\`NVM_LTS=${NVM_LTS} nvm_ls_remote\` output nothing" + OTHER_LINES="$(nvm_echo "$OUTPUT" | command awk '$2 != "Iron"')" + [ -z "$OTHER_LINES" ] || die "\`NVM_LTS=${NVM_LTS} nvm_ls_remote\` listed versions outside the Iron line: >$OTHER_LINES<" +done diff --git a/test/fast/Unit tests/nvm_resolve_lts_name b/test/fast/Unit tests/nvm_resolve_lts_name new file mode 100755 index 00000000..ee026890 --- /dev/null +++ b/test/fast/Unit tests/nvm_resolve_lts_name @@ -0,0 +1,57 @@ +#!/bin/sh + +ORIG_NVM_DIR="${NVM_DIR}" +TMP_DIR='' + +cleanup () { + case "${TMP_DIR}" in + /*) rm -rf "${TMP_DIR}" ;; + esac + NVM_DIR="${ORIG_NVM_DIR}" + export NVM_DIR +} + +die () { echo "$@" ; cleanup ; exit 1; } + +: nvm.sh +\. ../../../nvm.sh + +TMP_DIR="$(mktemp -d)" +NVM_DIR="${TMP_DIR}/nvm" +export NVM_DIR +mkdir -p "${NVM_DIR}" || die 'could not create NVM_DIR' + +[ "_$(nvm_resolve_lts_name on)" = '_on' ] || die "without recorded LTS status, the name should be kept; got >$(nvm_resolve_lts_name on)<" + +row () { + printf '%s\t2024-01-01\tsrc\t-\t-\t-\t-\t-\t-\t%s\t-\n' "$1" "$2" +} +# not newest first, so the newest codename has to come from the versions +{ row v20.19.0 Iron; row v26.12.1 -; row v24.11.0 Krypton; row v4.9.1 Argon; row v24.0.0 -; row v22.11.0 Jod; row v8.17.0 Carbon; } | nvm_record_lts_status || die 'could not record LTS status' + +check () { + ACTUAL="$(nvm_resolve_lts_name "$1")" + [ "_${ACTUAL}" = "_$2" ] || die "\`nvm_resolve_lts_name '$1'\` should print >$2<; got >${ACTUAL}<" +} + +# the newest codename containing the name +check on krypton +check o krypton +check ar carbon +check arg argon +check ir iron +check iron iron +check j jod +# not a regex, and not a codename yet, so kept as given +check . . +check 'i.on' 'i.on' +check '(' '(' +check nosuch nosuch +# names nvm resolves some other way +check '*' '*' +check -1 -1 +check '' '' +# LTS names are lowercase; an uppercase one is kept, for `nvm_normalize_lts` to reject +check Iron Iron + +cleanup