Skip to content

Commit 81f1363

Browse files
Rahulbeniwal26119ljharb
authored andcommitted
[Fix] Reject bare LTS codenames in nvm install
Previously, `nvm install Argon` would succeed by matching the LTS name in the version description (e.g., "v4.9.1 (Latest LTS: Argon)"), but `nvm uninstall Argon` would fail because "Argon" is not a valid alias or not a valid version. Changes: - Added pattern matching check in nvm_remote_version (nvm.sh:785-791) - Skips check for implicit aliases (node, stable, etc.) to preserve existing functionality - Added unit tests to verify LTS names are rejected while version numbers still work After this fix: - `nvm install Argon` → fails (use `nvm install lts/argon` instead) - `nvm install 4` → still works - `nvm install node` → still works - `nvm install lts/argon` → still works This makes install and uninstall behavior consistent. Fixes #3474.
1 parent 26066c1 commit 81f1363

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

nvm.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,15 @@ nvm_remote_version() {
781781
else
782782
VERSION="$(NVM_LTS="${NVM_LTS-}" nvm_remote_versions "${PATTERN}" | command tail -1)"
783783
fi
784+
785+
if [ -n "${PATTERN}" ] && [ "_${VERSION}" != "_N/A" ] && ! nvm_validate_implicit_alias "${PATTERN}" 2>/dev/null; then
786+
local VERSION_NUM
787+
VERSION_NUM="$(nvm_echo "${VERSION}" | command awk '{print $1}')"
788+
if ! nvm_echo "${VERSION_NUM}" | nvm_grep -q "${PATTERN}"; then
789+
VERSION='N/A'
790+
fi
791+
fi
792+
784793
if [ -n "${NVM_VERSION_ONLY-}" ]; then
785794
command awk 'BEGIN {
786795
n = split(ARGV[1], a);

test/fast/Unit tests/nvm_remote_version

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,24 @@ EXIT_CODE="$(nvm_remote_version node >/dev/null 2>&1 ; echo $?)"
7575
|| die "nvm_remote_version node did not return contents of nvm_ls_remote node; got $OUTPUT"
7676
[ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_version node did not exit with 0, got $EXIT_CODE"
7777

78+
# Test LTS name rejection (Issue #3474)
79+
# When nvm_remote_versions returns a line with LTS name in description,
80+
# nvm_remote_version should reject it if the pattern doesn't match the version number
81+
82+
nvm_remote_versions() {
83+
echo "v4.9.1 Argon *"
84+
}
85+
OUTPUT="$(nvm_remote_version Argon)"
86+
EXIT_CODE="$(nvm_remote_version Argon >/dev/null 2>&1 ; echo $?)"
87+
[ "_$OUTPUT" = "_N/A" ] || die "nvm_remote_version Argon should return N/A (LTS name not in version), got $OUTPUT"
88+
[ "_$EXIT_CODE" = "_3" ] || die "nvm_remote_version Argon should exit with code 3, got $EXIT_CODE"
89+
90+
nvm_remote_versions() {
91+
echo "v4.9.1"
92+
}
93+
OUTPUT="$(nvm_remote_version 4)"
94+
EXIT_CODE="$(nvm_remote_version 4 >/dev/null 2>&1 ; echo $?)"
95+
[ "_$OUTPUT" = "_v4.9.1" ] || die "nvm_remote_version 4 should return v4.9.1, got $OUTPUT"
96+
[ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_version 4 should exit with code 0, got $EXIT_CODE"
97+
7898
cleanup

0 commit comments

Comments
 (0)