Homebrew / brew

🍺 The missing package manager for macOS (or Linux)

Home Page:https://brew.sh

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

System brew.env not taking precedence when HOMEBREW_SYSTEM_ENV_TAKES_PRIORITY is set

Josh-Ng opened this issue · comments

brew doctor output

Your system is ready to brew.

Verification

  • My "brew doctor output" above says Your system is ready to brew. and am still able to reproduce my issue.
  • I ran brew update twice and am still able to reproduce my issue.
  • This issue's title and/or description do not reference a single formula e.g. brew install wget. If they do, open an issue at https://github.com/Homebrew/homebrew-core/issues/new/choose instead.

brew config output

HOMEBREW_VERSION: 4.4.11
ORIGIN: https://github.com/Homebrew/brew
HEAD: ad356d365837a691a52dfec32b3fb5f282f3aefc
Last commit: 3 days ago
Branch: stable
Core tap HEAD: 79e30cce5e982093fb15363f2581dec221a67b38
Core tap last commit: 62 minutes ago
Core tap JSON: 12 Dec 20:18 UTC
Core cask tap JSON: 12 Dec 20:18 UTC
HOMEBREW_PREFIX: /opt/homebrew
HOMEBREW_CASK_OPTS: []
HOMEBREW_MAKE_JOBS: 10
HOMEBREW_SORBET_RUNTIME: set
Homebrew Ruby: 3.3.6 => /opt/homebrew/Library/Homebrew/vendor/portable-ruby/3.3.6/bin/ruby
CPU: 10-core 64-bit arm_firestorm_icestorm
Clang: 16.0.0 build 1600
Git: 2.39.5 => /Applications/Xcode.app/Contents/Developer/usr/bin/git
Curl: 8.7.1 => /usr/bin/curl
macOS: 14.6.1-arm64
CLT: 16.2.0.0.1.1733547573
Xcode: 16.2
Rosetta 2: false

Though changes, with the below testing

What were you trying to do (and why)?

Configure variables at the system level /etc/homebrew/brew.env so that they are not over-written by other configuration files.

What happened (include all command output)?

When running brew config with two environment files configured, the environment variables set in /etc/homebrew/brew.env are overridden by ~/.homebrew/brew.env, even though /etc/homebrew/brew.env has HOMEBREW_SYSTEM_ENV_TAKES_PRIORITY=1 set.

What did you expect to happen?

When HOMEBREW_SYSTEM_ENV_TAKES_PRIORITY=1 is set in /etc/homebrew/brew.env any variable prefixed with HOMEBREW_ defined in /etc/homebrew/brew.env should override definitions elsewhere.

Step-by-step reproduction instructions (by running brew commands)

Create a file in `/etc/homebrew/brew.env`
Populate with HOMEBREW_SYSTEM_ENV_TAKES_PRIORITY=1 and any other HOMEBREW_ environment variable.
Create a file in `~/.homebrew/brew.env`, and populate with HOMEBREW_ environment variables matching system with different values.

Run `brew config`

Acutally you can see this just with HOMEBREW_SYSTEM_ENV_TAKES_PRIORITY=1 in /etc/homebrew/brew.env and HOMEBREW_SYSTEM_ENV_TAKES_PRIORITY= in ~/homebrew/brew.env

This functioned prior to this change going in, which looks to have intended to remove usage of eval: bcbb969#diff-a8ebbe0466e56bdb9c7ebc74d5ffbd383b8c0902fcb5146cb1960e3d69dadeb1R100

The original loaded the env variable first from /etc/homebrew/brew.env here: 375a7ee

which is the desired behaviour.

Can't reproduce:

❯ cat ~/.homebrew/brew.env
HOMEBREW_AUTO_UPDATE_SECS=3600

❯ cat /etc/homebrew/brew.env
HOMEBREW_SYSTEM_ENV_TAKES_PRIORITY=1
HOMEBREW_AUTO_UPDATE_SECS=1

