[Fix] install.sh: propagate requested Node.js installation failures

Automated provisioning with NODE_VERSION can continue without the requested Node.js version when the installer discards a failure from `nvm install`.
Re-resolving a successful install's selector locally can also misreport its result for `stable` and `--lts`.

Use the install command's status and preserve it through cleanup and setup guidance.
Cover failure and success paths in isolated Bash processes,
including errexit,
without allowing shell tracing to satisfy assertions on installer output.
This commit is contained in:
Peter Dave Hello authored and Jordan Harband committed 2026-12-02 15:22:57 -08:00
1 parent 2d393cbc36
commit a549731187
2 files changed
+116 -10

No files matched your search

+11 -10
View File
@@ -269,15 +269,13 @@ nvm_install_node() {
fi
nvm_echo "=> Installing Node.js version $NODE_VERSION_LOCAL"
nvm install "$NODE_VERSION_LOCAL"
local CURRENT_NVM_NODE
CURRENT_NVM_NODE="$(nvm_version current)"
if [ "$(nvm_version "$NODE_VERSION_LOCAL")" = "$CURRENT_NVM_NODE" ]; then
nvm_echo "=> Node.js version $NODE_VERSION_LOCAL has been successfully installed"
else
nvm_echo >&2 "Failed to install Node.js $NODE_VERSION_LOCAL"
fi
local NVM_INSTALL_STATUS
nvm install "$NODE_VERSION_LOCAL" || {
NVM_INSTALL_STATUS=$?
nvm_echo >&2 "nvm install $NODE_VERSION_LOCAL failed"
return "$NVM_INSTALL_STATUS"
}
nvm_echo "=> Node.js version $NODE_VERSION_LOCAL has been successfully installed"
}
install_nvm_as_script() {
@@ -600,7 +598,9 @@ nvm_do_install() {
nvm_check_global_modules
nvm_install_node
local NVM_NODE_STATUS
NVM_NODE_STATUS=0
nvm_install_node || NVM_NODE_STATUS=$?
nvm_reset
@@ -609,6 +609,7 @@ nvm_do_install() {
if ${BASH_OR_ZSH} ; then
command printf "${COMPLETION_STR}"
fi
return "$NVM_NODE_STATUS"
}
#
+105
View File
@@ -0,0 +1,105 @@
#!/bin/sh
[ -n "${BASH_VERSION-}" ] || exec bash "$0" "$@"
# Do not let shell tracing satisfy the assertions on installer output.
set +x +v
die () { echo "$@" ; exit 1; }
# Re-execute this test so that the installer is not called in a conditional context.
if [ "${1-}" = '--install' ]; then
NODE_VERSION="$2"
TEST_NODE_STATUS="$3"
TEST_NODE_CURRENT="$4"
NVM_ENV=testing \. "$(dirname "$0")/../../install.sh" || die 'Unable to source install.sh'
# Exercise the installer without requiring a downloader or installing real Node.js versions.
nvm_has_executable() { [ "$1" = curl ]; }
nvm_download() {
case "$2" in
*/nvm.sh)
cat > "$4" <<'NVM_SH'
nvm() {
[ "$1" = install ] && [ "$2" = "$NODE_VERSION" ] || return 99
: > "${NVM_DIR}/node-install-called"
return "$TEST_NODE_STATUS"
}
nvm_version() {
case "$1" in
current) printf '%s\n' "$TEST_NODE_CURRENT" ;;
stable) cat "${NVM_DIR}/alias/stable" ;;
--lts) printf '%s\n' 'N/A'; return 3 ;;
*) printf '%s\n' 'v22.1.0' ;;
esac
}
NVM_SH
;;
*) : > "$4" ;;
esac
}
nvm_check_global_modules() { return 0; }
# Observe cleanup even when errexit terminates the child at the final return.
trap '
if ! type nvm_reset >/dev/null 2>&1 && ! type nvm_do_install >/dev/null 2>&1; then
: > "${NVM_DIR}/installer-reset"
fi
' EXIT
nvm_do_install
exit $?
fi
TEST_DIR="$(mktemp -d)" || die 'Unable to create temporary directory'
trap 'rm -rf "${TEST_DIR}"' 0
export HOME="${TEST_DIR}"
export NVM_DIR="${TEST_DIR}/nvm"
export PROFILE=/dev/null
export METHOD=script
unset NVM_SOURCE NVM_INSTALL_VERSION NVM_INSTALL_GITHUB_REPO
mkdir -p "${NVM_DIR}/alias" || die 'Unable to create installation directory'
printf '%s\n' 'v20.1.0' > "${NVM_DIR}/alias/stable" || die 'Unable to create local stable alias'
check_install() {
NODE_VERSION="$1"
TEST_NODE_STATUS="$2"
TEST_NODE_CURRENT="$3"
EXPECTED_STATUS="$4"
shift 4
rm -f "${NVM_DIR}/node-install-called" "${NVM_DIR}/installer-reset" "${TEST_DIR}/stderr" || die 'Unable to clear test markers'
EXIT_CODE=0
OUTPUT="$(bash "$@" "$0" --install "${NODE_VERSION}" "${TEST_NODE_STATUS}" "${TEST_NODE_CURRENT}" 2>"${TEST_DIR}/stderr")" || EXIT_CODE=$?
ERRORS="$(cat "${TEST_DIR}/stderr")"
[ "${EXIT_CODE}" = "${EXPECTED_STATUS}" ] || die "Expected exit ${EXPECTED_STATUS}, got ${EXIT_CODE} (bash $*): ${OUTPUT} ${ERRORS}"
echo "${OUTPUT}" | grep -Fq 'Close and reopen your terminal' || die "Missing nvm setup instructions: ${OUTPUT}"
# shellcheck disable=SC2016
echo "${OUTPUT}" | grep -Fq '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' || die "Missing nvm source instructions: ${OUTPUT}"
[ -f "${NVM_DIR}/installer-reset" ] || die 'Installer did not finish cleanup'
if [ -z "${NODE_VERSION}" ]; then
[ ! -e "${NVM_DIR}/node-install-called" ] || die 'Unexpected Node.js installation without NODE_VERSION'
else
[ -f "${NVM_DIR}/node-install-called" ] || die 'Node.js installation was not attempted'
if [ "${EXPECTED_STATUS}" = 0 ]; then
echo "${OUTPUT}" | grep -Fq 'has been successfully installed' || die "Missing Node.js success message: ${OUTPUT}"
else
echo "${ERRORS}" | grep -Fxq "nvm install ${NODE_VERSION} failed" || die "Missing nvm install failure message on stderr: ${ERRORS}"
! echo "${OUTPUT}${ERRORS}" | grep -Fq 'has been successfully installed' || die "Unexpected Node.js success message: ${OUTPUT}"
fi
fi
}
check_install '22.1.0' 23 system 23
# Even if the requested version is already active, a failed install is not a success.
check_install '22.1.0' 23 v22.1.0 23
check_install '22.1.0' 0 v22.1.0 0
# Remote stable can differ from a user-defined local stable alias.
check_install stable 0 v22.1.0 0
# nvm install accepts --lts, but nvm_version does not accept this bare flag.
check_install --lts 0 v22.1.0 0
check_install '' 23 system 0
# A nonzero install result must not abort before cleanup and guidance under errexit.
check_install '22.1.0' 23 system 23 -e
check_install '22.1.0' 23 v22.1.0 23 --posix -e
check_install '22.1.0' 0 v22.1.0 0 -e