Commit Graph
515 Commits
Author SHA1 Message Date
Mike McQuaid 059785dc91 Preserve locale during brew startup
- Validate the locale charmap instead of trusting its name.
- Reuse filtered Linux locale settings to avoid `locale -a`.
2026-07-28 12:03:52 +01:00
Mike McQuaid d8952792a8 Remove most forks from no-op brew startup
- Parse `.git/config` once in Bash for the `devcmdrun` and analytics
  settings rather than forking `git config` via the shim four times.
- Read `.git/HEAD`, loose refs and `packed-refs` directly and keep
  `git rev-parse` only as a fallback for worktrees and symrefs.
- Only run the slow `git diff` describe-cache dirty check for
  developers as they are the only users likely to have dirty trees.
- Parse `SystemVersion.plist` rather than forking `sw_vers`, which
  reads the same file; keep `sw_vers` as a fallback.
- Check locale names rather than forking `locale charmap`; the
  filtered environment can never report UTF-8 anyway.
- Fork `sysctl` only when Rosetta is possible and run
  `${HOMEBREW_CURL}` directly rather than through the `curl` shim.
- Prefer builtins to `cat`, `readlink`, `dirname`, `tee` and
  subshells on the startup path.
- Extract the OS, auto-update, curl and Git logic from `brew.sh`
  into new `utils/os.sh`, `utils/auto-update.sh`, `utils/curl.sh`
  and `utils/git.sh` files; the same work happens in the same order.
- Warm no-op timings: `brew shellenv` 16ms to 9.5ms, `brew --version`
  62ms to 11ms and full command dispatch 275ms to 83ms.
2026-07-25 19:22:14 +01:00
Mike McQuaid 5da7b049b0 Sample only user-set env config in analytics
- The formulae.brew.sh env-config table was hard to interpret: unset
  and explicitly-set-to-default runs were merged as "default events",
  nothing showed what a variable's default was and variables brew
  exports itself (`HOMEBREW_UPDATE_TO_TAG` in `cmd/update.sh`,
  `HOMEBREW_EDITOR`/`HOMEBREW_DISPLAY` etc. copied from the user
  environment in `bin/brew`) dominated the chart as fake user
  configuration.
- Record `HOMEBREW_USER_SET_VARS` at `bin/brew` startup, after
  `brew.env` files load but before any exports, so sampling and
  `brew config` can tell deliberate configuration apart; sub-brews
  inherit the list.
- Replace the `env_config_non_default` tag with a single
  `env_config_state` tag (`unset`/`default`/`non_default`) computed
  from user-set values only.
- Compute previously `default_text`-only defaults so explicitly-set
  default values are ignored everywhere: `HOMEBREW_AUTO_UPDATE_SECS`
  (mirroring `brew.sh`), `HOMEBREW_FORBID_PACKAGES_FROM_PATHS` (now
  `boolean: :set`, matching its accessor), `HOMEBREW_PIP_INDEX_URL`
  and `HOMEBREW_SSH_CONFIG_PATH`; generated accessors and
  `non_default_variable?` call callable defaults and treat blank
  booleans as unset like their accessors.
- `brew config` now only prints user-set non-default variables and
  `Resource#determine_url_mirrors` only rewrites PyPI URLs for
  non-default `$HOMEBREW_PIP_INDEX_URL` values.
- Sanitise `formula-analytics` results: only current variables and
  valid states are accepted and the query requires the new tag,
  which drops the contaminated data already collected.
- Stop excluding CI events from `command_run_options`: CI users are
  normal users so CI-only usage should stay visible.
- Output `non_default_count`, `set_default_count`, `unset_count` and
  `default_value` per variable in JSON, using the human
  `default_text` summary for varying defaults via
  `EnvConfig.default_description`, now shared with the manpage.
