From 2f736f5781d7b91afeed59b9e3f7045b05a08e94 Mon Sep 17 00:00:00 2001 From: Peter Dave Hello <3691490+PeterDaveHello@users.noreply.github.com> Date: Tue, 1 Dec 2026 03:57:04 -0800 Subject: [PATCH] [Fix] `install.sh`: abort when adding the required Git remote fails An existing installation may need a new remote before it can fetch an update. If adding that remote fails, the helper returns 2 but the installer ignores it, modifies the profile, and can report success while still using the old version. Exit at the failing command, consistent with other fatal Git errors. Use a real Git config lock to exercise both installation methods. Verify that the test reaches its intended failure rather than passing on an unrelated nonzero exit. Regression introduced by dbda4b93871fa032ceb1471e21631101eabbcb9e --- install.sh | 2 +- .../install_script/nvm_do_install_git_failure | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100755 test/install_script/nvm_do_install_git_failure diff --git a/install.sh b/install.sh index cd60d6e7..0cd20ae1 100755 --- a/install.sh +++ b/install.sh @@ -197,7 +197,7 @@ install_nvm_from_git() { NVM_REMOTE_SUFFIX=$((NVM_REMOTE_SUFFIX + 1)) NVM_REMOTE_NAME="nvm-origin-${NVM_REMOTE_SUFFIX}" done - command git --git-dir="${INSTALL_DIR}/.git" remote add "${NVM_REMOTE_NAME}" "${NVM_GIT_SOURCE}" || return 2 + command git --git-dir="${INSTALL_DIR}/.git" remote add "${NVM_REMOTE_NAME}" "${NVM_GIT_SOURCE}" || exit 2 fi else fetch_error="Failed to fetch origin with $NVM_VERSION. Please report this!" diff --git a/test/install_script/nvm_do_install_git_failure b/test/install_script/nvm_do_install_git_failure new file mode 100755 index 00000000..c2c32e9e --- /dev/null +++ b/test/install_script/nvm_do_install_git_failure @@ -0,0 +1,30 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +INSTALLER="$(dirname "$0")/../../install.sh" + +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 NVM_SOURCE="${TEST_DIR}/new-source" +export PROFILE="${TEST_DIR}/.bashrc" +unset NVM_ENV NVM_INSTALL_VERSION NVM_INSTALL_GITHUB_REPO NODE_VERSION + +command git init -q "${NVM_DIR}" || die 'Unable to initialize repository' +command git --git-dir="${NVM_DIR}/.git" remote add origin "${TEST_DIR}/old-source" || die 'Unable to add origin' +# A concurrent git operation can prevent the installer from adding its remote. +: > "${NVM_DIR}/.git/config.lock" || die 'Unable to lock Git config' +: > "${PROFILE}" || die 'Unable to create profile' +# Record sourcing without invoking the system npm if the installer continues. +printf '%s\n' ': > "${NVM_DIR}/sourced"' 'nvm_check_global_modules() { return 0; }' > "${NVM_DIR}/nvm.sh" || die 'Unable to write nvm.sh' + +for METHOD in '' git; do + EXIT_CODE=0 + OUTPUT="$(METHOD="${METHOD}" LC_ALL=C bash +e "${INSTALLER}" 2>&1)" || EXIT_CODE=$? + [ "${EXIT_CODE}" = '2' ] || die "Expected exit 2 for METHOD=${METHOD}, got ${EXIT_CODE}: ${OUTPUT}" + printf '%s\n' "${OUTPUT}" | grep -q 'could not lock config file .*: File exists$' || die "Expected Git config lock error: ${OUTPUT}" + [ -f "${PROFILE}" ] && [ ! -s "${PROFILE}" ] || die 'The installer modified the profile after failing' + [ ! -e "${NVM_DIR}/sourced" ] || die 'The installer sourced nvm.sh after failing' +done