From 9ac2cbb95f4bd7a978903a9032d1867cb9347e56 Mon Sep 17 00:00:00 2001 From: Amiraslan Muradov <70092128+aslanmuradov@users.noreply.github.com> Date: Wed, 2 Dec 2026 01:41:30 +0300 Subject: [PATCH] [Fix] Ignore a UTF-8 BOM in default-packages Co-authored-by: Amiraslan Muradov <70092128+aslanmuradov@users.noreply.github.com> Co-authored-by: Jordan Harband --- nvm.sh | 3 +- ...vm_get_default_packages strips a UTF-8 BOM | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100755 test/fast/Unit tests/nvm_get_default_packages strips a UTF-8 BOM diff --git a/nvm.sh b/nvm.sh index 4ee66afb..233be703 100755 --- a/nvm.sh +++ b/nvm.sh @@ -7529,7 +7529,8 @@ nvm_get_default_packages() { local NVM_DEFAULT_PACKAGE_FILE NVM_DEFAULT_PACKAGE_FILE="${NVM_DIR}/default-packages" if [ -f "${NVM_DEFAULT_PACKAGE_FILE}" ]; then - command awk ' + LC_ALL=C command awk ' + NR == 1 { sub(/^\357\273\277/, "") } # Strip a UTF-8 BOM at the start of the file { sub(/\r$/, "") } # Strip CRLF line endings { sub(/^[ \t]+/, ""); sub(/[ \t]+$/, "") } # Strip leading and trailing blanks /^[ \t]*#/ { next } # Skip lines that begin with # diff --git a/test/fast/Unit tests/nvm_get_default_packages strips a UTF-8 BOM b/test/fast/Unit tests/nvm_get_default_packages strips a UTF-8 BOM new file mode 100755 index 00000000..ea7831bd --- /dev/null +++ b/test/fast/Unit tests/nvm_get_default_packages strips a UTF-8 BOM @@ -0,0 +1,51 @@ +#!/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" + +# the leading space after a skipped first line is not what's under test +expect_packages () { + for AWK_IMPL in awk mawk gawk; do + AWK_PATH="$(command -v "${AWK_IMPL}")" || continue + ln -sf "${AWK_PATH}" "${NVM_DIR}/bin/awk" + for LOCALE in C en_US.UTF-8 C.UTF-8; do + DEFAULT_PKGS="$(LC_ALL="${LOCALE}" PATH="${NVM_DIR}/bin:${PATH}" nvm_get_default_packages)" \ + || die "${1} (${AWK_IMPL}, ${LOCALE}): failed" + DEFAULT_PKGS="${DEFAULT_PKGS# }" + [ "${DEFAULT_PKGS}" = "${2}" ] || die "${1} (${AWK_IMPL}, ${LOCALE}): expected >${2}<; got >${DEFAULT_PKGS}<" + done + done +} + +printf '\357\273\277rimraf\r\nobject-inspect@1.0.2\r\n' > "${FILE}" +expect_packages 'BOM with CRLF line endings' 'rimraf object-inspect@1.0.2' + +printf '\357\273\277# commented-package\nrimraf\n' > "${FILE}" +expect_packages 'BOM before a comment' 'rimraf' + +printf '\357\273\277 rimraf \n' > "${FILE}" +expect_packages 'BOM before blanks' 'rimraf' + +printf '\357\273\277\nrimraf\n' > "${FILE}" +expect_packages 'BOM alone on the first line' 'rimraf' + +printf 'rimraf\n\357\273\277mkdirp\n' > "${FILE}" +expect_packages 'BOM after the first line' "rimraf $(printf '\357\273\277')mkdirp" + +printf '\357\273\277rimraf mkdirp\n' > "${FILE}" +ERROR="$(nvm_get_default_packages 2>&1 >/dev/null)" && die 'expected a line with two values after a BOM 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