Skip to content
Myr

[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

2 files+35 added−7 removed

modified:test/fast/Unit tests/nvm_tree_contains_path+30 added
1#!/bin/sh23cleanup () {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 tmp2
6 unmodified lines
21mkdir -p tmp22touch tmp/node23mkdir -p tmp224touch tmp2/node25mkdir -p tmp/tree26touch tmp/tree/node27ln -s tmp tmp-alias28CASE_SENSITIVE=029if [ ! -d tmp/TREE ]; then30 CASE_SENSITIVE=131 mkdir tmp/TREE32 touch tmp/TREE/node33fi3435[ "$(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 lines
5152nvm_tree_contains_path "tmp[glob]" "tmp[glob]/node" || die '"tmp[glob]" should contain "tmp[glob]/node"'53nvm_tree_contains_path "tmp" "tmp[glob]/node" && die '"tmp" should not contain "tmp[glob]/node"'5455if [ "${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'58else59 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'61fi6263nvm_tree_contains_path tmp-alias tmp/node || die 'a symlink to the tree should contain its node'64nvm_tree_contains_path tmp tmp-alias/node || die 'the tree should contain a node reached through a symlink'65nvm_tree_contains_path tmp-alias tmp2/node && die 'a symlink should not contain a sibling tree'6667nvm_tree_contains_path nonexistent NONEXISTENT/node && die 'missing directories should not be case folded'6869cleanup
modified:nvm.sh+5 added-7 removed
550 unmodified lines
551 esac552fi553unset NVM_SCRIPT_SOURCE 2>/dev/null554555# 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.557nvm_tree_contains_path() {558 local tree559 tree="${1-}"560 local node_path
14 unmodified lines
575 if [ -z "${clean_tree}" ]; then576 clean_tree='/'577 fi578580 # 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}" ]; then
4 unmodified lines
592 break593 ;;594 esac595 done598 [ "${pathdir}" = "${clean_tree}" ]596 [ "${pathdir}" = "${clean_tree}" ] || [ "${pathdir}" -ef "${clean_tree}" ]597}598599nvm_find_project_dir() {600 local path_
4831 unmodified lines