78e85c7d78edeca7f06d6a54ad7f03f10dacde62

Author
Carlo <carlosalag@protonmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

fix(nvm): source nvm script only when used (#11205)

closes #11042

Diff

 1diff --git a/plugins/nvm/README.md b/plugins/nvm/README.md
 2index a8bc34ae7970f6b7af4212f0deb4d4a5a676e4c5..1acf120503f014afb059654f38f530cbc64d5f00 100644
 3--- a/plugins/nvm/README.md
 4+++ b/plugins/nvm/README.md
 5@@ -22,9 +22,9 @@ These settings should go in your zshrc file, before Oh My Zsh is sourced:
 6   nvm has been installed, regardless of chip architecture, use `NVM_HOMEBREW=$(brew --prefix nvm)`.
 7 
 8 - **`NVM_LAZY`**: if you want the plugin to defer the load of nvm to speed-up the start of your zsh session,
 9-  set `NVM_LAZY` to `1`. This will use the `--no-use` parameter when loading nvm, and will create a function
10-  for `node`, `npm`, `yarn`, and the command(s) specified by `NVM_LAZY_CMD`, so when you call either of them,
11-  nvm will load with `nvm use default`.
12+  set `NVM_LAZY` to `1`. This will source nvm script only when using it, and will create a function for `node`,
13+  `npm`, `pnpm`, `yarn`, and the command(s) specified by `NVM_LAZY_CMD`, so when you call either of them,
14+  nvm will be loaded and run with default version.
15 
16 - **`NVM_LAZY_CMD`**: if you want additional command(s) to trigger lazy loading of nvm, set `NVM_LAZY_CMD` to
17   the command or an array of the commands.
18diff --git a/plugins/nvm/nvm.plugin.zsh b/plugins/nvm/nvm.plugin.zsh
19index 630854a7100e0b60ac756b2e502f8ecf5c5402ef..1fb4d238b41036f11c0b8d638a4db57295de78a7 100644
20--- a/plugins/nvm/nvm.plugin.zsh
21+++ b/plugins/nvm/nvm.plugin.zsh
22@@ -4,39 +4,33 @@ if [[ -z "$NVM_DIR" ]]; then
23     export NVM_DIR="$HOME/.nvm"
24   elif [[ -d "${XDG_CONFIG_HOME:-$HOME/.config}/nvm" ]]; then
25     export NVM_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/nvm"
26+  elif (( $+commands[brew] )); then
27+    NVM_HOMEBREW="${NVM_HOMEBREW:-${HOMEBREW_PREFIX:-$(brew --prefix)}/opt/nvm}"
28+    if [[ -d "$NVM_HOMEBREW" ]]; then
29+      export NVM_DIR="$NVM_HOMEBREW"
30+    fi
31   fi
32 fi
33 
34 # Don't try to load nvm if command already available
35 # Note: nvm is a function so we need to use `which`
36-! which nvm &>/dev/null || return
37+which nvm &>/dev/null && return
38 
39-if [[ -f "$NVM_DIR/nvm.sh" ]]; then
40+if (( $+NVM_LAZY )); then
41+  # Call nvm when first using nvm, node, npm, pnpm, yarn or $NVM_LAZY_CMD
42+  function nvm node npm pnpm yarn $NVM_LAZY_CMD {
43+    unfunction nvm node npm pnpm yarn $NVM_LAZY_CMD
44+    # Load nvm if it exists in $NVM_DIR
45+    [[ -f "$NVM_DIR/nvm.sh" ]] && source "$NVM_DIR/nvm.sh"
46+    "$0" "$@"
47+  }
48+elif [[ -f "$NVM_DIR/nvm.sh" ]]; then
49   # Load nvm if it exists in $NVM_DIR
50-  source "$NVM_DIR/nvm.sh" ${NVM_LAZY+"--no-use"}
51-elif (( $+commands[brew] )); then
52-  # Otherwise try to load nvm installed via Homebrew
53-  # User can set this if they have an unusual Homebrew setup
54-  NVM_HOMEBREW="${NVM_HOMEBREW:-${HOMEBREW_PREFIX:-$(brew --prefix)}/opt/nvm}"
55-  # Load nvm from Homebrew location if it exists
56-  if [[ -f "$NVM_HOMEBREW/nvm.sh" ]]; then
57-    source "$NVM_HOMEBREW/nvm.sh" ${NVM_LAZY+"--no-use"}
58-  else
59-    return
60-  fi
61+  source "$NVM_DIR/nvm.sh"
62 else
63   return
64 fi
65 
66-# Call nvm when first using node, npm or yarn
67-if (( $+NVM_LAZY )); then
68-  function node npm yarn $NVM_LAZY_CMD {
69-    unfunction node npm yarn $NVM_LAZY_CMD
70-    nvm use default
71-    command "$0" "$@"
72-  }
73-fi
74-
75 # Autoload nvm when finding a .nvmrc file in the current directory
76 # Adapted from: https://github.com/nvm-sh/nvm#zsh
77 if (( $+NVM_AUTOLOAD )); then