Let's avoid having `exec` and `env`/`sh` subcommands have different
environment variables logic. Instead, let's e.g. add `HOMEBREW_PATH`
and remove `*/Homebrew/shims/*` from the `PATH` for all three
subcommands.
This allows more consistency for users and easier to debug and reason
about behaviour.
`brew bundle exec` behaves correctly only after doing `brew bundle install`.
Running `brew bundle check` can be slow, so let's add a `--check` flag
to `brew bundle exec` which will also run `brew bundle check` before
`brew bundle exec` to ensure that the `Brewfile` has been installed
before proceeding.
- if HOMEBREW_TEMP is not writable, use the default temp directory
- when running `brew bundle exec`, strip various temporary directories
from the environment if they are not writable
- Rather than maintaining a list of Homebrew environment variables to
delete, delete all Homebrew (and Portable Ruby) internal variables
that won't be used by other tools.
- When exporting variables, only export variables that have changed.
- When exporting PATH-like variables, ensure the PATH is appending to
the existing path rather than replacing it and ensure we only include
newly added paths.
This flag allows you to specify formulae to upgrade, even if
`$HOMEBREW_BUNDLE_NO_UPGRADE` is set.
This is useful for upgrading specific formulae without upgrading all
formulae.
While we're here, let's add Sorbet signatures to the `Bundle` module
because I needed to add a new method there anyway.
Extract the relevant logic from `formula.rb`, moving to `keg.rb` and
then use this logic in `bundle/commands/cleanup.rb` to ensure that we
don't say we need to uninstall formulae that should be still kept.
If callers of `brew bundle check` have already output some formulae
errors, they can set the
`HOMEBREW_BUNDLE_CHECK_ALREADY_OUTPUT_FORMULAE_ERRORS` environment
variable to the names of the formulae that have already been output.
- Sort output by key so it's more readable.
- Skip exporting empty values because shell scripts treat them as unset.
- Skip exporting non-Homebrew things that were already set in the old
environment to avoid massive duplication and higher chances of e.g.
bad escapes breaking things.
We've had requests for this in Homebrew/bundle a few times so let's
implement it both for there and for `brew edit`.
Tested on my machine with `cursor` and working as expected.