496 Commits

Author SHA1 Message Date
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 McQuaid
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 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 McQuaid
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 Ho
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.

ec8113b986/builtins/cd.def (L267)
2023-11-04 16:21:31 -07:00
Mike McQuaid
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
Carlo Cabrera
bcbb969796
bin/brew: avoid eval and grep
`eval` is a much bigger hammer than we need here, so let's try to avoid
that.

Also, we can use the builtin `read` instead of shelling out to `grep`
which will be slightly more efficient.
2023-08-04 22:00:46 +08:00
Mike McQuaid
59ea118991
bin/brew: handle missing $HOME.
Try to build it using `$USER` or `$LOGNAME` and, if both are missing,
just give up.
2023-08-04 08:30:37 +01:00
Mike McQuaid
375a7ee8dc
Allow configuring Homebrew with .env files
For a long time people have requested some sort of configuration files
for Homebrew. Now: here's the first version of that.

Similarly to how you can configure Git for a system, a repository or
a user: you can configure Homebrew for a system, a prefix or a user.

The system-wide configuration file is `/etc/homebrew/brew.env`, the
prefix-specific configuration file is
`$HOMEBREW_PREFIX/etc/homebrew/brew.env`
and the user-specific configuration file is `~/.homebrew/brew.env`.

As we need to read these files from Bash in `bin/brew` (so they can)
influence functionality ASAP: they are in a simple format that Bash
can read. It may be that we have more complex array or hash data in
future that's configured through JSON or YAML (most likely JSON as we
use it more) and stored in a `brew.json`/`brew.yaml` file in the same
directory.

As this is relying on `eval` in Bash which is fairly dangerous: we
filter the lines with a regex to ensure we're only permitting setting
`HOMEBREW_*` variables and nothing more.

To give a bit of power to system administrators, the
`HOMEBREW_SYSTEM_ENV_TAKES_PRIORITY` variable can be set in
`/etc/homebrew/brew.env` to ensure that the system-wide configuration
file is loaded last and overrides any prefix or user settings.

Now that we have an actual location for configuration files, let's also
change the `brew livecheck` watchlist configuration file to be in this
directory and deprecate the existing location. As this is a developer
command and the mitigation is to just move the file: we don't need to
follow the normal deprecation process here.
2023-07-28 17:17:14 +01:00
Bo Anderson
623eeb4e47
cmd/update-report: tweak messages for CI 2023-07-07 21:10:21 +01:00
Patrick Linnane
a7e23a27ae
bin/brew: remove HOMEBREW_NO_ENV_FILTERING error 2023-06-25 11:47:42 -07:00
Mike McQuaid
e6a006ee6b
Revert "Guard GITHUB_* variables by GITHUB_ACTIONS." 2023-05-24 12:49:27 +01:00
Markus Reiter
f320139fca
Guard GITHUB_* variables by GITHUB_ACTIONS. 2023-05-19 19:56:02 +02:00
Carlo Cabrera
b65f08700d
bin/brew: add all GITHUB_* vars to env allowlist
See #15441.
2023-05-17 19:53:46 +08:00
Carlo Cabrera
da0cd37bd4
brew/brew: add GITHUB_EVENT_PATH to env allowlist
Needed for Homebrew/homebrew-test-bot#922.
2023-05-17 01:41:58 +08:00
William Woodruff
ce7283ac32
brew: add GITHUB_STEP_SUMMARY to allowlist
Signed-off-by: William Woodruff <william@yossarian.net>
2023-05-02 19:24:14 -04:00
Carlo Cabrera
de95396407
brew: don't filter GITHUB_EVENT_NAME
We need this in `test-bot`. See Homebrew/homebrew-test-bot#905 and
Homebrew/homebrew-test-bot#906.
2023-04-14 20:40:14 +08:00
Carlo Cabrera
6f63be411b
bin/brew: refine generation of HOMEBREW_* env vars
We inherit some variables from the user's environment but prefix them
with `HOMEBREW_`. This is the case for (1) variables documented in the
manpage and also for some (2) variables used internally by `brew`.

We currently treat both types identically, but we should not. In
particular, we allow users to override type (1), but we don't want to do
this for type (2).

This was partially fixed in f4103e5d61526cfbf7f31540ba45ec171adc452e,
but that fix did not go far enough. Some variables that the user should
not be allowed to override can still be overridden.

This change completes the partial fix and refactors the code so that
we're less likely to mistakenly conflate the two types of variables in
the future.
2023-02-15 13:27:02 +08:00
Daniel Bayley
2ff90c3232 Fix HOMEBREW_BAT_CONFIG_PATH hint
`HOMEBREW_BAT_CONFIG_PATH` should respect [`BAT_CONFIG_PATH`](https://github.com/sharkdp/bat#configuration-file), in the same way that `HOMEBREW_BAT_THEME` [reflects](https://github.com/Homebrew/brew/pull/13385/files#diff-a8ebbe0466e56bdb9c7ebc74d5ffbd383b8c0902fcb5146cb1960e3d69dadeb1R78) `BAT_THEME`.

Previous, for context:
https://github.com/Homebrew/brew/pull/7488
https://github.com/Homebrew/brew/pull/11975
https://github.com/Homebrew/brew/pull/13385
2023-02-09 19:24:31 +00:00
Mike McQuaid
4c95e0dc3f
Merge pull request #14181 from kaihowl/fix/nounset
fix: allow running bin/brew with SHELLOPTS=nounset
2022-11-29 11:14:44 +00:00
Kai Hoewelmeyer
3e0bb17780 please shellcheck 2022-11-28 19:54:30 +01:00
Kai Hoewelmeyer
a5b29a79e6 and another one 2022-11-28 19:36:03 +01:00
Kai Hoewelmeyer
43c3c59ca4 fix one more 2022-11-28 19:34:20 +01:00
Kai Hoewelmeyer
4d4f476f73 set -u and fix follow up errors 2022-11-28 19:31:28 +01:00
Shivam Mathur
27dd46d6a9 bin/brew: add GITHUB_OUTPUT 2022-11-27 09:06:56 +00:00