From 9877db5b5024b94d729b39383adb6a1dc91f7b23 Mon Sep 17 00:00:00 2001 From: Reetik Rajan Date: Sat, 5 Oct 2024 09:21:42 +0530 Subject: [PATCH] [patch] give a more helpful message when `lts` alias is mistakenly used --- nvm.sh | 10 ++++++---- ...nning 'nvm use lts' shows actionable error | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100755 test/slow/nvm use/Running 'nvm use lts' shows actionable error diff --git a/nvm.sh b/nvm.sh index 9eeede2ac6..d539f7e4ae 100755 --- a/nvm.sh +++ b/nvm.sh @@ -700,10 +700,12 @@ nvm_ensure_version_installed() { nvm_err "N/A: version \"${PREFIXED_VERSION:-$PROVIDED_VERSION}\" is not yet installed." fi nvm_err "" - if [ "${IS_VERSION_FROM_NVMRC}" != '1' ]; then - nvm_err "You need to run \`nvm install ${PROVIDED_VERSION}\` to install and use it." - else - nvm_err 'You need to run `nvm install` to install and use the node version specified in `.nvmrc`.' + if [ "${PROVIDED_VERSION}" = 'lts' ]; then + nvm_err "\`lts\` is not an alias - you may need to run \`nvm install --lts\` to install and \`nvm use --lts\` to use it." + elif [ "${IS_VERSION_FROM_NVMRC}" != '1' ]; then + nvm_err "You need to run \`nvm install ${PROVIDED_VERSION}\` to install and use it." + else + nvm_err 'You need to run `nvm install` to install and use the node version specified in `.nvmrc`.' fi return 1 fi diff --git a/test/slow/nvm use/Running 'nvm use lts' shows actionable error b/test/slow/nvm use/Running 'nvm use lts' shows actionable error new file mode 100755 index 0000000000..903abaca98 --- /dev/null +++ b/test/slow/nvm use/Running 'nvm use lts' shows actionable error @@ -0,0 +1,19 @@ +#!/bin/sh + +set -ex + +die () { echo "$@" ; exit 1; } + +\. ../../../nvm.sh + +# Deactivate any active node version +nvm deactivate >/dev/null 2>&1 || die 'deactivate failed' + +# Attempt to use 'lts' without '--' and capture the error message +ERROR_OUTPUT=$(nvm use lts 2>&1) || true + +EXPECTED_ERROR="`lts` is not an alias - you may need to run \`nvm install --lts\` to install and \`nvm use --lts\` to use it." + +# Check if the error message matches the expected output +echo "$ERROR_OUTPUT" | grep -q "$EXPECTED_ERROR" \ + || die "Expected error message not found. Got: $ERROR_OUTPUT"