[Fix] Handle filesystem identity when checking path containment

Fixes #1049.

Assisted-by: OpenAI GPT-6 <noreply@openai.com>
This commit is contained in:
Mingyang Wu authored and Jordan Harband committed 2026-09-11 16:49:46 +08:00
1 parent 46ca42f939
commit cb96d2a56a
2 files changed
+35 -7

No files matched your search

+5 -7
View File
@@ -552,9 +552,8 @@ else
fi
unset NVM_SCRIPT_SOURCE 2>/dev/null
# Performs pure in-memory POSIX path containment checking without subshell process forks.
# Uses case-guarded ${pathdir%/*} for parent-walk and exact string equality (=) to ensure literal matching
# for directory names containing glob metacharacters (*, ?, []) across all shells including zsh.
# Walks parent paths without subshells, comparing names literally before checking
# filesystem identity to account for case-insensitive paths and directory symlinks.
nvm_tree_contains_path() {
local tree
tree="${1-}"
@@ -577,12 +576,11 @@ nvm_tree_contains_path() {
clean_tree='/'
fi
# Pure in-memory POSIX parent-walk using parameter expansion instead of subshell dirname forks.
# Uses literal string equality [ "${pathdir}" = "${clean_tree}" ] to prevent glob expansion bugs.
# Keep literal matching for paths that do not exist, including glob characters.
local pathdir
pathdir="${node_path}"
while [ "${pathdir}" != '' ] && [ "${pathdir}" != '.' ] && [ "${pathdir}" != '/' ] &&
[ "${pathdir}" != "${clean_tree}" ]; do
[ "${pathdir}" != "${clean_tree}" ] && ! [ "${pathdir}" -ef "${clean_tree}" ]; do
case "${pathdir}" in
*/*)
pathdir="${pathdir%/*}"
@@ -595,7 +593,7 @@ nvm_tree_contains_path() {
;;
esac
done
[ "${pathdir}" = "${clean_tree}" ]
[ "${pathdir}" = "${clean_tree}" ] || [ "${pathdir}" -ef "${clean_tree}" ]
}
nvm_find_project_dir() {