[Fix] nvm_die_on_prefix: check a nonexistent prefix on Windows

On Windows, `npm_config_prefix` is resolved with `cd` + `pwd` so it can be compared with `$NVM_DIR`.
When the directory does not exist yet, that resolution fails,
the value becomes empty, and the check is skipped.
Keep the value as given in that case, like on other systems.

Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Wei-Lun Li <b84330808@gmail.com>
This commit is contained in:
Wei-Lun Li authored and Jordan Harband committed 2026-10-29 09:48:45 +09:00
1 parent c7a1b344da
commit 970ed9ea18
2 files changed
+12 -2

No files matched your search

+4 -2
View File
@@ -4223,8 +4223,10 @@ nvm_die_on_prefix() {
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
NVM_CONFIG_VALUE="$(nvm_cd "$NVM_CONFIG_VALUE" 2>/dev/null && pwd)"
local NVM_CONFIG_DIR
# a prefix that does not exist yet can not be resolved, so it is checked as-is
if [ -n "${NVM_CONFIG_VALUE-}" ] && [ "_${NVM_OS}" = "_win" ] && NVM_CONFIG_DIR="$(nvm_cd "$NVM_CONFIG_VALUE" 2>/dev/null && pwd)"; then
NVM_CONFIG_VALUE="${NVM_CONFIG_DIR}"
fi
if [ -n "${NVM_CONFIG_VALUE-}" ] && ! nvm_tree_contains_path "${NVM_DIR}" "${NVM_CONFIG_VALUE}"; then
nvm deactivate >/dev/null 2>&1
+8
View File
@@ -97,6 +97,14 @@ Run `unset NPM_CONFIG_PREFIX` to unset it.'
[ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "'NPM_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'"
[ "_$CAPTURED_EXIT_CODE" = "_4" ] || die "'NPM_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo' did not exit with 4; got '$CAPTURED_EXIT_CODE'"
# on Windows, a prefix that does not exist can not be resolved, but it is still not inside `$NVM_DIR`
(
nvm_get_os() { nvm_echo win; }
NPM_CONFIG_PREFIX=bar try_err nvm_die_on_prefix 0 foo "$(nvm_version_dir new)"
[ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "on Windows, 'NPM_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'"
[ "_$CAPTURED_EXIT_CODE" = "_4" ] || die "on Windows, 'NPM_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo' did not exit with 4; got '$CAPTURED_EXIT_CODE'"
) || die 'nvm_die_on_prefix must not skip an unresolvable NPM_CONFIG_PREFIX on Windows'
npm_CONFIG_PREFIX=bar try_err nvm_die_on_prefix 0 foo "$(nvm_version_dir new)"
unset npm_CONFIG_PREFIX
EXPECTED_OUTPUT='nvm is not compatible with the "npm_CONFIG_PREFIX" environment variable: currently set to "bar"