Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[patch] give a more helpful message when lts alias is mistakenly used #3441

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions test/slow/nvm use/Running 'nvm use lts' shows actionable error
Original file line number Diff line number Diff line change
@@ -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"
Loading