From c937d62f56b02dc7ffcd96c0a1297b446b7ad260 Mon Sep 17 00:00:00 2001 From: Oliver Henshaw Date: Wed, 21 Jun 2023 15:47:52 +0100 Subject: [PATCH] [Fix] nvm_strip_path: Preserve leading/trailing colons Path lists in environmental variables often give special meaning to empty entries (e.g. in PATH or MANPATH). These are represented by leading or trailing colons, or by doubled colons in the middle of the list. Adjust the awk invocation to correctly deal with trailing colons by printing the separator before every field except the first, and then printing the final separator that is read from the input - this will either be a colon or the null string. This preserves leading and trailing colons in all cases while not adding extra colons in the wrong place. Add test to confirm the correct behaviour. Fixes #3144 --- 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 d8694064a68..7de650c1a8d 100644 --- a/nvm.sh +++ b/nvm.sh @@ -802,7 +802,8 @@ nvm_strip_path() { path = substr($0, length(NVM_DIR) + 1) if (path ~ "^(/versions/[^/]*)?/[^/]*'"${2-}"'.*$") { next } } - { print }' | command paste -s -d: - + # The final RT will contain a colon if the input has a trailing colon, or a null string otherwise + { printf "%s%s", sep, $0; sep=RS } END { printf "%s", RT }' } nvm_change_path() { diff --git a/test/fast/Unit tests/nvm_strip_path b/test/fast/Unit tests/nvm_strip_path index adce734a578..6f00da0275a 100755 --- a/test/fast/Unit tests/nvm_strip_path +++ b/test/fast/Unit tests/nvm_strip_path @@ -16,3 +16,9 @@ TEST_PATH="$NVM_DIR/v0.10.5/bin:/usr/bin:$NVM_DIR/v0.11.5/bin:$NVM_DIR/v0.9.5/bi STRIPPED_PATH=`nvm_strip_path "$TEST_PATH" "/bin"` [ "$STRIPPED_PATH" = "/usr/bin:/usr/local/bin" ] || die "Not correctly stripped: $STRIPPED_PATH " + +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 "