2026-07-24 19:40:08 +01:00
Michael Cho 25d32f2450 extend/os/linux/cask/config: actually use XDG_DATA_HOME 2026-04-11 13:51:43 -04:00
James Roberts-Thomson e1305b80dc Add 'LANG' to USED_BY_HOMEBREW_VARS, so it will automatically define HOMEBREW_LANG from LANG in user's env 2026-04-08 20:32:40 +12:00
James Roberts-Thomson 5e1c4dd6d7 Add 'LANG' to ENV_VAR_NAMES so it is included in the filtered environment 2026-04-07 16:00:01 +12:00
Branch Vincent 188ce1ef95 bundle/cargo: respect CARGO_HOME and friends 2026-03-23 21:31:55 -07:00
Carlo CabreraandCarlo Cabrera ef949fc7a2 Do not allow setting HOMEBREW_EXPERIMENTAL_RUST_FRONTEND in env file
We rely on being able to `unset` this environment variable when handing
back to Ruby, but we won't be able to unset this if we got it from an
env file.
2026-03-23 00:59:19 +08:00
Mike McQuaid 1730ec2f7a Homebrew startup performance tweaks
Move some stuff around to improve cold startup performance.

`brew --prefix` went from about 16.5 ms to 9.0 ms
`brew help` went from about 16.5 ms to 8.7 ms.
2026-03-14 16:43:07 +00:00
Carlo CabreraandCarlo Cabrera cc5000dd99 bin/brew: tighten regex for variable exclusion
This will ensure that we don't inadvertently filter out future
environment variables that start with text contained in
`BIN_BREW_EXPORTED_VARS` (e.g. `HOMEBREW_REPOSITORY_*`).
2026-03-09 20:52:24 +08:00
Carlo CabreraandCarlo Cabrera 21f7ed28f6 bin/brew: simplify filtering of BIN_BREW_EXPORTED_VARS
Looping over `BIN_BREW_EXPORTED_VARS` for each line of an env file is
relatively inefficient. We can avoid this by constructing a regex before
the loop and using that regex to match against the lines we want to
skip.
2026-03-09 17:08:05 +08:00
TANIGUCHI KoheiandClaude Opus 4.6 3efedbcf97 bin/brew: preserve XDG_CONFIG_HOME through environment filter
Add `XDG_CONFIG_HOME` to `USED_BY_HOMEBREW_VARS` so it is copied to
`HOMEBREW_XDG_CONFIG_HOME` and survives the `env -i` filter, matching
the existing pattern for `XDG_CACHE_HOME`, `XDG_DATA_DIRS` and
`XDG_RUNTIME_DIR`.

Also fall back to `HOMEBREW_XDG_CONFIG_HOME` when computing
`HOMEBREW_USER_CONFIG_HOME`, so that the value is correct when
`bin/brew` is re-executed after auto-update (via `exec` in `brew.sh`)
and the original `XDG_CONFIG_HOME` has been filtered out.

Without this fix, `brew bundle --global` fails with "No Brewfile found"
for users whose Brewfile is at `$XDG_CONFIG_HOME/homebrew/Brewfile`
whenever auto-update triggers a re-exec.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-02 01:55:04 +09:00
Pablo Castellazzi 82c6382da6 Allow environment variable WSL_DISTRO_NAME.
Visual Studio Code, when run from WSL (Windows
Subsystem for Linux) uses WSL_DISTRO_NAME presence
to detect it. When it fails to do so its spawn the
GUI instead of running the CLI application.
2025-11-13 03:23:46 -03:00
Wheeler Law ae0df0ef90 Pass through VSCODE_IPC_HOOK_CLI to brew edit 2025-11-11 17:59:32 +00:00
Mike McQuaid abbc591175 Pass through GOBIN and GOPATH to brew bundle
Fixes #20863
2025-10-13 08:24:17 +01:00
Bo AndersonandGitHub e13f4a8295 Merge pull request #20486 from Homebrew/env-file-expand
bin/brew: expand env file filter to allow manpage-documented envs
2025-08-18 14:24:53 +00:00
Bo Anderson 5e339e2081 bin/brew: expand env file filter to allow manpage-documented envs 2025-08-18 13:46:08 +01:00
Carlo CabreraandCarlo Cabrera b7d8072575 Restore handling of HOMEBREW_BREW_WRAPPER
This now requires `HOMEBREW_DISABLE_NO_FORCE_BREW_WRAPPER` to be unset.
If it is set (but only in a `brew.env` file), then we use the new
functionality of checking the path of the parent process.
2025-08-13 14:41:46 +08:00
Carlo CabreraandCarlo Cabrera a7c124c2d0 brew.sh: enforce HOMEBREW_FORCE_BREW_WRAPPER more strictly
`HOMEBREW_FORCE_BREW_WRAPPER` can be used as a security/compliance
feature, but allowing it to be disabled by setting
`HOMEBREW_NO_FORCE_BREW_WRAPPER` leaves a pretty large hole in it that
allows it to be sidestepped.

