Skip to content

Commit 9e884b8

Browse files
deepakchethanljharb
authored andcommitted
[Fix] nvm install: show proper version in .nvmrc install instructions
1 parent 7d86701 commit 9e884b8

File tree

5 files changed

+71
-10
lines changed

5 files changed

+71
-10
lines changed

nvm.sh

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ nvm_version_path() {
506506
nvm_ensure_version_installed() {
507507
local PROVIDED_VERSION
508508
PROVIDED_VERSION="${1-}"
509+
local IS_VERSION_FROM_NVMRC
510+
IS_VERSION_FROM_NVMRC="${2-}"
509511
if [ "${PROVIDED_VERSION}" = 'system' ]; then
510512
if nvm_has_system_iojs || nvm_has_system_node; then
511513
return 0
@@ -527,7 +529,11 @@ nvm_ensure_version_installed() {
527529
nvm_err "N/A: version \"${PREFIXED_VERSION:-$PROVIDED_VERSION}\" is not yet installed."
528530
fi
529531
nvm_err ""
530-
nvm_err "You need to run \"nvm install ${PROVIDED_VERSION}\" to install it before using it."
532+
if [ "${IS_VERSION_FROM_NVMRC}" != '1' ]; then
533+
nvm_err "You need to run \`nvm install ${PROVIDED_VERSION}\` to install and use it."
534+
else
535+
nvm_err 'You need to run `nvm install` to install and use the node version specified in `.nvmrc`.'
536+
fi
531537
return 1
532538
fi
533539
}
@@ -3522,6 +3528,8 @@ nvm() {
35223528
local NVM_DELETE_PREFIX
35233529
NVM_DELETE_PREFIX=0
35243530
local NVM_LTS
3531+
local IS_VERSION_FROM_NVMRC
3532+
IS_VERSION_FROM_NVMRC=0
35253533

35263534
while [ $# -ne 0 ]; do
35273535
case "$1" in
@@ -3549,6 +3557,7 @@ nvm() {
35493557
NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version
35503558
if [ -n "${NVM_RC_VERSION-}" ]; then
35513559
PROVIDED_VERSION="${NVM_RC_VERSION}"
3560+
IS_VERSION_FROM_NVMRC=1
35523561
VERSION="$(nvm_version "${PROVIDED_VERSION}")"
35533562
fi
35543563
unset NVM_RC_VERSION
@@ -3588,14 +3597,12 @@ nvm() {
35883597
fi
35893598
if [ "${VERSION}" = 'N/A' ]; then
35903599
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
3591-
nvm_err "N/A: version \"${PROVIDED_VERSION} -> ${VERSION}\" is not yet installed."
3592-
nvm_err ""
3593-
nvm_err "You need to run \"nvm install ${PROVIDED_VERSION}\" to install it before using it."
3600+
nvm_ensure_version_installed "${PROVIDED_VERSION}" "${IS_VERSION_FROM_NVMRC}"
35943601
fi
35953602
return 3
35963603
# This nvm_ensure_version_installed call can be a performance bottleneck
35973604
# on shell startup. Perhaps we can optimize it away or make it faster.
3598-
elif ! nvm_ensure_version_installed "${VERSION}"; then
3605+
elif ! nvm_ensure_version_installed "${VERSION}" "${IS_VERSION_FROM_NVMRC}"; then
35993606
return $?
36003607
fi
36013608

@@ -3650,6 +3657,8 @@ nvm() {
36503657
local provided_version
36513658
local has_checked_nvmrc
36523659
has_checked_nvmrc=0
3660+
local IS_VERSION_FROM_NVMRC
3661+
IS_VERSION_FROM_NVMRC=0
36533662
# run given version of node
36543663

36553664
local NVM_SILENT
@@ -3695,6 +3704,8 @@ nvm() {
36953704
if [ $has_checked_nvmrc -ne 1 ]; then
36963705
NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version && has_checked_nvmrc=1
36973706
fi
3707+
provided_version="${NVM_RC_VERSION}"
3708+
IS_VERSION_FROM_NVMRC=1
36983709
VERSION="$(nvm_version "${NVM_RC_VERSION}")" ||:
36993710
unset NVM_RC_VERSION
37003711
else
@@ -3717,7 +3728,7 @@ nvm() {
37173728
VERSION=''
37183729
fi
37193730
if [ "_${VERSION}" = "_N/A" ]; then
3720-
nvm_ensure_version_installed "${provided_version}"
3731+
nvm_ensure_version_installed "${provided_version}" "${IS_VERSION_FROM_NVMRC}"
37213732
elif [ "${NVM_IOJS}" = true ]; then
37223733
nvm exec "${NVM_SILENT_ARG-}" "${LTS_ARG-}" "${VERSION}" iojs "$@"
37233734
else

test/fast/Unit tests/nvm_ensure_version_installed

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ OUTPUT="$(nvm_ensure_version_installed foo 2>&1)"
1515
EXIT_CODE=$?
1616
EXPECTED_OUTPUT='N/A: version "foo" is not yet installed.
1717
18-
You need to run "nvm install foo" to install it before using it.'
18+
You need to run `nvm install foo` to install and use it.'
19+
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "expected 'nvm_ensure_version_installed foo' to give $EXPECTED_OUTPUT, got $OUTPUT"
20+
[ "_$EXIT_CODE" = "_1" ] || die "expected 'nvm_ensure_version_installed foo' to exit with 1, got $EXIT_CODE"
21+
22+
# Case when .nvmrc file is opened, we should be skip showing the version
23+
OUTPUT="$(nvm_ensure_version_installed foo 1 2>&1)"
24+
EXIT_CODE=$?
25+
EXPECTED_OUTPUT='N/A: version "foo" is not yet installed.
26+
27+
You need to run `nvm install` to install and use the node version specified in `.nvmrc`.'
1928
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "expected 'nvm_ensure_version_installed foo' to give $EXPECTED_OUTPUT, got $OUTPUT"
2029
[ "_$EXIT_CODE" = "_1" ] || die "expected 'nvm_ensure_version_installed foo' to exit with 1, got $EXIT_CODE"
2130

@@ -29,7 +38,7 @@ OUTPUT="$(nvm_ensure_version_installed iojs 2>&1)"
2938
EXIT_CODE=$?
3039
EXPECTED_OUTPUT='N/A: version "iojs" is not yet installed.
3140
32-
You need to run "nvm install iojs" to install it before using it.'
41+
You need to run `nvm install iojs` to install and use it.'
3342
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "expected 'nvm_ensure_version_installed iojs' to give $EXPECTED_OUTPUT, got $OUTPUT"
3443
[ "_$EXIT_CODE" = "_1" ] || die "expected 'nvm_ensure_version_installed iojs' to exit with 1, got $EXIT_CODE"
3544

test/slow/nvm run/Running 'nvm run 0.x' should error out sensibly when 0.x is not installed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ die () { echo "$@" ; exit 1; }
77

88
EXPECTED_OUTPUT='N/A: version "v0.2" is not yet installed.
99
10-
You need to run "nvm install 0.2" to install it before using it.'
10+
You need to run `nvm install 0.2` to install and use it.'
1111
[ "_$(nvm run 0.2 --version 2>&1)" = "_$EXPECTED_OUTPUT" ] || die "\`nvm run\` with an uninstalled node version failed to error out correctly"
1212

1313
EXPECTED_OUTPUT='N/A: version "iojs-v0.2" is not yet installed.
1414
15-
You need to run "nvm install iojs-0.2" to install it before using it.'
15+
You need to run `nvm install iojs-0.2` to install and use it.'
1616
[ "_$(nvm run iojs-0.2 --version 2>&1)" = "_$EXPECTED_OUTPUT" ] || die "\`nvm run\` with an uninstalled iojs version failed to error out correctly"

test/slow/nvm run/Running 'nvm run' should pick up .nvmrc version

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,16 @@ echo "0.10.7" > .nvmrc
1010
[ "$(nvm run --version | tail -1)" = "v0.10.7" ] || die "\`nvm run\` failed to run with the .nvmrc version"
1111

1212
[ "$(nvm run --version | head -1)" = "Found '$PWD/.nvmrc' with version <0.10.7>" ] || die "\`nvm run\` failed to print out the \"found in .nvmrc\" message"
13+
14+
15+
echo "foo" > .nvmrc
16+
17+
# running nvm run with .nvmrc should not print the version information when not installed
18+
OUTPUT="$(nvm run --version 2>&1)"
19+
EXIT_CODE=$?
20+
EXPECTED_OUTPUT="Found '$PWD/.nvmrc' with version <foo>
21+
N/A: version \"foo\" is not yet installed.
22+
23+
You need to run \`nvm install\` to install and use the node version specified in \`.nvmrc\`."
24+
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "expected 'nvm use' with nvmrc to give $EXPECTED_OUTPUT, got $OUTPUT"
25+
[ "_$EXIT_CODE" = "_1" ] || die "expected 'nvm use' with nvmrc to exit with 1, got $EXIT_CODE"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/sh
2+
3+
die () { echo "$@" ; exit 1; }
4+
5+
\. ../../../nvm.sh
6+
7+
nvm deactivate 2>&1 >/dev/null || die 'deactivate failed'
8+
9+
echo "foo" > .nvmrc
10+
11+
# running nvm use with .nvmrc should not print the version information
12+
OUTPUT="$(nvm use 2>&1)"
13+
EXIT_CODE=$?
14+
EXPECTED_OUTPUT="Found '$PWD/.nvmrc' with version <foo>
15+
N/A: version \"foo\" is not yet installed.
16+
17+
You need to run \`nvm install\` to install and use the node version specified in \`.nvmrc\`."
18+
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "expected 'nvm use' with nvmrc to give $EXPECTED_OUTPUT, got $OUTPUT"
19+
[ "_$EXIT_CODE" = "_3" ] || die "expected 'nvm use' with nvmrc to exit with 3, got $EXIT_CODE"
20+
21+
# --silent should not print anything
22+
OUTPUT=$(nvm use --silent)
23+
EXPECTED_OUTPUT=""
24+
25+
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \
26+
|| die "'nvm use --silent' output was not silenced to '$EXPECTED_OUTPUT'; got '$OUTPUT'"
27+
28+
rm .nvmrc

0 commit comments

Comments
 (0)