From d66749b21c38b58eb449f6205259f4a2cb639d23 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 16 Nov 2026 15:31:02 -0800 Subject: [PATCH] [Fix] `install.sh`: run the system npm outside the project `nvm_check_global_modules` ran `npm --version` and `npm list -g` in `$PWD`, so the project `.npmrc` applied, eg its `logs-dir` on npm v8.11 through v10.5. The "npm unavailable" test put its fake npm on a relative `PATH` entry, which no longer resolves from `/`. --- install.sh | 5 ++-- test/install_script/nvm_check_global_modules | 26 ++++++++++++++++---- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/install.sh b/install.sh index 73b5b34a..68333133 100755 --- a/install.sh +++ b/install.sh @@ -403,14 +403,15 @@ nvm_check_global_modules() { */*) nvm_is_wsl && [ -f "${NPM_COMMAND%/*}/npm.cmd" ] && return 0 ;; esac + # run npm from `/`, so it does not apply the npmrc of the (untrusted) project that `$PWD` is in local NPM_VERSION - NPM_VERSION="$(npm --version)" + NPM_VERSION="$(command cd / && npm --version)" NPM_VERSION="${NPM_VERSION:--1}" [ "${NPM_VERSION%%[!-0-9]*}" -gt 0 ] || return 0 local NPM_GLOBAL_MODULES NPM_GLOBAL_MODULES="$( - npm list -g --depth=0 | + (command cd / && npm list -g --depth=0) | command sed -e '/ npm@/d' -e '/ (empty)$/d' )" diff --git a/test/install_script/nvm_check_global_modules b/test/install_script/nvm_check_global_modules index 3a63cbe2..38e96ab3 100755 --- a/test/install_script/nvm_check_global_modules +++ b/test/install_script/nvm_check_global_modules @@ -4,13 +4,13 @@ cleanup () { rm -rf "$npm_config_prefix/lib" >/dev/null 2>&1 unset npm_config_prefix - rm -f npm + [ -z "${FAKE_NPM_DIR-}" ] || rm -rf "${FAKE_NPM_DIR}" [ -z "${WINDOWS_NPM_DIR-}" ] || rm -rf "${WINDOWS_NPM_DIR}" PATH="$ORIGINAL_PATH" NVM_DIR="$ORIGINAL_NVM_DIR" unset -f setup cleanup die - unset message ORIGINAL_PATH ORIGINAL_NVM_DIR WINDOWS_NPM_DIR + unset message ORIGINAL_PATH ORIGINAL_NVM_DIR FAKE_NPM_DIR WINDOWS_NPM_DIR } die () { echo "$@" ; cleanup ; exit 1; } @@ -55,9 +55,10 @@ message=$(nvm_check_global_modules) [ -z "$message" ] || die "nvm_check_global_modules should have not printed a notice when npm had only itself installed as a global module; got:\n${message}" # Faking the absence of npm -PATH=".:$PATH" -touch npm -chmod +x npm +FAKE_NPM_DIR="$(mktemp -d)" +touch "${FAKE_NPM_DIR}/npm" +chmod +x "${FAKE_NPM_DIR}/npm" +PATH="${FAKE_NPM_DIR}:$PATH" message=$(nvm_check_global_modules) [ -z "$message" ] || die "nvm_check_global_modules should have not printed a notice when npm was unavailable; got:\n${message}" @@ -83,5 +84,20 @@ nvm_is_wsl() { return 1; } message=$(nvm_check_global_modules) [ -n "$message" ] || die 'nvm_check_global_modules should still warn about a Windows npm outside WSL, where it is the system node' +# npm must not apply the npmrc of the project that `$PWD` is in +cat <<'SH' >"${FAKE_NPM_DIR}/npm" +#!/bin/sh +case "$1" in + --version) echo '10.0.0' ;; + list) printf '%s\n' 'npm@10.0.0' "└── cwd@[$(pwd)]" ;; +esac +SH +PATH="${FAKE_NPM_DIR}:${ORIGINAL_PATH}" +message=$(nvm_check_global_modules) +case "$message" in + *"cwd@[/]"*) ;; + *) die "nvm_check_global_modules should run npm from \`/\`; got:\n${message}" ;; +esac + cleanup