[Fix] nvm_print_implicit_alias: keep STABLE bound, and STABLE and UNSTABLE independent of the caller

`STABLE` is only assigned inside the loop over `LAST_TWO`,
but it is echoed unconditionally afterwards.
With nothing installed, or with only an odd minor,
the loop never runs, so under `set -u` `nvm ls` dies on
`STABLE: unbound variable` instead of resolving `stable` to nothing.

`local STABLE` without an initializer also keeps the caller's value in sh and dash,
so `STABLE=x nvm ls stable` resolves the alias to something that isn't installed.
Initializing it where it is declared fixes both,
the way 139ed9c did for `PATTERN`, `NVM_LTS`, `LTS` and `VERSION`.
`local UNSTABLE` keeps the caller's value the same way,
which its `N/A` fallback doesn't cover, so it is initialized too.

The nounset test now covers the two states that leave `stable` unresolved,
and the caller test that `nvm ls stable` answers the same either way,
and that an outer `UNSTABLE` doesn't become the `unstable` alias.

Co-authored-by: NUAAwqy <146524024+NUAAwqy@users.noreply.github.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
This commit is contained in:
NUAAwqyandJordan Harband authored and Jordan Harband committed 2026-10-29 17:08:50 +00:00
1 parent c8dc8c9b1c
commit 7ab48a653b
3 files changed
+32 -5

No files matched your search

@@ -54,4 +54,19 @@ VERSION=v20.0.0
[ $? = 127 ] || die 'nvm use without a version or an .nvmrc should fail, rather than use an outer VERSION'
unset VERSION
# with nothing installed, `nvm ls stable` has no version to resolve to, so an outer STABLE must not supply one
NVM_DIR="${TMP_DIR}/empty"
mkdir -p "${NVM_DIR}"
OUTPUT="$(nvm ls stable | tr -d ' \n')"
STABLE=v20.0.0
OUTPUT_WITH_STABLE="$(nvm ls stable | tr -d ' \n')"
[ "_${OUTPUT_WITH_STABLE}" = "_${OUTPUT}" ] || die "nvm ls stable should ignore an outer STABLE; got >${OUTPUT_WITH_STABLE}< instead of >${OUTPUT}<"
unset STABLE
# likewise for an outer UNSTABLE; `nvm ls unstable` can't show it, since the leaked version isn't installed either
UNSTABLE=v21.0.0
OUTPUT="$(nvm_print_implicit_alias local unstable)"
[ "_${OUTPUT}" = '_N/A' ] || die "nvm_print_implicit_alias local unstable should ignore an outer UNSTABLE; got >${OUTPUT}<"
unset UNSTABLE
cleanup