[Fix] nvm debug: don't let the shell read stdin when asked for its version

busybox's shells have no `--version`, and start an interactive shell instead,
so with `$SHELL` set to busybox `sh` on a terminal, `nvm debug` waited for input forever,
which hung the alpine `sh` CI lanes in the "mirror URL credentials are masked in output" test.
Like the other busybox tools, its version now comes from `--help`,
and every shell is given no stdin to read.
This commit is contained in:
Jordan Harband committed 2026-10-25 11:42:45 -07:00
1 parent e780535240
commit 898124fd60
2 files changed
+66 -1

No files matched your search

+8 -1
View File
@@ -4268,7 +4268,14 @@ nvm() {
nvm_err "\${NPM_CONFIG_PREFIX}: '$(nvm_sanitize_path "${NPM_CONFIG_PREFIX-}")'"
nvm_err "\$NVM_NODEJS_ORG_MIRROR: '$(nvm_sanitize_url "${NVM_NODEJS_ORG_MIRROR-}")'"
nvm_err "\$NVM_IOJS_ORG_MIRROR: '$(nvm_sanitize_url "${NVM_IOJS_ORG_MIRROR-}")'"
nvm_err "shell version: '$(${SHELL} --version | command head -n 1)'"
local NVM_SHELL_VERSION
# busybox's shells have no `--version`, and start an interactive shell instead
if command ls -l "$(command -v "${SHELL}" 2>/dev/null)" 2>/dev/null | nvm_grep -q busybox; then
NVM_SHELL_VERSION="$(${SHELL} --help 2>&1 </dev/null | command head -n 1)"
else
NVM_SHELL_VERSION="$(${SHELL} --version </dev/null | command head -n 1)"
fi
nvm_err "shell version: '${NVM_SHELL_VERSION}'"
nvm_err "uname -a: '$(command uname -a | command awk '{$2=""; print}' | command xargs)'"
nvm_err "checksum binary: '$(nvm_get_checksum_binary 2>/dev/null)'"
if [ "$(nvm_get_os)" = "darwin" ] && nvm_has sw_vers; then
@@ -0,0 +1,58 @@
#!/bin/sh
TMP_DIR=''
cleanup () {
case "${TMP_DIR}" in
/*) rm -rf "${TMP_DIR}" ;;
esac
}
die () { echo "$@" ; cleanup ; exit 1; }
: nvm.sh
\. ../../../nvm.sh
TMP_DIR="$(mktemp -d)"
# like busybox's `sh`, which starts an interactive shell for `--version`
cat > "${TMP_DIR}/busybox" <<'FAKE'
#!/bin/sh
if [ "$1" = '--help' ]; then
echo 'BusyBox v9.9.9 (fake) multi-call binary.'
elif read -r LINE; then
echo 'interactive shell read stdin'
fi
FAKE
chmod +x "${TMP_DIR}/busybox" || die 'could not make the fake busybox'
ln -s "${TMP_DIR}/busybox" "${TMP_DIR}/sh" || die 'could not link the fake busybox sh'
cat > "${TMP_DIR}/fakesh" <<'FAKE'
#!/bin/sh
if read -r LINE; then
echo 'fake shell read stdin'
else
echo 'fake shell 1.0'
fi
FAKE
chmod +x "${TMP_DIR}/fakesh" || die 'could not make the fake shell'
OUTPUT="$(SHELL="${TMP_DIR}/sh" nvm debug 2>&1 <<STDIN
a line a shell must not read
STDIN
)"
case "${OUTPUT}" in
*"shell version: 'BusyBox v9.9.9 (fake) multi-call binary.'"*) ;;
*) die "nvm debug should get a busybox shell's version from --help; got >${OUTPUT}<" ;;
esac
OUTPUT="$(SHELL="${TMP_DIR}/fakesh" nvm debug 2>&1 <<STDIN
a line a shell must not read
STDIN
)"
case "${OUTPUT}" in
*"shell version: 'fake shell 1.0'"*) ;;
*) die "nvm debug should not let the shell read stdin for its version; got >${OUTPUT}<" ;;
esac
cleanup