[Fix] nvm_rc_version: clarify error when no version and no .nvmrc

Previously the message read "No .nvmrc file found",
which obscured the fact that the user also did not pass a version.
The new wording names both halves of the actual problem.

Refs #3755
This commit is contained in:
Jordan Harband
2026-05-05 16:20:22 -07:00
parent a9933f77a6
commit ed4dbdfdd5
2 changed files with 40 additions and 1 deletions

2
nvm.sh
View File

@@ -622,7 +622,7 @@ nvm_rc_version() {
NVMRC_PATH="$(nvm_find_nvmrc)"
if [ ! -e "${NVMRC_PATH}" ]; then
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
nvm_err "No .nvmrc file found"
nvm_err "No version provided and no .nvmrc file found"
fi
return 1
fi

View File

@@ -0,0 +1,39 @@
#!/bin/sh
die () { echo "$@" ; cleanup ; exit 1; }
cleanup() {
cd "${ORIG_PWD}" 2>/dev/null || true
[ -n "${TMP_DIR-}" ] && rm -rf "${TMP_DIR}"
}
: nvm.sh
\. ../../../nvm.sh
\. ../../common.sh
ORIG_PWD="$(pwd)"
# Run from a fresh, empty directory so no ambient .nvmrc above the test dir
# is found by the upward lookup.
TMP_DIR="$(mktemp -d)"
cd "${TMP_DIR}" || die "could not cd to temp dir"
# The message must name both halves of the problem: no argument AND no .nvmrc.
try_err nvm_rc_version
EXPECTED='No version provided and no .nvmrc file found'
[ "_${CAPTURED_STDERR}" = "_${EXPECTED}" ] \
|| die "nvm_rc_version did not print >${EXPECTED}<; got >${CAPTURED_STDERR}<"
[ "_${CAPTURED_EXIT_CODE}" = "_1" ] \
|| die "nvm_rc_version expected exit code 1; got ${CAPTURED_EXIT_CODE}"
# NVM_SILENT suppresses the message but the call still fails.
export NVM_SILENT=1
try_err nvm_rc_version
unset NVM_SILENT
[ -z "${CAPTURED_STDERR}" ] \
|| die "NVM_SILENT nvm_rc_version should be silent; got >${CAPTURED_STDERR}<"
[ "_${CAPTURED_EXIT_CODE}" = "_1" ] \
|| die "NVM_SILENT nvm_rc_version expected exit code 1; got ${CAPTURED_EXIT_CODE}"
cleanup