[Fix] nvm_find_up, nvm_find_project_dir: stop at a Windows drive root instead of looping forever

With a Windows-style `$PWD` (e.g. `D:/Work`, as Git Bash inherits when launched
by a native Windows program), `${path_%/*}` stops changing at `D:`, so the walk
never ends and sourcing `nvm.sh` hangs. Stop when going up doesn't change the
path, which also covers the existing `.` case.

Fixes #3967.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RBGzjdkmgq6LSA7SFtjRsY
This commit is contained in:
Daniel OriyanandClaude Opus 5.5 authored and Jordan Harband committed 2026-10-23 13:54:04 +03:00
1 parent 13eee46e7d
commit ea84cb311e
3 files changed
+16 -4

No files matched your search

+12 -4
View File
@@ -815,9 +815,13 @@ nvm_tree_contains_path() {
nvm_find_project_dir() {
local path_
local parent_
path_="${PWD}"
while [ "${path_}" != "" ] && [ "${path_}" != '.' ] && [ ! -f "${path_}/package.json" ] && [ ! -d "${path_}/node_modules" ]; do
path_=${path_%/*}
while [ "${path_}" != "" ] && [ ! -f "${path_}/package.json" ] && [ ! -d "${path_}/node_modules" ]; do
parent_=${path_%/*}
# stop when there is nowhere left to go up, e.g. `.` or a Windows drive root like `C:`
[ "${parent_}" != "${path_}" ] || break
path_="${parent_}"
done
nvm_echo "${path_}"
}
@@ -825,9 +829,13 @@ nvm_find_project_dir() {
# Traverse up in directory tree to find containing folder
nvm_find_up() {
local path_
local parent_
path_="${PWD}"
while [ "${path_}" != "" ] && [ "${path_}" != '.' ] && [ ! -f "${path_}/${1-}" ]; do
path_=${path_%/*}
while [ "${path_}" != "" ] && [ ! -f "${path_}/${1-}" ]; do
parent_=${path_%/*}
# stop when there is nowhere left to go up, e.g. `.` or a Windows drive root like `C:`
[ "${parent_}" != "${path_}" ] || break
path_="${parent_}"
done
nvm_echo "${path_}"
}