[Fix] nvm_npm_global_modules: keep npm's tree drawing out of shell variables

On arm64, dash 0.5.13.5 (Alpine 3.25) corrupts some non-ASCII bytes it stores in a variable,
so no package was parsed out of `npm ls` and `--reinstall-packages-from` passed it junk.
This commit is contained in:
Jordan Harband committed 2026-12-04 10:36:29 -08:00
1 parent 885a8fad86
commit deaffdb1f1
2 files changed
+63 -1

No files matched your search

+2 -1
View File
@@ -4739,7 +4739,8 @@ nvm_npm_global_modules() {
local NPMLIST local NPMLIST
local VERSION local VERSION
VERSION="$1" VERSION="$1"
NPMLIST=$(nvm use "${VERSION}" >/dev/null && nvm_npm list -g --depth=0 2>/dev/null | command sed -e '1d' -e '/UNMET PEER DEPENDENCY/d') # npm's tree drawing becomes ASCII before reaching a variable: dash 0.5.13.5 on arm64 corrupts it
NPMLIST=$(nvm use "${VERSION}" >/dev/null && nvm_npm list -g --depth=0 2>/dev/null | command sed -e '1d' -e '/UNMET PEER DEPENDENCY/d' -e 's/^[^[:alnum:]@(]*/- /')
local INSTALLS local INSTALLS
INSTALLS=$(nvm_echo "${NPMLIST}" | command sed -e '/ -> / d' -e '/\(empty\)/ d' -e 's/^.* \(.*@[^ ]*\).*/\1/' -e '/^npm@[^ ]*.*$/ d' -e '/^corepack@[^ ]*.*$/ d' | command xargs) INSTALLS=$(nvm_echo "${NPMLIST}" | command sed -e '/ -> / d' -e '/\(empty\)/ d' -e 's/^.* \(.*@[^ ]*\).*/\1/' -e '/^npm@[^ ]*.*$/ d' -e '/^corepack@[^ ]*.*$/ d' | command xargs)
@@ -0,0 +1,61 @@
#!/bin/sh
TEST_DIR=''
cleanup () {
case "${TEST_DIR}" in
/*) rm -rf "${TEST_DIR}" ;;
esac
unset -f nvm nvm_npm nvm_echo
unset MOCK_NPM_OUTPUT
}
die () { echo "$@" ; cleanup ; exit 1; }
: nvm.sh
\. ../../../nvm.sh
\. ../../common.sh
TEST_DIR="$(mktemp -d)" || die 'could not create temp dir'
nvm() {
return 0
}
nvm_npm() {
printf '%s\n' "${MOCK_NPM_OUTPUT}"
}
# every variable the function prints passes through here
nvm_echo() {
printf '%s\n' "$*" >> "${TEST_DIR}/echoed"
printf '%s\n' "$*"
}
MOCK_NPM_OUTPUT='/fake/lib
├── @scope/pkg@1.2.3
├── lodash@4.17.21 extraneous
├── npm@10.0.0
├── linked@0.0.1 -> /home/u/linked
└── corepack@0.20.0'
try nvm_npm_global_modules v1.2.3
EXPECTED='@scope/pkg@1.2.3 lodash@4.17.21 //// /home/u/linked'
[ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "unicode tree: got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"
[ -s "${TEST_DIR}/echoed" ] || die 'nvm_echo was not called'
if LC_ALL=C command grep -n '[^[:print:]]' "${TEST_DIR}/echoed" >/dev/null; then
die "npm's tree drawing reached a variable: >$(cat "${TEST_DIR}/echoed")<"
fi
MOCK_NPM_OUTPUT='/fake/lib
+-- @scope/pkg@1.2.3
+-- lodash@4.17.21 extraneous
+-- npm@10.0.0
+-- linked@0.0.1 -> /home/u/linked
`-- corepack@0.20.0'
try nvm_npm_global_modules v1.2.3
[ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "ascii tree: got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"
cleanup