From 970ed9ea18ba51b2f55b9da585b801e06318b0a0 Mon Sep 17 00:00:00 2001 From: Wei-Lun Li Date: Thu, 29 Oct 2026 09:48:45 +0900 Subject: [PATCH] [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 Signed-off-by: Wei-Lun Li --- nvm.sh | 6 ++++-- test/fast/Unit tests/nvm_die_on_prefix | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/nvm.sh b/nvm.sh index 6140b386..22024257 100755 --- a/nvm.sh +++ b/nvm.sh @@ -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 diff --git a/test/fast/Unit tests/nvm_die_on_prefix b/test/fast/Unit tests/nvm_die_on_prefix index 8c1f3884..11237fde 100755 --- a/test/fast/Unit tests/nvm_die_on_prefix +++ b/test/fast/Unit tests/nvm_die_on_prefix @@ -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"