Skip to content

Should nvm_auto not prefer current and .nvmrc versions of node to default alias? #2053

Closed

Description

Currently, sourcing $NVM_DIR/nvm.sh after running nvm use will change the current version of node to the default alias, if set.
Sourcing $NVM_DIR/nvm.sh in a directory containing an .nvmrc will set the default alias, which I thought was surprising, since it will select the .nvmrc version if there's no default alias set.

The relevant code is:

#nvm.sh (lines 3539-3562)
nvm_auto() {
  nvm_echo "debug: nvm_auto"
  local NVM_MODE
  NVM_MODE="${1-}"
  local VERSION
  if [ "_$NVM_MODE" = '_install' ]; then
    VERSION="$(nvm_alias default 2>/dev/null || nvm_echo)"
    if [ -n "$VERSION" ]; then
      nvm install "$VERSION" >/dev/null
    elif nvm_rc_version >/dev/null 2>&1; then
      nvm install >/dev/null
    fi
  elif [ "_$NVM_MODE" = '_use' ]; then
    VERSION="$(nvm_resolve_local_alias default 2>/dev/null || nvm_echo)"
    if [ -n "$VERSION" ]; then
      nvm use --silent "$VERSION" >/dev/null
    elif nvm_rc_version >/dev/null 2>&1; then
      nvm use --silent >/dev/null
    fi
  elif [ "_$NVM_MODE" != '_none' ]; then
    nvm_err 'Invalid auto mode supplied.'
    return 1
  fi
}

I would expect the auto mode to prefer the current version (if any), then .nvmrc version to the default alias:

 nvm_auto() {
   local NVM_MODE
   NVM_MODE="${1-}"
-  local VERSION
+  local DEFAULT
+  local NVM_CURRENT
   if [ "_$NVM_MODE" = '_install' ]; then
-    VERSION="$(nvm_alias default 2>/dev/null || nvm_echo)"
-    if [ -n "$VERSION" ]; then
-      nvm install "$VERSION" >/dev/null
+    NVM_CURRENT="$(nvm_ls_current 2>/dev/null || nvm_echo)"
+    DEFAULT="$(nvm_alias default 2>/dev/null || nvm_echo)"
+    if [ "_$NVM_CURRENT" != "_$DEFAULT" ] && [ "_$NVM_CURRENT" != "_system" ]; then
+      nvm install "$NVM_CURRENT" >/dev/null
     elif nvm_rc_version >/dev/null 2>&1; then
       nvm install >/dev/null
+    elif [ -n "$DEFAULT" ]; then
+        nvm install "$DEFAULT" >/dev/null
     fi
   elif [ "_$NVM_MODE" = '_use' ]; then
-    VERSION="$(nvm_resolve_local_alias default 2>/dev/null || nvm_echo)"
-    if [ -n "$VERSION" ]; then
-      nvm use --silent "$VERSION" >/dev/null
+    NVM_CURRENT="$(nvm_ls_current 2>/dev/null || nvm_echo)"
+    DEFAULT="$(nvm_resolve_local_alias default 2>/dev/null || nvm_echo)"
+    if [ "_$NVM_CURRENT" != "_$DEFAULT" ] && [ "_$NVM_CURRENT" != "_system" ]; then
+      nvm use --silent "$NVM_CURRENT" >/dev/null
     elif nvm_rc_version >/dev/null 2>&1; then
       nvm use --silent >/dev/null
+    elif [ -n "$DEFAULT" ]; then
+      nvm use --silent "$DEFAULT" >/dev/null
     fi
   elif [ "_$NVM_MODE" != '_none' ]; then
     nvm_err 'Invalid auto mode supplied.'

This way, re-sourcing nvm will prefer the current version (if not system) or the .nvmrc version, or the default alias version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions