From 2d393cbc36828391341e37fc71739dbfc7b23722 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 2 Dec 2026 14:35:48 -0800 Subject: [PATCH] [Fix] `nvm uninstall`: remove aliases once a partial deletion takes the binary When the version directory can't be fully removed but its binary was, the version is no longer installed, so retrying `nvm uninstall` reports it as not installed and never reaches the alias cleanup. Remove its aliases anyway, and still report the failure. --- nvm.sh | 12 ++++- ...l removes aliases after a partial deletion | 54 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100755 test/fast/Unit tests/nvm uninstall removes aliases after a partial deletion diff --git a/nvm.sh b/nvm.sh index 39150008..4e49f5cb 100755 --- a/nvm.sh +++ b/nvm.sh @@ -6293,11 +6293,18 @@ nvm() { command rm -rf \ "${NVM_CACHE_DIR}/bin/${NVM_SLUG_BINARY}/files" \ "${NVM_CACHE_DIR}/src/${NVM_SLUG_SOURCE}/files" 2>/dev/null || : + local NVM_UNINSTALL_FAILED + NVM_UNINSTALL_FAILED=0 if ! command rm -rf "${NVM_VERSION_PATH}"; then nvm_err "Failed to uninstall ${NVM_UNINSTALL_NAME}. Check \"$(nvm_sanitize_path "${NVM_VERSION_PATH}")\" for remaining files." - return 1 + # without its binary the version is no longer installed, so a retry would never reach the alias cleanup + if nvm_is_version_installed "${NVM_VERSION}"; then + return 1 + fi + NVM_UNINSTALL_FAILED=1 + else + nvm_echo "Uninstalled ${NVM_UNINSTALL_NAME}" fi - nvm_echo "Uninstalled ${NVM_UNINSTALL_NAME}" # rm any aliases that point to uninstalled version. nvm_is_zsh && setopt local_options nonomatch @@ -6319,6 +6326,7 @@ nvm() { nvm unalias "$(command basename "${NVM_ALIAS_FILE}")" fi done + return "${NVM_UNINSTALL_FAILED}" ;; "prune") nvm_prune "$@" diff --git a/test/fast/Unit tests/nvm uninstall removes aliases after a partial deletion b/test/fast/Unit tests/nvm uninstall removes aliases after a partial deletion new file mode 100755 index 00000000..7226ef9c --- /dev/null +++ b/test/fast/Unit tests/nvm uninstall removes aliases after a partial deletion @@ -0,0 +1,54 @@ +#!/bin/sh + +if [ "$(command id -u)" = '0' ]; then + echo 'running as root, which may uninstall any version; skipping' + exit 0 +fi + +TMP_DIR='' +cleanup () { + if [ -n "${TMP_DIR}" ] && [ -d "${TMP_DIR}/nvm/versions/node" ]; then + chmod u+w "${TMP_DIR}/nvm/versions/node" + fi + case "${TMP_DIR}" in + /*) rm -rf "${TMP_DIR}" ;; + esac +} + +die () { echo "$@" ; cleanup ; exit 1; } + +TMP_DIR="$(mktemp -d)" || die 'could not create a temporary directory' + +unset NVM_DEBUG +NVM_DIR="${TMP_DIR}/nvm" +export NVM_DIR +mkdir -p "${NVM_DIR}/alias" || die 'could not create alias dir' + +: nvm.sh +\. ../../../nvm.sh +\. ../../common.sh + +make_fake_node v20.1.1 || die 'could not make fake node v20.1.1' +make_fake_node v22.1.0 || die 'could not make fake node v22.1.0' +nvm_make_alias one v20.1.1 || die 'could not create alias one' +nvm_make_alias two v22.1.0 || die 'could not create alias two' + +# the version's contents can be deleted, but not its directory +chmod a-w "${NVM_DIR}/versions/node" || die 'could not make versions dir read-only' + +OUTPUT="$(set +x; nvm uninstall v20.1.1 2>"${TMP_DIR}/stderr")" && STATUS=0 || STATUS=$? +[ "${STATUS}" -eq 1 ] || die "expected uninstall to fail; got status ${STATUS}, output: ${OUTPUT}" +command grep -F -q 'Failed to uninstall node v20.1.1. Check "${NVM_DIR}/versions/node/v20.1.1" for remaining files.' "${TMP_DIR}/stderr" || die "missing uninstall failure message: $(cat "${TMP_DIR}/stderr")" +[ -d "${NVM_DIR}/versions/node/v20.1.1" ] || die 'the version directory should remain' +! nvm_is_version_installed v20.1.1 || die 'the fixture should leave v20.1.1 without its binary' +case "${OUTPUT}" in + *Uninstalled*) die "a failed uninstall should not report success: ${OUTPUT}" ;; +esac +case "${OUTPUT}" in + *'Deleted alias one'*) ;; + *) die "expected the dangling alias to be deleted, got: ${OUTPUT}" ;; +esac +[ ! -f "$(nvm_alias_path)/one" ] || die 'alias one should be removed once its version is gone' +[ "$(nvm_alias two)" = 'v22.1.0' ] || die 'aliases to other versions should be kept' + +cleanup