shellenv: check that Homebrew's prefix precedes /usr/bin

Some setups (e.g. a default `tmux` session) will result in a user
starting multiple nested login shells. This is a problem for the way we
currently handle `shellenv` because a login shell invokes
`/usr/libexec/path_helper` which will prepend the contents of
`/etc/paths` and `/etc/paths.d` to `PATH`. In particular, the second
login shell will place `brew` further down the `PATH` than desired in a
non-`/usr/local` prefix, since `brew shellenv` will no longer produce
output the second time around.

Let's fix this by replacing the check that `brew` is in `PATH` with a
check that `${HOMEBREW_PREFIX}/bin` precedes `/usr/bin` in `PATH`.

Fixes #11851, #11883, Homebrew/discussions#2547.

See also:
- Homebrew/discussions#2237
- https://superuser.com/questions/544989/does-tmux-sort-the-path-variable
This commit is contained in:
Carlo Cabrera 2021-11-30 22:12:22 +08:00
parent 5fb34c8ef2
commit a62e88304b
No known key found for this signature in database
GPG Key ID: C74D447FC549A1D0

View File

@ -11,7 +11,7 @@
# shellcheck disable=SC2154
homebrew-shellenv() {
if [[ "${HOMEBREW_SHELLENV_PREFIX}" == "${HOMEBREW_PREFIX}" ]] &&
[[ "$(PATH="${HOMEBREW_PATH}" command -v brew)" == "${HOMEBREW_PREFIX}/bin/brew" ]]
[[ "${HOMEBREW_PATH%%/usr/bin:*}" == *"${HOMEBREW_PREFIX}/bin"* ]]
then
return
fi