[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".
This commit is contained in:
Jordan Harband committed 2026-10-21 12:39:28 -07:00
1 parent 43a2abbfd5
commit 79214249bb
2 files changed
+45 -2

No files matched your search

+6 -2
View File
@@ -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")
@@ -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