❯ brew config | grep AUTO_UPDATE_SECS
HOMEBREW_AUTO_UPDATE_SECS: 1

When I remove HOMEBREW_SYSTEM_ENV_TAKES_PRIORITY:

❯ cat ~/.homebrew/brew.env
HOMEBREW_AUTO_UPDATE_SECS=3600

❯ cat /etc/homebrew/brew.env
HOMEBREW_AUTO_UPDATE_SECS=1

❯ brew config | grep AUTO_UPDATE_SECS
HOMEBREW_AUTO_UPDATE_SECS: 3600

Tested with

❯ brew --version
Homebrew 4.4.11
Homebrew/homebrew-core (git revision e4efa203f91; last commit 2024-12-12)
Homebrew/homebrew-cask (git revision 8e9f646c3d5; last commit 2024-12-12)

Okay I have this:

% cat /etc/homebrew/brew.env
HOMEBREW_SYSTEM_ENV_TAKES_PRIORITY=1
HOMEBREW_AUTO_UPDATE_SECS= 1
% cat ~/.homebrew/brew.env 
HOMEBREW_AUTO_UPDATE_SECS= 3600
% brew config | grep AUTO_UPDATE
HOMEBREW_AUTO_UPDATE_SECS:  3600
brew --version
Homebrew 4.4.11
Homebrew/homebrew-core (git revision 79e30cce5e9; last commit 2024-12-12)

Edit:
My colleagues are getting the same behaviour, not sure what's happened with yours above

What's the output of

brew config | grep SYSTEM_ENV

when /etc/homebrew/brew.env contains

HOMEBREW_SYSTEM_ENV_TAKES_PRIORITY=1
HOMEBREW_AUTO_UPDATE_SECS=1

?

I get this:

~ % cat /etc/homebrew/brew.env
HOMEBREW_SYSTEM_ENV_TAKES_PRIORITY=1
HOMEBREW_AUTO_UPDATE_SECS=1
~ % cat ~/.homebrew/brew.env  
HOMEBREW_AUTO_UPDATE_SECS= 3600
~ % brew config | grep SYSTEM_ENV
HOMEBREW_SYSTEM_ENV_TAKES_PRIORITY: set
~ % 

Note that I can also unset that in ~/.homebrew/brew.env

~ % cat /etc/homebrew/brew.env   
HOMEBREW_SYSTEM_ENV_TAKES_PRIORITY=1
HOMEBREW_AUTO_UPDATE_SECS=1
~ % cat ~/.homebrew/brew.env     
HOMEBREW_SYSTEM_ENV_TAKES_PRIORITY=
HOMEBREW_AUTO_UPDATE_SECS= 3600
~ % brew config | grep SYSTEM_ENV
~ % 

Try applying this patch to see if it helps:

diff --git a/bin/brew b/bin/brew
index 58bc4f24a3..14c135f0f0 100755
--- a/bin/brew
+++ b/bin/brew
@@ -131,12 +131,12 @@ export_homebrew_env_file() {
 }
 
 # First, load the system-wide configuration.
+export_homebrew_env_file "/etc/homebrew/brew.env"
+
 unset SYSTEM_ENV_TAKES_PRIORITY
 if [[ -n "${HOMEBREW_SYSTEM_ENV_TAKES_PRIORITY-}" ]]
 then
   SYSTEM_ENV_TAKES_PRIORITY="1"
-else
-  export_homebrew_env_file "/etc/homebrew/brew.env"
 fi
 
 # Next, load the prefix configuration
@@ -152,7 +152,7 @@ fi
 
 export_homebrew_env_file "${HOMEBREW_USER_CONFIG_HOME}/brew.env"
 
-# If the system configuration takes priority, load it last.
+# If the system configuration takes priority, load it again to override any previous settings.
 if [[ -n "${SYSTEM_ENV_TAKES_PRIORITY-}" ]]
 then
   export_homebrew_env_file "/etc/homebrew/brew.env"

I made a fork for the same 👍

Does the trick

Raised this: #18926

OH timing lol