[Fix] --lts=<name>: 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=<name>` in an alias.

Locally, `--lts=<name>` resolved the `lts/<name>` 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/<codename>` 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 <ljharb@gmail.com>
This commit is contained in:
Jordan HarbandandDivya Chukkapalli committed 2026-11-20 10:36:47 -08:00
1 parent dbda4b9387
commit 62adf80079
6 files changed
+249 -7

No files matched your search

@@ -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}<"
@@ -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
@@ -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=<name>` 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=<name>` 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)"
@@ -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
+57
View File
@@ -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