Let's fix that by actually checking the path of the process that called
`brew`, and the verify that that path matches the configured value of
`HOMEBREW_NO_FORCE_BREW_WRAPPER`.
2025-08-09 03:15:53 +08:00
Mike McQuaid 916c25ef18 bin/brew: don't copy across zeroed env values. 2025-06-05 08:33:08 +01:00
Mike McQuaid 7c5affa824 brew.sh: move sudo reset timestamp.
Let's move this from `bin/brew` to make things like e.g. `brew --prefix`
or `brew shellenv` not reset the sudo timestamp.

This is still in a place that ensures that e.g. no untrusted formula or
tap code has been run yet so should have no security implications but
provide mild usability improvements.
2025-06-02 15:09:27 +01:00
Mike McQuaid 6e20cedb67 Don't filter the environment for brew bundle (exec|env|sh)
It's both unexpected and undesirable for `brew bundle (exec|env|sh)` to
filter the environment and makes these tools less useful.

Not filtering the environment, though, causes issues with the
`brew bundle sh` shell. Fix this up and, while we're here, also improve
the formatting for both `zsh` and `bash` (the default) to use nicer and
more consistent prompts and colours.

To simplify this, consolidate some logic in a new
`Utils::Shell.shell_with_prompt` method and add tests for it and a
similar notice for `brew bundle sh`.

Finally, avoid printing out the notice when `HOMEBREW_NO_ENV_HINTS` is
set.
2025-03-24 13:37:25 +00:00
Mike McQuaidandGitHub 816cda2093 Merge pull request #19062 from Homebrew/bash_pu
bin/brew: don't allow unbound variables.
2025-01-09 13:00:26 +00:00
Mike McQuaid 7c7b15d886 bin/brew: don't allow unbound variables.
This can fail for users using `bash -u` or `set -u` in their shell so
let's try to be stricter here for both them and us.
2025-01-09 12:48:48 +00:00
Mike McQuaid ac7f3d6182 bin/brew: handle unbound variable.
This can fail when running `bin/brew` under `set -u`.
2025-01-09 12:25:10 +00:00
Mike McQuaid e9b4979f40 Add support for Homebrew wrappers
Allow the ability for a system administrator to use
`HOMEBREW_BREW_WRAPPER` and `HOMEBREW_FORCE_BREW_WRAPPER` variables to
enforce the usage of a particular `brew` command for non-trivial (e.g.
`brew --prefix` is considered trivial, it doesn't need to write to the
prefix) Homebrew commands.

This also introduces a `HOMEBREW_ORIGINAL_BREW_FILE` variable for some
internal usage; `HOMEBREW_BREW_FILE` was being used internally for
both "how should we shell out to Homebrew" and "what should we use
to check permissions on Homebrew". `HOMEBREW_ORIGINAL_BREW_FILE` is
now used just for the latter case.

Inspired by conversation in
https://github.com/Homebrew/homebrew-bundle/pull/1551 which suggested
this was worth fixing in wider than just `brew bundle`.
2025-01-07 17:40:18 +00:00
Carlo CabreraandCarlo Cabrera 2a3169da76 bin/brew: fix prioritisation of system env files
Fixes #18925.
2024-12-13 06:19:16 +08:00
Tianshu Wang 4f62fd0789 Revert $TMPDIR for emacsclient 2024-09-05 15:49:11 +08:00
Carlo Cabrera cd4f6eb2c0 bin/brew: improve sudo handling
- prioritise `/usr/bin/sudo` instead of any random `sudo` in `PATH`
2024-08-25 18:03:16 +08:00
Mike McQuaidandGitHub 97db68d408 Revert "bin/brew: tighten check in export_homebrew_env_file" 2024-08-21 09:46:49 +01:00
Carlo Cabrera 4a11e74307 bin/brew: tighten check in export_homebrew_env_file
The current glob check will accept lines like

  HOMEBREW_FOO=bar BAD_ENV_VAR=baz

and happily export them, but we don't want that.

