From 79214249bb7b5e1e533e1a0ee8ec575a30f10d64 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 21 Oct 2026 12:39:28 -0700 Subject: [PATCH] [Fix] `nvm uninstall`: only remove aliases that point at the uninstalled version Matching alias files by substring also removed aliases to longer versions, so uninstalling v20.1.1 deleted an alias to v20.1.10. Each alias's target is now read the way `nvm_alias` reads it, ignoring comments, and in zsh an empty alias directory no longer errors with "no matches found". --- nvm.sh | 8 +++- ...removes aliases to the uninstalled version | 39 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100755 test/fast/Unit tests/nvm uninstall only removes aliases to the uninstalled version diff --git a/nvm.sh b/nvm.sh index 4828fd51..673fa050 100755 --- a/nvm.sh +++ b/nvm.sh @@ -4874,8 +4874,12 @@ nvm() { nvm_echo "${NVM_SUCCESS_MSG}" # rm any aliases that point to uninstalled version. - for ALIAS in $(nvm_grep -l -e "${VERSION}" "$(nvm_alias_path)"/* 2>/dev/null); do - nvm unalias "$(command basename "${ALIAS}")" + nvm_is_zsh && setopt local_options nonomatch + local NVM_ALIAS_FILE + for NVM_ALIAS_FILE in "$(nvm_alias_path)"/*; do + if [ -f "${NVM_ALIAS_FILE}" ] && [ "_$(nvm_print_alias_file "${NVM_ALIAS_FILE}" | command head -n 1)" = "_${VERSION}" ]; then + nvm unalias "$(command basename "${NVM_ALIAS_FILE}")" + fi done ;; "deactivate") diff --git a/test/fast/Unit tests/nvm uninstall only removes aliases to the uninstalled version b/test/fast/Unit tests/nvm uninstall only removes aliases to the uninstalled version new file mode 100755 index 00000000..adcee113 --- /dev/null +++ b/test/fast/Unit tests/nvm uninstall only removes aliases to the uninstalled version @@ -0,0 +1,39 @@ +#!/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" || die 'could not create alias dir' + +make_fake_node v20.1.1 || die 'fake node v20.1.1 could not be made' +make_fake_node v20.1.10 || die 'fake node v20.1.10 could not be made' +nvm_make_alias one v20.1.1 || die 'could not alias one' +nvm_make_alias ten v20.1.10 || die 'could not alias ten' +printf 'v20.1.1 # a comment\n' > "$(nvm_alias_path)/commented" || die 'could not alias commented' + +nvm deactivate >/dev/null 2>&1 +nvm uninstall v20.1.1 >/dev/null 2>&1 || die 'nvm uninstall v20.1.1 failed' +! nvm_is_version_installed v20.1.1 || die 'v20.1.1 should be uninstalled' +[ ! -e "$(nvm_alias_path)/one" ] || die 'the alias to v20.1.1 should be removed' +[ ! -e "$(nvm_alias_path)/commented" ] || die 'the alias to v20.1.1 with a comment should be removed' +[ "_$(nvm_alias ten)" = '_v20.1.10' ] || die "the alias to v20.1.10 should be kept; got >$(nvm_alias ten 2>&1)<" + +cleanup