From 8486d07a5e087053d540bb2833dd59368882031c Mon Sep 17 00:00:00 2001 From: Mola-maker <229602071+Mola-maker@users.noreply.github.com> Date: Wed, 14 Oct 2026 23:00:32 +0800 Subject: [PATCH] [Fix] `nvm_strip_path`: preserve literal backslashes in `NVM_DIR` --- nvm.sh | 3 ++- test/fast/Unit tests/nvm_strip_path | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/nvm.sh b/nvm.sh index 81cf4d1b..69da5a8f 100755 --- a/nvm.sh +++ b/nvm.sh @@ -1527,7 +1527,8 @@ nvm_strip_path() { return 1 fi local RESULT - RESULT="$(command printf %s "${1-}" | command awk -v NVM_DIR="${NVM_DIR}" -v RS=: ' + RESULT="$(command printf %s "${1-}" | NVM_DIR="${NVM_DIR}" command awk -v RS=: ' + BEGIN { NVM_DIR = ENVIRON["NVM_DIR"] } index($0, NVM_DIR) == 1 { path = substr($0, length(NVM_DIR) + 1) if (path ~ "^(/versions/[^/]*)?/[^/]*'"${2-}"'.*$") { next } diff --git a/test/fast/Unit tests/nvm_strip_path b/test/fast/Unit tests/nvm_strip_path index a900cab4..76ffdd29 100755 --- a/test/fast/Unit tests/nvm_strip_path +++ b/test/fast/Unit tests/nvm_strip_path @@ -23,3 +23,9 @@ TEST_PATH=":/a/b/bin::/c/d/bin:" STRIPPED_PATH=`nvm_strip_path "$TEST_PATH" "/bin"` [ "$STRIPPED_PATH" = "$TEST_PATH" ] || die "Stripping does not preserve colons: $STRIPPED_PATH " + +for NVM_DIR in '/tmp/nvm\test' '/tmp/nvm\new' '/tmp/nvm\\dir'; do + TEST_PATH="$NVM_DIR/v0.10.5/bin:/usr/bin:$NVM_DIR/versions/node/v22.0.0/bin:$NVM_DIR/versions/io.js/v1.0.0/bin:$NVM_DIR-other/v22.0.0/bin:/usr/local/bin" + STRIPPED_PATH=$(nvm_strip_path "$TEST_PATH" '/bin') + [ "$STRIPPED_PATH" = "/usr/bin:$NVM_DIR-other/v22.0.0/bin:/usr/local/bin" ] || die "Not correctly stripped with literal backslashes: $STRIPPED_PATH" +done