From adf35d7697ed8fbaf172a6babbb771d538a87d96 Mon Sep 17 00:00:00 2001 From: thejesh23 <35212698+thejesh23@users.noreply.github.com> Date: Sun, 22 Nov 2026 08:26:23 -0800 Subject: [PATCH] [Fix] `nvm_get_default_packages`: ignore leading and trailing blanks A trailing space (or tab) left by an editor, or an indented line, in `$NVM_DIR/default-packages` tripped the "multiple space-separated values" check, so the whole file was rejected and no default packages were installed after `nvm install`, with an error naming the wrong problem. Trim blanks around each line before checking it, as is already done for a trailing `\r`; a line with two values is still rejected. Assisted-by: Claude Code Signed-off-by: thejesh23 <35212698+thejesh23@users.noreply.github.com> --- nvm.sh | 1 + ...efault_packages ignores surrounding blanks | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100755 test/fast/Unit tests/nvm_get_default_packages ignores surrounding blanks diff --git a/nvm.sh b/nvm.sh index 8d253bbe..31867252 100755 --- a/nvm.sh +++ b/nvm.sh @@ -7219,6 +7219,7 @@ nvm_get_default_packages() { if [ -f "${NVM_DEFAULT_PACKAGE_FILE}" ]; then command awk ' { sub(/\r$/, "") } # Strip CRLF line endings + { sub(/^[ \t]+/, ""); sub(/[ \t]+$/, "") } # Strip leading and trailing blanks /^[ \t]*#/ { next } # Skip lines that begin with # /^[ \t]*$/ { next } # Skip empty lines /[ \t]/ && !/^[ \t]*#/ { diff --git a/test/fast/Unit tests/nvm_get_default_packages ignores surrounding blanks b/test/fast/Unit tests/nvm_get_default_packages ignores surrounding blanks new file mode 100755 index 00000000..f64fceaf --- /dev/null +++ b/test/fast/Unit tests/nvm_get_default_packages ignores surrounding blanks @@ -0,0 +1,35 @@ +#!/bin/sh + +die () { echo "$@" ; cleanup ; exit 1; } +cleanup () { + rm -rf "${NVM_DIR}" + NVM_DIR="${ORIG_NVM_DIR}" +} + +: nvm.sh +\. ../../../nvm.sh + +ORIG_NVM_DIR="${NVM_DIR}" +NVM_DIR="$(mktemp -d)" +FILE="${NVM_DIR}/default-packages" +mkdir "${NVM_DIR}/bin" + +printf 'rimraf \nobject-inspect@1.0.2\t\r\n stevemao/left-pad\n\t# commented-package\n \n' > "${FILE}" +EXPECTED_PKGS='rimraf object-inspect@1.0.2 stevemao/left-pad' + +# nvm runs `command awk`, so a PATH entry named `awk` selects the implementation +for AWK_IMPL in awk mawk gawk; do + AWK_PATH="$(command -v "${AWK_IMPL}")" || continue + ln -sf "${AWK_PATH}" "${NVM_DIR}/bin/awk" + + DEFAULT_PKGS="$(PATH="${NVM_DIR}/bin:${PATH}" nvm_get_default_packages)" + [ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "${AWK_IMPL}: expected >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<" +done + +# blanks around a line with two values do not hide the second value +printf 'rimraf\n object-inspect @ 1.0.2 \n' > "${FILE}" +ERROR="$(nvm_get_default_packages 2>&1 >/dev/null)" && die 'expected a line with two values to be rejected' +EXPECTED_ERROR="Only one package per line is allowed in \`${FILE}\`. Please remove any lines with multiple space-separated values." +[ "${ERROR}" = "${EXPECTED_ERROR}" ] || die "expected >${EXPECTED_ERROR}<; got >${ERROR}<" + +cleanup