[Fix] nvm exec/nvm run: warn when no version and no .nvmrc

Currently these commands silently fall back to the active node version when neither a version argument nor an `.nvmrc` resolves,
making them invisibly dependent on shell state and impossible to script predictably (see #3755).

Print a stderr deprecation warning in this case (suppressed by `--silent`) and continue with the active node version,
so existing callers keep working.
The follow-up change will turn this into a hard error;
pass `current` explicitly (e.g. `nvm exec current node ...`) to silence the warning and lock in the new behavior now.

Refs #3755
This commit is contained in:
Jordan Harband
2026-05-05 18:10:53 -07:00
parent 7079a5d61c
commit a9933f77a6
2 changed files with 88 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
#!/bin/sh
set -ex
die () { echo "$@" ; cleanup ; exit 1; }
cleanup() {
cd "${ORIG_PWD}" 2>/dev/null || true
[ -n "${TMP_DIR-}" ] && rm -rf "${TMP_DIR}"
}
export NVM_DIR="$(cd ../.. && pwd)"
: nvm.sh
\. ../../nvm.sh
\. ../common.sh
ORIG_PWD="$(pwd)"
# Run from a fresh, empty directory so no ambient .nvmrc above the test dir
# can satisfy the lookup and mask the warning.
TMP_DIR="$(mktemp -d)"
cd "${TMP_DIR}" || die "could not cd to temp dir"
EXEC_WARNING='WARNING: `nvm exec` was invoked without a version argument and without an .nvmrc file.'
RUN_WARNING='WARNING: `nvm run` was invoked without a version argument and without an .nvmrc file.'
# `nvm exec` with no version and no .nvmrc should warn on stderr (and fall back).
set +ex # needed for stderr
EXEC_STDERR="$(nvm exec </dev/null 2>&1 1>/dev/null)"
set -ex
case "${EXEC_STDERR}" in
*"${EXEC_WARNING}"*) ;;
*) die "'nvm exec' with no version did not warn; got >${EXEC_STDERR}<" ;;
esac
# `--silent` should suppress the warning.
set +ex # needed for stderr
EXEC_SILENT_STDERR="$(nvm exec --silent </dev/null 2>&1 1>/dev/null)"
set -ex
case "${EXEC_SILENT_STDERR}" in
*WARNING*) die "'nvm exec --silent' should not warn; got >${EXEC_SILENT_STDERR}<" ;;
esac
# `nvm run` with an unresolvable version and no .nvmrc should warn (and fall back).
set +ex # needed for stderr
RUN_STDERR="$(nvm run bogusversion </dev/null 2>&1 1>/dev/null)"
set -ex
case "${RUN_STDERR}" in
*"${RUN_WARNING}"*) ;;
*) die "'nvm run' with no resolvable version did not warn; got >${RUN_STDERR}<" ;;
esac
# `--silent` should suppress the warning.
set +ex # needed for stderr
RUN_SILENT_STDERR="$(nvm run --silent bogusversion </dev/null 2>&1 1>/dev/null)"
set -ex
case "${RUN_SILENT_STDERR}" in
*WARNING*) die "'nvm run --silent' should not warn; got >${RUN_SILENT_STDERR}<" ;;
esac
cleanup