[Perf] nvm_die_on_prefix: detect the OS only when it is needed

This commit is contained in:
Cassidy Marble
2026-10-06 14:00:46 -03:00
committed by Jordan Harband
parent c7ba8d2106
commit f296b8d210
2 changed files with 18 additions and 3 deletions
+4 -3
View File
@@ -3365,9 +3365,6 @@ nvm_die_on_prefix() {
return 3
fi
local NVM_OS
NVM_OS="$(nvm_get_os)"
# npm normalizes NPM_CONFIG_-prefixed env vars
# https://github.com/npm/npmconf/blob/22827e4038d6eebaafeb5c13ed2b92cf97b8fb82/npmconf.js#L331-L348
# https://github.com/npm/npm/blob/5e426a78ca02d0044f8dd26e0c5f881217081cbd/lib/config/core.js#L343-L359
@@ -3377,6 +3374,10 @@ nvm_die_on_prefix() {
local NVM_NPM_CONFIG_x_PREFIX_ENV
NVM_NPM_CONFIG_x_PREFIX_ENV="$(command awk 'BEGIN { for (name in ENVIRON) if (toupper(name) == "NPM_CONFIG_PREFIX") { print name; break } }')"
if [ -n "${NVM_NPM_CONFIG_x_PREFIX_ENV-}" ]; then
# `$NVM_OS` is only used to translate Windows paths, but detecting the OS
# shells out to `uname`, so it is deferred until it is actually needed
local NVM_OS
NVM_OS="$(nvm_get_os)"
local NVM_CONFIG_VALUE
eval "NVM_CONFIG_VALUE=\"\$${NVM_NPM_CONFIG_x_PREFIX_ENV}\""
if [ -n "${NVM_CONFIG_VALUE-}" ] && [ "_${NVM_OS}" = "_win" ]; then
+14
View File
@@ -49,6 +49,20 @@ nvm_has() { return 0; }
OUTPUT="$(nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" 2>&1)"
[ -z "$OUTPUT" ] || die "'nvm_die_on_prefix' was not a noop when prefix is good; got '$OUTPUT'"
# `nvm_get_os` runs `uname`; it is only needed when an `NPM_CONFIG_PREFIX`
# variable is set, so with none set it must not run at all
MOCK_BIN="${TEST_DIR}/bin"
mkdir -p "${MOCK_BIN}"
{
echo '#!/bin/sh'
echo 'echo "uname was called" >&2'
echo 'exit 1'
} > "${MOCK_BIN}/uname"
chmod +x "${MOCK_BIN}/uname"
unset NPM_CONFIG_PREFIX npm_config_prefix npm_CONFIG_PREFIX
OUTPUT="$(PATH="${MOCK_BIN}:${PATH}" nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" 2>&1)"
[ -z "$OUTPUT" ] || die "'nvm_die_on_prefix' detected the OS (ran uname) when no NPM_CONFIG_PREFIX was set; got '$OUTPUT'"
mkdir -p "$(nvm_version_dir new)"
ln -s "$(nvm_version_dir new)" "$TEST_DIR/node"