From 0add6b8555fb378aab9ea6567e464d0349a4e837 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 30 Sep 2026 15:50:38 +0900 Subject: [PATCH] [Fix] `nvm`: do not change or export the caller's `IFS` `IFS=... nvm "$@"` is a prefix assignment to a function call, which bash's POSIX mode (macOS `/bin/sh`) keeps after the call returns, and exports. --- nvm.sh | 16 ++++++++++++---- ...ould not change or export the caller's IFS | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) create mode 100755 test/fast/Running 'nvm' should not change or export the caller's IFS diff --git a/nvm.sh b/nvm.sh index 8204b6d9..17d36c4f 100755 --- a/nvm.sh +++ b/nvm.sh @@ -3672,14 +3672,18 @@ nvm() { if [ "${-#*e}" != "$-" ]; then set +e local EXIT_CODE - IFS="${DEFAULT_IFS}" nvm "$@" + local IFS + IFS="${DEFAULT_IFS}" + nvm "$@" EXIT_CODE="$?" set -e return "$EXIT_CODE" elif [ "${-#*a}" != "$-" ]; then set +a local EXIT_CODE - IFS="${DEFAULT_IFS}" nvm "$@" + local IFS + IFS="${DEFAULT_IFS}" + nvm "$@" EXIT_CODE="$?" set -a return "$EXIT_CODE" @@ -3687,13 +3691,17 @@ nvm() { # shellcheck disable=SC3041 set +E local EXIT_CODE - IFS="${DEFAULT_IFS}" nvm "$@" + local IFS + IFS="${DEFAULT_IFS}" + nvm "$@" EXIT_CODE="$?" # shellcheck disable=SC3041 set -E return "$EXIT_CODE" elif [ "${IFS}" != "${DEFAULT_IFS}" ]; then - IFS="${DEFAULT_IFS}" nvm "$@" + local IFS + IFS="${DEFAULT_IFS}" + nvm "$@" return "$?" fi diff --git a/test/fast/Running 'nvm' should not change or export the caller's IFS b/test/fast/Running 'nvm' should not change or export the caller's IFS new file mode 100755 index 00000000..07a4cbb0 --- /dev/null +++ b/test/fast/Running 'nvm' should not change or export the caller's IFS @@ -0,0 +1,19 @@ +#!/bin/sh + +die () { IFS=' '; echo "$@" ; exit 1; } + +: nvm.sh +\. ../../nvm.sh + +IFS=':' +nvm --version >/dev/null +[ "${IFS}" = ':' ] || die "nvm changed the caller's IFS; got >${IFS}<" + +set -e +nvm --version >/dev/null +set +e +[ "${IFS}" = ':' ] || die "nvm changed the caller's IFS under set -e; got >${IFS}<" + +! env | grep -q '^IFS=' || die 'nvm exported IFS' + +IFS=' '