Skip to content

Commit 5dddeef

Browse files
committed
nvm use: perfect pre-npm-config env var checking
1 parent 8a8dcbb commit 5dddeef

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

nvm.sh

+24-8
Original file line numberDiff line numberDiff line change
@@ -2111,19 +2111,35 @@ nvm_die_on_prefix() {
21112111
return 2
21122112
fi
21132113

2114-
if [ -n "${PREFIX-}" ] && ! (nvm_tree_contains_path "$NVM_DIR" "$PREFIX" >/dev/null 2>&1); then
2114+
# npm first looks at $PREFIX (case-sensitive)
2115+
# we do not bother to test the value here; if this env var is set, unset it to continue.
2116+
if [ -n "${PREFIX-}" ]; then
21152117
nvm deactivate >/dev/null 2>&1
2116-
nvm_err "nvm is not compatible with the \"PREFIX\" environment variable: currently set to \"$PREFIX\""
2118+
nvm_err "nvm is not compatible with the \"PREFIX\" environment variable: currently set to \"${PREFIX}\""
21172119
nvm_err 'Run `unset PREFIX` to unset it.'
21182120
return 3
21192121
fi
21202122

2121-
if [ -n "${NPM_CONFIG_PREFIX-}" ] && ! (nvm_tree_contains_path "$NVM_DIR" "$NPM_CONFIG_PREFIX" >/dev/null 2>&1); then
2122-
nvm deactivate >/dev/null 2>&1
2123-
nvm_err "nvm is not compatible with the \"NPM_CONFIG_PREFIX\" environment variable: currently set to \"$NPM_CONFIG_PREFIX\""
2124-
nvm_err 'Run `unset NPM_CONFIG_PREFIX` to unset it.'
2125-
return 4
2126-
elif ! nvm_has 'npm'; then
2123+
# npm normalizes NPM_CONFIG_-prefixed env vars
2124+
# https://github.com/npm/npmconf/blob/22827e4038d6eebaafeb5c13ed2b92cf97b8fb82/npmconf.js#L331-L348
2125+
# https://github.com/npm/npm/blob/5e426a78ca02d0044f8dd26e0c5f881217081cbd/lib/config/core.js#L343-L359
2126+
#
2127+
# here, we avoid trying to replicate "which one wins" or testing the value; if any are defined, it errors
2128+
# until none are left.
2129+
local NVM_NPM_CONFIG_PREFIX_ENV
2130+
NVM_NPM_CONFIG_PREFIX_ENV="$(command env | nvm_grep -i NPM_CONFIG_PREFIX | command tail -1 | command awk -F '=' '{print $1}')"
2131+
if [ -n "${NVM_NPM_CONFIG_PREFIX_ENV-}" ]; then
2132+
local NVM_CONFIG_VALUE
2133+
eval "NVM_CONFIG_VALUE=\"\$${NVM_NPM_CONFIG_PREFIX_ENV}\""
2134+
if [ -n "${NVM_CONFIG_VALUE-}" ]; then
2135+
nvm deactivate >/dev/null 2>&1
2136+
nvm_err "nvm is not compatible with the \"${NVM_NPM_CONFIG_PREFIX_ENV}\" environment variable: currently set to \"${NVM_CONFIG_VALUE}\""
2137+
nvm_err "Run \`unset ${NVM_NPM_CONFIG_PREFIX_ENV}\` to unset it."
2138+
return 4
2139+
fi
2140+
fi
2141+
2142+
if ! nvm_has 'npm'; then
21272143
return
21282144
fi
21292145

test/fast/Unit tests/nvm_die_on_prefix

+10-3
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,24 @@ OUTPUT="$(nvm_die_on_prefix 0 foo 2>&1)"
4646
OUTPUT="$(PREFIX=bar nvm_die_on_prefix 0 foo 2>&1)"
4747
EXPECTED_OUTPUT='nvm is not compatible with the "PREFIX" environment variable: currently set to "bar"
4848
Run `unset PREFIX` to unset it.'
49-
EXIT_CODE="$(PREFIX=bar nvm_die_on_prefix 0 foo >/dev/null 2>&1; echo $?)"
49+
EXIT_CODE="$(export PREFIX=bar ; nvm_die_on_prefix 0 foo >/dev/null 2>&1; echo $?)"
5050
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'PREFIX=bar nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'"
5151
[ "_$EXIT_CODE" = "_3" ] || die "'PREFIX=bar nvm_die_on_prefix 0 foo' did not exit with 3; got '$EXIT_CODE'"
5252

53-
OUTPUT="$(NPM_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo 2>&1)"
53+
OUTPUT="$(export NPM_CONFIG_PREFIX=bar ; nvm_die_on_prefix 0 foo 2>&1)"
5454
EXPECTED_OUTPUT='nvm is not compatible with the "NPM_CONFIG_PREFIX" environment variable: currently set to "bar"
5555
Run `unset NPM_CONFIG_PREFIX` to unset it.'
56-
EXIT_CODE="$(NPM_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo >/dev/null 2>&1; echo $?)"
56+
EXIT_CODE="$(export NPM_CONFIG_PREFIX=bar ; nvm_die_on_prefix 0 foo >/dev/null 2>&1; echo $?)"
5757
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'NPM_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'"
5858
[ "_$EXIT_CODE" = "_4" ] || die "'NPM_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo' did not exit with 4; got '$EXIT_CODE'"
5959

60+
OUTPUT="$(export npm_CONFIG_PREFIX=bar ; nvm_die_on_prefix 0 foo 2>&1)"
61+
EXPECTED_OUTPUT='nvm is not compatible with the "npm_CONFIG_PREFIX" environment variable: currently set to "bar"
62+
Run `unset npm_CONFIG_PREFIX` to unset it.'
63+
EXIT_CODE="$(export npm_CONFIG_PREFIX=bar ; nvm_die_on_prefix 0 foo >/dev/null 2>&1; echo $?)"
64+
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'npm_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'"
65+
[ "_$EXIT_CODE" = "_4" ] || die "'npm_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo' did not exit with 4; got '$EXIT_CODE'"
66+
6067
npm() {
6168
local args
6269
args="$@"

0 commit comments

Comments
 (0)