浏览代码

fix(brew): do not source `brew shellenv` if already on path (#11167)

tmoschou 1 年之前
父节点
当前提交
c7c0a8917b
共有 2 个文件被更改,包括 14 次插入8 次删除
  1. 4 1
      plugins/brew/README.md
  2. 10 7
      plugins/brew/brew.plugin.zsh

+ 4 - 1
plugins/brew/README.md

@@ -10,7 +10,10 @@ plugins=(... brew)
 
 ## Shellenv
 
-This plugin also executes `brew shellenv` at plugin load to set up many useful variables, such as `HOMEBREW_PREFIX` or `HOMEBREW_REPOSITORY`.
+If `brew` is not found in the PATH, this plugin will attempt to find it in common
+locations, and execute `brew shellenv` to set the environment appropriately.
+This plugin will also export `HOMEBREW_PREFIX="$(brew --prefix)"` if not previously
+defined for convenience.
 
 ## Aliases
 

+ 10 - 7
plugins/brew/brew.plugin.zsh

@@ -10,18 +10,21 @@ if (( ! $+commands[brew] )); then
   else
     return
   fi
+
+  # Only add Homebrew installation to PATH, MANPATH, and INFOPATH if brew is
+  # not already on the path, to prevent duplicate entries. This aligns with
+  # the behavior of the brew installer.sh post-install steps.
+  eval "$("$BREW_LOCATION" shellenv)"
+  unset BREW_LOCATION
 fi
 
 if [[ -z "$HOMEBREW_PREFIX" ]]; then
-  if [[ -z $BREW_LOCATION ]]; then
-    eval "$(brew shellenv)"
-  else
-    eval "$("$BREW_LOCATION" shellenv)"
-  fi
+  # Maintain compatability with potential custom user profiles, where we had
+  # previously relied on always sourcing shellenv. OMZ plugins should not rely
+  # on this to be defined due to out of order processing.
+  export HOMEBREW_PREFIX="$(brew --prefix)"
 fi
 
-unset BREW_LOCATION
-
 alias bcubc='brew upgrade --cask && brew cleanup'
 alias bcubo='brew update && brew outdated --cask'
 alias brewp='brew pin'