Skip to content

Commit e77cead

Browse files
gridbugsljharb
authored andcommitted
Normalize paths in nvm_tree_contains_path before comparing
It's possible that `tree` and `node_path` both refer to the same directory, but are lexically different. This converts both paths into a canonical form so that any lexical-only differences are removed. This solves a problem where setting `NVM_DIR` to /home/user//.nvm instead of /home/user/.nvm results in the infamous: nvm is not compatible with the npm config "prefix" option This adds a test that the above case no longer results in an error. An existing test stopped failing as expected as a result, as the test set the npm prefix to a local-directory-relative path, which resolved to a directory inside the `NVM_DIR` when running the tests from within the `NVM_DIR`. This test is updated to use an absolute path.
1 parent c26bd93 commit e77cead

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

nvm.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,9 @@ unset NVM_SCRIPT_SOURCE 2>/dev/null
317317

318318
nvm_tree_contains_path() {
319319
local tree
320-
tree="${1-}"
320+
tree=$(realpath --canonicalize-missing "${1-}")
321321
local node_path
322-
node_path="${2-}"
322+
node_path=$(realpath --canonicalize-missing "${2-}")
323323

324324
if [ "@${tree}@" = "@@" ] || [ "@${node_path}@" = "@@" ]; then
325325
nvm_err "both the tree and the node path are required"

test/fast/Unit tests/nvm_die_on_prefix

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,18 @@ Run \`foo\` to unset it."
186186
[ "_$EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' with user .npmrc that has globalconfig did not exit with 10; got '$EXIT_CODE'"
187187
)
188188

189+
npm() {
190+
local args
191+
args="$@"
192+
if [ "_$args" = "_config --loglevel=warn get prefix" ]; then
193+
echo "/home/foo/.nvm/path/to/version"
194+
fi
195+
}
196+
NON_NORMALIZED_NVM_DIR="//home//foo//.nvm/"
197+
OUTPUT="$(export NVM_DIR="$NON_NORMALIZED_NVM_DIR" ; nvm_die_on_prefix 0 foo 2>&1)"
198+
EXPECTED_OUTPUT=''
199+
EXIT_CODE="$(export NVM_DIR="$NON_NORMALIZED_NVM_DIR" ; nvm_die_on_prefix 0 foo >/dev/null 2>&1; echo $?)"
200+
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT' with non-normalized NVM_DIR; got '$OUTPUT'"
201+
[ "_$EXIT_CODE" = "_0" ] || die "'nvm_die_on_prefix 0 foo' did not exit with 0 with non-normalized NVM_DIR; got '$EXIT_CODE'"
202+
189203
cleanup

0 commit comments

Comments
 (0)