[Fix] nvm install: migrate packages/set alias when target is already installed

When `nvm install <target>` found that `<target>` was already installed,
the post-`nvm use` steps
(`--reinstall-packages-from`, default packages, and `--alias`/`--default`)
were gated on `[ $EXIT_CODE -ne 0 ]`.
Since that branch is only entered when `nvm use` succeeded (EXIT_CODE == 0),
those conditions were always false, so the steps were silently skipped.

The fresh-install branch correctly uses `-eq 0`;
mirror that here so `--reinstall-packages-from` actually migrates global packages
(and the alias/default get set) when the target version already exists.

Includes a regression test covering the already-installed path.

Fixes #3858
This commit is contained in:
Jordan Harband
2026-06-29 16:39:19 -05:00
parent ee4fa818d2
commit ce15734300
2 changed files with 76 additions and 4 deletions
+3 -3
View File
@@ -3736,10 +3736,10 @@ nvm() {
nvm install-latest-npm
EXIT_CODE=$?
fi
if [ $EXIT_CODE -ne 0 ] && [ -z "${SKIP_DEFAULT_PACKAGES-}" ]; then
if [ $EXIT_CODE -eq 0 ] && [ -z "${SKIP_DEFAULT_PACKAGES-}" ]; then
nvm_install_default_packages
fi
if [ $EXIT_CODE -ne 0 ] && [ -n "${REINSTALL_PACKAGES_FROM-}" ] && [ "_${REINSTALL_PACKAGES_FROM}" != "_N/A" ]; then
if [ $EXIT_CODE -eq 0 ] && [ -n "${REINSTALL_PACKAGES_FROM-}" ] && [ "_${REINSTALL_PACKAGES_FROM}" != "_N/A" ]; then
nvm reinstall-packages "${REINSTALL_PACKAGES_FROM}"
EXIT_CODE=$?
fi
@@ -3757,7 +3757,7 @@ nvm() {
EXIT_CODE=$?
fi
if [ $EXIT_CODE -ne 0 ] && [ -n "${ALIAS-}" ]; then
if [ $EXIT_CODE -eq 0 ] && [ -n "${ALIAS-}" ]; then
nvm alias "${ALIAS}" "${provided_version}"
EXIT_CODE=$?
fi