Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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

authored by Jordan Harband · committed by Jordan Harband · parent 7079a5d · Browse files · 2 files+88 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+88 added−0 removed

  • tests:+63 added−0 removed
  • other:+25 added−0 removed

2 files+88 added−0 removed

added:test/fast/Running 'nvm exec' and 'nvm run' without a resolvable version warn and fall back+63 added
1#!/bin/sh23set -ex45die () { echo "$@" ; cleanup ; exit 1; }67cleanup() {8 cd "${ORIG_PWD}" 2>/dev/null || true9 [ -n "${TMP_DIR-}" ] && rm -rf "${TMP_DIR}"10}1112export NVM_DIR="$(cd ../.. && pwd)"1314: nvm.sh15\. ../../nvm.sh1617\. ../common.sh1819ORIG_PWD="$(pwd)"2021# Run from a fresh, empty directory so no ambient .nvmrc above the test dir22# can satisfy the lookup and mask the warning.23TMP_DIR="$(mktemp -d)"24cd "${TMP_DIR}" || die "could not cd to temp dir"2526EXEC_WARNING='WARNING: `nvm exec` was invoked without a version argument and without an .nvmrc file.'27RUN_WARNING='WARNING: `nvm run` was invoked without a version argument and without an .nvmrc file.'2829# `nvm exec` with no version and no .nvmrc should warn on stderr (and fall back).30set +ex # needed for stderr31EXEC_STDERR="$(nvm exec </dev/null 2>&1 1>/dev/null)"32set -ex33case "${EXEC_STDERR}" in34 *"${EXEC_WARNING}"*) ;;35 *) die "'nvm exec' with no version did not warn; got >${EXEC_STDERR}<" ;;36esac3738# `--silent` should suppress the warning.39set +ex # needed for stderr40EXEC_SILENT_STDERR="$(nvm exec --silent </dev/null 2>&1 1>/dev/null)"41set -ex42case "${EXEC_SILENT_STDERR}" in43 *WARNING*) die "'nvm exec --silent' should not warn; got >${EXEC_SILENT_STDERR}<" ;;44esac4546# `nvm run` with an unresolvable version and no .nvmrc should warn (and fall back).47set +ex # needed for stderr48RUN_STDERR="$(nvm run bogusversion </dev/null 2>&1 1>/dev/null)"49set -ex50case "${RUN_STDERR}" in51 *"${RUN_WARNING}"*) ;;52 *) die "'nvm run' with no resolvable version did not warn; got >${RUN_STDERR}<" ;;53esac5455# `--silent` should suppress the warning.56set +ex # needed for stderr57RUN_SILENT_STDERR="$(nvm run --silent bogusversion </dev/null 2>&1 1>/dev/null)"58set -ex59case "${RUN_SILENT_STDERR}" in60 *WARNING*) die "'nvm run --silent' should not warn; got >${RUN_SILENT_STDERR}<" ;;61esac6263cleanup
modified:nvm.sh+25 added
4177 unmodified lines
4178 provided_version=''4179 if [ $has_checked_nvmrc -ne 1 ]; then4180 { NVM_RC_VERSION="$(NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version 3>&1 1>&4)"; } 4>&1 && has_checked_nvmrc=14181 fi4182 if [ -z "${NVM_RC_VERSION-}" ]; then4183 if [ "${NVM_SILENT:-0}" -ne 1 ]; then4184 nvm_err 'WARNING: `nvm run` was invoked without a version argument and without an .nvmrc file.'4185 nvm_err ' Falling back to the active node version; this will become an error in a future release.'4186 nvm_err ' Pass `current` explicitly (e.g. `nvm run current ...`) to silence this warning.'4187 fi4188 NVM_RC_VERSION="$(nvm_version current)" ||:4189 fi4190 provided_version="${NVM_RC_VERSION}"4191 IS_VERSION_FROM_NVMRC=14192 VERSION="$(nvm_version "${NVM_RC_VERSION}")" ||:4193 else
49 unmodified lines
4243 done42444245 local provided_version4246 provided_version="$1"4247 local VERSION_SOURCE4248 VERSION_SOURCE=''4249 if [ "${NVM_LTS-}" != '' ]; then4250 provided_version="lts/${NVM_LTS:-*}"4251 VERSION="${provided_version}"4252 VERSION_SOURCE='lts'4253 elif [ -n "${provided_version}" ]; then4254 VERSION="$(nvm_version "${provided_version}")" ||:4255 if [ "_${VERSION}" = '_N/A' ] && ! nvm_is_valid_version "${provided_version}"; then4256 { provided_version="$(NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version 3>&1 1>&4)"; } 4>&1 && has_checked_nvmrc=14257 VERSION="$(nvm_version "${provided_version}")" ||:4258 if [ -n "${provided_version}" ]; then4259 VERSION_SOURCE='nvmrc'4260 fi4261 else4262 VERSION_SOURCE='arg'4263 shift4264 fi4265 fi42664267 if [ -z "${VERSION_SOURCE}" ]; then4268 if [ "${NVM_SILENT:-0}" -ne 1 ]; then4269 nvm_err 'WARNING: `nvm exec` was invoked without a version argument and without an .nvmrc file.'4270 nvm_err ' Falling back to the active node version; this will become an error in a future release.'4271 nvm_err ' Pass `current` explicitly (e.g. `nvm exec current ...`) to silence this warning.'4272 fi4273 provided_version='current'4274 VERSION="$(nvm_version current)" ||:4275 fi42764277 nvm_ensure_version_installed "${provided_version}"4278 EXIT_CODE=$?4279 if [ "${EXIT_CODE}" != "0" ]; then4280 # shellcheck disable=SC2086
591 unmodified lines