[Fix] Handle filesystem identity when checking path containment
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #1049.
authored by Mingyang Wu · committed by Jordan Harband · parent 46ca42f · Browse files · 2 files·+35 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Assisted-by: OpenAI GPT-6 <noreply@openai.com>
Diff stats
2 files+35 added−7 removed
- tests:+30 added−0 removed
- other:+5 added−7 removed
Provenance
- Commit
cb96d2a- Tree
344f977- Author
Mingyang Wu <129849514+aprylewu@users.noreply.github.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
46ca42f
2 files changed+35 added−7 removed
modified:test/fast/Unit tests/nvm_tree_contains_path+30 added
1 #!/bin/sh2 3 cleanup () {4+ rm tmp-alias5+ if [ "${CASE_SENSITIVE-}" = '1' ]; then6+ rm tmp/TREE/node7+ rmdir tmp/TREE8+ fi9+ rm tmp/tree/node10+ rmdir tmp/tree11 rm tmp/node12 rmdir tmp13 rm tmp2/node14 rmdir tmp26 unmodified lines21 mkdir -p tmp22 touch tmp/node23 mkdir -p tmp224 touch tmp2/node25+mkdir -p tmp/tree26+touch tmp/tree/node27+ln -s tmp tmp-alias28+CASE_SENSITIVE=029+if [ ! -d tmp/TREE ]; then30+ CASE_SENSITIVE=131+ mkdir tmp/TREE32+ touch tmp/TREE/node33+fi34 35 [ "$(nvm_tree_contains_path 2>&1)" = "both the tree and the node path are required" ] || die 'incorrect error message with no args'36 [ "$(nvm_tree_contains_path > /dev/null 2>&1 ; echo $?)" = "2" ] || die 'incorrect error code with no args'37 [ "$(nvm_tree_contains_path tmp 2>&1)" = "both the tree and the node path are required" ] || die 'incorrect error message with one arg'13 unmodified lines51 52 nvm_tree_contains_path "tmp[glob]" "tmp[glob]/node" || die '"tmp[glob]" should contain "tmp[glob]/node"'53 nvm_tree_contains_path "tmp" "tmp[glob]/node" && die '"tmp" should not contain "tmp[glob]/node"'54 55+if [ "${CASE_SENSITIVE}" = '1' ]; then56+ nvm_tree_contains_path tmp/tree tmp/TREE/node && die 'case-sensitive directories should stay distinct'57+ nvm_tree_contains_path tmp/TREE tmp/tree/node && die 'case-sensitive directories should stay distinct'58+else59+ nvm_tree_contains_path tmp/tree tmp/TREE/node || die 'case-insensitive directories should match'60+ nvm_tree_contains_path tmp/TREE tmp/tree/node || die 'case-insensitive directories should match'61+fi62+63+nvm_tree_contains_path tmp-alias tmp/node || die 'a symlink to the tree should contain its node'64+nvm_tree_contains_path tmp tmp-alias/node || die 'the tree should contain a node reached through a symlink'65+nvm_tree_contains_path tmp-alias tmp2/node && die 'a symlink should not contain a sibling tree'66+67+nvm_tree_contains_path nonexistent NONEXISTENT/node && die 'missing directories should not be case folded'68+69 cleanupmodified:nvm.sh+5 added-7 removed
550 unmodified lines551 esac552 fi553 unset NVM_SCRIPT_SOURCE 2>/dev/null554 555-# Performs pure in-memory POSIX path containment checking without subshell process forks.556-# Uses case-guarded ${pathdir%/*} for parent-walk and exact string equality (=) to ensure literal matching557-# for directory names containing glob metacharacters (*, ?, []) across all shells including zsh.555+# Walks parent paths without subshells, comparing names literally before checking556+# filesystem identity to account for case-insensitive paths and directory symlinks.557 nvm_tree_contains_path() {558 local tree559 tree="${1-}"560 local node_path14 unmodified lines575 if [ -z "${clean_tree}" ]; then576 clean_tree='/'577 fi578 580- # Pure in-memory POSIX parent-walk using parameter expansion instead of subshell dirname forks.581- # Uses literal string equality [ "${pathdir}" = "${clean_tree}" ] to prevent glob expansion bugs.579+ # Keep literal matching for paths that do not exist, including glob characters.580 local pathdir581 pathdir="${node_path}"582 while [ "${pathdir}" != '' ] && [ "${pathdir}" != '.' ] && [ "${pathdir}" != '/' ] &&585- [ "${pathdir}" != "${clean_tree}" ]; do583+ [ "${pathdir}" != "${clean_tree}" ] && ! [ "${pathdir}" -ef "${clean_tree}" ]; do584 case "${pathdir}" in585 */*)586 pathdir="${pathdir%/*}"587 if [ -z "${pathdir}" ]; then4 unmodified lines592 break593 ;;594 esac595 done598- [ "${pathdir}" = "${clean_tree}" ]596+ [ "${pathdir}" = "${clean_tree}" ] || [ "${pathdir}" -ef "${clean_tree}" ]597 }598 599 nvm_find_project_dir() {600 local path_4831 unmodified lines