[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 <noreply@anthropic.com>
Signed-off-by: thejesh23 <35212698+thejesh23@users.noreply.github.com>
This commit is contained in:
thejesh23 authored and Jordan Harband committed 2026-11-22 08:26:23 -08:00
1 parent 0a6e489837
commit adf35d7697
2 files changed
+36

No files matched your search

+1
View File
@@ -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]*#/ {
@@ -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