[Fix] nvm uninstall: report version directory removal failures

The permission preflight cannot guarantee that recursive deletion will succeed.
Ignoring the removal status can report success and delete aliases even when the installed version was not fully removed.
It also prevents commands such as `nvm prune` from counting failures accurately.

Keep cache cleanup best-effort,
but check version-directory removal before reporting success or deleting aliases.
Preserve the native rm diagnostic and report the directory path to aid recovery from partial removal.

Exercise real permission failures and a cache-only failure independently of download slug naming.
Use portable sourcing parameters, check fixture setup,
isolate diagnostics,
and preserve failure status during cleanup.

Refs #1641, #1732
This commit is contained in:
Peter Dave Hello authored and Jordan Harband committed 2026-12-02 00:20:35 -08:00
1 parent 2f736f5781
commit d6297b5a15
2 files changed
+101 -6

No files matched your search

+9 -6
View File
@@ -6269,11 +6269,11 @@ nvm() {
NVM_SLUG_SOURCE="$(nvm_get_download_slug node source std "${NVM_VERSION}")"
fi
local NVM_SUCCESS_MSG
local NVM_UNINSTALL_NAME
if nvm_is_iojs_version "${NVM_VERSION}"; then
NVM_SUCCESS_MSG="Uninstalled io.js $(nvm_strip_iojs_prefix "${NVM_VERSION}")"
NVM_UNINSTALL_NAME="io.js $(nvm_strip_iojs_prefix "${NVM_VERSION}")"
else
NVM_SUCCESS_MSG="Uninstalled node ${NVM_VERSION}"
NVM_UNINSTALL_NAME="node ${NVM_VERSION}"
fi
local NVM_VERSION_PATH
@@ -6292,9 +6292,12 @@ nvm() {
NVM_CACHE_DIR="$(nvm_cache_dir)"
command rm -rf \
"${NVM_CACHE_DIR}/bin/${NVM_SLUG_BINARY}/files" \
"${NVM_CACHE_DIR}/src/${NVM_SLUG_SOURCE}/files" \
"${NVM_VERSION_PATH}" 2>/dev/null
nvm_echo "${NVM_SUCCESS_MSG}"
"${NVM_CACHE_DIR}/src/${NVM_SLUG_SOURCE}/files" 2>/dev/null || :
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
fi
nvm_echo "Uninstalled ${NVM_UNINSTALL_NAME}"
# rm any aliases that point to uninstalled version.
nvm_is_zsh && setopt local_options nonomatch
@@ -0,0 +1,92 @@
#!/bin/sh
if [ "$(command id -u)" = '0' ]; then
echo 'running as root, which may uninstall any version; skipping'
exit 0
fi
TMP_DIR=''
VERSION_PATH=''
IOJS_PATH=''
CACHE_FILES=''
cleanup () {
if [ -n "${VERSION_PATH}" ] && [ -d "${VERSION_PATH}/bin" ]; then
chmod u+w "${VERSION_PATH}/bin"
fi
if [ -n "${IOJS_PATH}" ] && [ -d "${IOJS_PATH}/bin" ]; then
chmod u+w "${IOJS_PATH}/bin"
fi
if [ -n "${CACHE_FILES}" ] && [ -d "${CACHE_FILES}" ]; then
chmod u+w "${CACHE_FILES}"
fi
case "${TMP_DIR}" in
/*) rm -rf "${TMP_DIR}" ;;
esac
}
die () { echo "$@" ; exit 1; }
TMP_DIR="$(mktemp -d)" || die 'could not create a temporary directory'
trap '
STATUS=$?
cleanup || [ "${STATUS}" -ne 0 ] || STATUS=1
exit "${STATUS}"
' 0
# Check normal diagnostics, independently of the caller's debug setting.
unset NVM_DEBUG
NVM_DIR="${TMP_DIR}/nvm"
export NVM_DIR
mkdir -p "${NVM_DIR}/alias" || die 'could not create alias dir'
: nvm.sh
# dash needs the sourcing arguments in the caller's positional parameters.
set -- --no-use
\. ../../../nvm.sh --no-use || die 'could not source nvm.sh'
set --
\. ../../common.sh || die 'could not source common.sh'
make_fake_node v20.1.1 || die 'could not make fake node'
nvm_make_alias one v20.1.1 || die 'could not create alias'
VERSION_PATH="$(nvm_version_path v20.1.1)" || die 'could not resolve version path'
chmod a-w "${VERSION_PATH}/bin" || die 'could not make bin 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}"
[ -z "${OUTPUT}" ] || die "unexpected stdout from failed uninstall: ${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 and version path: $(cat "${TMP_DIR}/stderr")"
command grep -F -q '/bin/node' "${TMP_DIR}/stderr" || die 'missing rm failure details for node'
nvm_is_version_installed v20.1.1 || die 'node should still be installed'
[ "$(nvm_alias one)" = 'v20.1.1' ] || die 'alias should be kept after failed uninstall'
make_fake_iojs v3.0.0 || die 'could not make fake io.js'
IOJS_PATH="$(nvm_version_path iojs-v3.0.0)" || die 'could not resolve io.js path'
chmod a-w "${IOJS_PATH}/bin" || die 'could not make io.js bin dir read-only'
(set +x; nvm uninstall iojs-v3.0.0 >/dev/null 2>"${TMP_DIR}/iojs-stderr") && die 'expected io.js uninstall to fail'
command grep -F -q 'Failed to uninstall io.js v3.0.0. Check "${NVM_DIR}/versions/io.js/v3.0.0" for remaining files.' "${TMP_DIR}/iojs-stderr" || die "missing io.js uninstall failure message: $(cat "${TMP_DIR}/iojs-stderr")"
# A failure confined to the cache must not prevent a successful uninstall.
chmod u+w "${VERSION_PATH}/bin" || die 'could not restore permissions'
# Isolate cache error handling from the download slug format.
nvm_get_download_slug() { nvm_echo 'uninstall-cache-fixture'; }
CACHE_SLUG="$(nvm_get_download_slug)" || die 'could not resolve cache slug'
CACHE_FILES="$(nvm_cache_dir)/src/${CACHE_SLUG}/files" || die 'could not resolve cache path'
mkdir -p "${CACHE_FILES}/writable" || die 'could not create cache dir'
: > "${CACHE_FILES}/cached-file" || die 'could not create cached file'
: > "${CACHE_FILES}/writable/removable-file" || die 'could not create removable cache file'
chmod a-w "${CACHE_FILES}" || die 'could not make cache dir read-only'
(
set +x
if nvm_is_zsh; then
setopt err_return
fi
nvm uninstall v20.1.1 >"${TMP_DIR}/retry-stdout" 2>"${TMP_DIR}/retry-stderr"
)
STATUS=$?
[ "${STATUS}" -eq 0 ] || die "uninstall failed with a cache-only error: $(cat "${TMP_DIR}/retry-stderr")"
[ ! -s "${TMP_DIR}/retry-stderr" ] || die 'cache cleanup errors should remain silent'
command grep -F -q -x 'Uninstalled node v20.1.1' "${TMP_DIR}/retry-stdout" || die "missing success message: $(cat "${TMP_DIR}/retry-stdout")"
[ -f "${CACHE_FILES}/cached-file" ] || die 'cached file should remain to demonstrate the cache cleanup failure'
[ ! -e "${CACHE_FILES}/writable/removable-file" ] || die 'cache cleanup did not reach the protected directory'
[ ! -d "${VERSION_PATH}" ] || die 'version directory should be removed'
[ ! -f "$(nvm_alias_path)/one" ] || die 'alias should be removed after successful uninstall'