Let's tighten up the check to reject lines like the above.
2024-08-20 14:54:39 +08:00
Rylan Polster 842b4ac3d3 Improve VS Code debugger support 2024-08-15 11:47:31 -04:00
Adrian HoandMike McQuaid 3eaebdc9d8 diagnostic: fix XDG_DATA_DIRS check
Pass `XDG_DATA_DIRS` through `bin/brew` so that it can be checked.
2024-08-05 09:11:01 +01:00
Anil Madhavapeddy 0c85c07be5 bin/brew: do not exit on sudo errors for resetting timestamp
In #17694, a call to `sudo --reset-timestamp` was added to prevent
cached credentials being run. Unfortunately, this breaks `brew`
invocation under `sandbox-exec`, which forbids any attempts to
exec setuid executables.  This in turn breaks the OCaml opam
package manager, which sandboxes its build commands (including
brew prefix queries): see https://github.com/ocaml/opam/issues/6117
for that error.

This commit just changes the sudo invocation to suppress errors
and continue if it fails, which should be harmless in normal
operation as sudo doesn't emit an error for this option when
invoked normally.
2024-07-25 18:50:15 +01:00
Sean Molenaar 2adf25dcaf feat: do not use existing sudo authentication in brew commands 2024-07-13 22:24:00 +02:00
Leo Heitmann Ruiz 4ac57d85b2 Read ZDOTDIR environment variable 2024-06-12 16:53:43 +02:00
Bo Anderson 2f0f5fbb61 bin/brew: fix tracking of original paths with brew-in-brew calls 2024-06-04 03:47:31 +01:00
Mike McQuaid 0c1766510a env_config: add HOMEBREW_BUNDLE_USER_CACHE.
Used to configure `BUNDLE_USER_CACHE`.
2024-05-18 00:08:42 +09:00
Mike McQuaid 0d1c7b9367 bin/brew: improve a comment. 2024-05-14 15:46:33 +09:00
Bo Anderson 636d883008 Support bash in privileged mode 2024-03-27 04:40:44 +00:00
Mike McQuaid f404aafe56 brew: clarify which user needs to be able to read the working directory.
Otherwise, in multi-user environments, it can be ambiguous.
2024-01-11 12:24:04 +00:00
Mike McQuaid abc1d14807 Improve working directory error messages
- Check for directory existing and directory being readable separately
  and print appropriate messages for each
- Make error messages more consistent
2024-01-10 17:21:57 +00:00
Mike McQuaid 06752f032f cmd/*env-sync: use *ENV_ROOT
Fixes https://github.com/Homebrew/brew/issues/16428
2024-01-09 10:13:33 +00:00
Greg NISBET 077eee8f86 [shell] Override CDPATH in brew script.
The CDPATH environment variable can affect the behavior of
`cd`, and `cd` takes the `-P`, `-L` and `-e` flags.
(I didn't know about `-e` until looking at the source)
Make quietcd more robust by setting the CDPATH to ''.

I tested this change by running the following commands from a nonstandard
directory, which caused some of tcsh's and perl's dependencies to be recompiled.

$ [path to brew] install tcsh
$ [path to brew] install perl

Here's a link to the source code of `cd` in a mirror of the bash repo.

https://github.com/bminor/bash/blob/ec8113b9861375e4e17b3307372569d429dec814/builtins/cd.def#L267
2023-11-04 16:21:31 -07:00
Mike McQuaidandGitHub 729a6d4eac Merge pull request #16064 from thecaralice/term-env
Add a few environment variables important for editors
2023-10-30 14:40:56 +00:00
Alice Carroll 9378b12d26 Re-export COLORTERM only locally in brew edit 2023-10-30 00:01:51 +03:00
apainintheneck e1dda872c3 use XDG_CACHE_HOME on Linux
This variable was getting filtered out before so it wasn't possible
for us to use it as the default for HOMEBREW_DEFAULT_CACHE
and HOMEBREW_DEFAULT_LOGS.
2023-10-28 19:10:20 -07:00
Alice Carroll 5bd9fe7303 Add a few environment variables important for editors 2023-09-30 05:51:36 +03:00
Mike McQuaid d43e1d0d83 bin/brew: pass through some SSH/sudo env vars.
`HOMEBREW_SSH_TTY` and `HOMEBREW_SUDO_USER` will be needed for
`brew services` after
https://github.com/Homebrew/homebrew-services/pull/588 is merged.
2023-09-27 11:59:28 +01:00
Mike McQuaid 4184546067 bin/brew: require HOME to be set.
Rather than having increasingly involved workarounds that require
other variables to be set.

Closes #15822
2023-08-05 13:41:50 +01:00