From 81f13638d76e549072ec614d523e9cccfefd0ec9 Mon Sep 17 00:00:00 2001 From: Rahul Beniwal Date: Mon, 24 Nov 2025 21:57:39 +0530 Subject: [PATCH] [Fix] Reject bare LTS codenames in nvm install MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- nvm.sh | 9 +++++++++ test/fast/Unit tests/nvm_remote_version | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/nvm.sh b/nvm.sh index 5db4df3..2300f96 100755 --- a/nvm.sh +++ b/nvm.sh @@ -781,6 +781,15 @@ nvm_remote_version() { else VERSION="$(NVM_LTS="${NVM_LTS-}" nvm_remote_versions "${PATTERN}" | command tail -1)" fi + + if [ -n "${PATTERN}" ] && [ "_${VERSION}" != "_N/A" ] && ! nvm_validate_implicit_alias "${PATTERN}" 2>/dev/null; then + local VERSION_NUM + VERSION_NUM="$(nvm_echo "${VERSION}" | command awk '{print $1}')" + if ! nvm_echo "${VERSION_NUM}" | nvm_grep -q "${PATTERN}"; then + VERSION='N/A' + fi + fi + if [ -n "${NVM_VERSION_ONLY-}" ]; then command awk 'BEGIN { n = split(ARGV[1], a); diff --git a/test/fast/Unit tests/nvm_remote_version b/test/fast/Unit tests/nvm_remote_version index 9a4f94f..96f33f5 100755 --- a/test/fast/Unit tests/nvm_remote_version +++ b/test/fast/Unit tests/nvm_remote_version @@ -75,4 +75,24 @@ EXIT_CODE="$(nvm_remote_version node >/dev/null 2>&1 ; echo $?)" || die "nvm_remote_version node did not return contents of nvm_ls_remote node; got $OUTPUT" [ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_version node did not exit with 0, got $EXIT_CODE" +# Test LTS name rejection (Issue #3474) +# When nvm_remote_versions returns a line with LTS name in description, +# nvm_remote_version should reject it if the pattern doesn't match the version number + +nvm_remote_versions() { + echo "v4.9.1 Argon *" +} +OUTPUT="$(nvm_remote_version Argon)" +EXIT_CODE="$(nvm_remote_version Argon >/dev/null 2>&1 ; echo $?)" +[ "_$OUTPUT" = "_N/A" ] || die "nvm_remote_version Argon should return N/A (LTS name not in version), got $OUTPUT" +[ "_$EXIT_CODE" = "_3" ] || die "nvm_remote_version Argon should exit with code 3, got $EXIT_CODE" + +nvm_remote_versions() { + echo "v4.9.1" +} +OUTPUT="$(nvm_remote_version 4)" +EXIT_CODE="$(nvm_remote_version 4 >/dev/null 2>&1 ; echo $?)" +[ "_$OUTPUT" = "_v4.9.1" ] || die "nvm_remote_version 4 should return v4.9.1, got $OUTPUT" +[ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_version 4 should exit with code 0, got $EXIT_CODE" + cleanup