You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
uninstall: refuse to remove build dependencies - #24713
brew autoremove keeps the build dependencies of formulae that were built from source, because the next upgrade needs them again and they take a while to install. brew uninstall has no such check. brew uninstall cmake removes it without a word, even when the installed formulae that need it to build have no bottle, and the next upgrade of any of them then starts by building cmake again.
brew uninstall now refuses in that case, as it does for runtime dependents, with the same --ignore-dependencies override:
$ brew uninstall --dry-run cmakeError: Refusing to uninstall /usr/local/Cellar/cmake/4.4.4because it is required to build blake3, ccache, cpp-httplib, doctest, fmt, jpeg-turbo, json-c, libgit2, libssh, llhttp, llvm, lz4, snappy, span-lite, tl-expected, xxhash and zstd, which are currently installed.You can override this and force removal with: brew uninstall --ignore-dependencies cmake
The rule is autoremove's own. Its keep logic moves from Utils::Autoremove into InstalledDependents, where uninstall already looks up runtime dependents, so that both commands share it. uninstall refuses exactly the build dependencies that autoremove keeps, and autoremove's own uninstall step cannot trip over the new check. Build dependencies are read from the current formula, so one that only the installed version needed does not block. Nothing changes where every formula was poured and still has a bottle, because pouring does not use build dependencies.
brew bundle cleanup had its own, narrower copy of the keep rule and hands its whole list to one brew uninstall, which the new check would refuse. It now asks InstalledDependents.formulae_safe_to_remove instead. The receipt lookup that autoremove and leaves each spelled out moves into Formula#installed_runtime_dependency_names.
Benchmark with hyperfine (what brew benchmark --exec runs), 3 warmups and 15 runs each, on 117 installed formulae of which 96 were built from source:
The new check costs about 20 ms here. The shared rule now looks up each build dependency once (34 lookups instead of 143), which makes up for it because uninstall runs the autoremove rule afterwards too.
Have you checked for other open Pull Requests for the same change?
Have you explained what your changes do? Performance claims (e.g. "this is faster") must include brew benchmark results.
Have you explained why you'd like these changes included, not just what they do?
For bug fixes, have you given step-by-step brew commands to reproduce the bug?
Have you written new tests (excluding integration tests)? Here's an example.
Have you successfully run brew lgtm (style, typechecking and tests) locally?
I did not use AI/LLM to create this PR, or I disclosed the tool/model below and reviewed its output; every commit has a human author or co-author and I will answer maintainer questions and review comments myself without AI/LLM.
Claude Code (Fable 5.2) made the code and spec changes from my instructions. It also ran the checks on my machine: brew lgtm, and brew uninstall --dry-run from this branch against my installed kegs for the output above. I reviewed the diff.
No actionable comments were generated in the recent review.
Summary
Summary by CodeRabbit
Bug Fixes
Cleanup and autoremove better preserve formulae needed by installed formulae, including build dependencies and formulae marked to keep. Autoremove also preserves pinned formulae.
Uninstall checks for build-time dependencies and reports when a formula is needed to build an installed formula.
The leaves command uses recorded runtime dependency names to identify dependencies.
Dependency messages distinguish formulae required for building from those required at runtime, with clearer wording for one or multiple dependents.
Installed runtime and build dependency handling is centralised in InstalledDependents. Autoremove, bundle cleanup, leaves, and uninstall checks use updated dependency information. Formula adds a method to read installed runtime dependency names.
Changes: Installed dependency handling
Layer / File(s)
Summary
Dependency calculation and removal safety Library/Homebrew/formula.rb, Library/Homebrew/installed_dependents.rb, Library/Homebrew/test/formula_spec.rb, Library/Homebrew/test/installed_dependents_spec.rb
Formula adds installed_runtime_dependency_names. InstalledDependents adds build-dependency mapping, dependency-name collection, and iterative removal-safety logic. Tests cover these calculations.
Uninstall checks for build dependents when it finds no installed dependents. DependentsMessage adds build-specific wording, and tests cover the lookup and resulting message.
Autoremove and leaves integration Library/Homebrew/utils/autoremove.rb, Library/Homebrew/cleanup.rb, Library/Homebrew/cmd/leaves.rb, Library/Homebrew/test/cleanup_spec.rb, Library/Homebrew/test/cmd/leaves_spec.rb, Library/Homebrew/test/utils/autoremove_spec.rb
Autoremove and leaves use installed runtime dependency names. Formula removal delegates dependency-name and removal-safety calculations to InstalledDependents; pinned formulae are excluded from candidates before the safety check.
Bundle cleanup uses InstalledDependents to determine formulae to keep and removes recursive dependency traversal. Bundle formula hashes omit build_dependencies and poured_from_bottle?; cleanup fixtures and expectations are updated.
Merge Risk: Minimal · up to 96f61
This change makes uninstall refuse to remove formulae that installed formulae still need to build, using the same keep rule as autoremove. Earlier review concerns have been addressed, and no merge-blocking risk is evident from the supplied context.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title names the main change: brew uninstall now refuses to remove formulae that are build dependencies of installed formulae. The changeset adds a build-dependent check to uninstall.rb and moves t…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Description check
The description states the purpose and behaviour of the change: brew uninstall now refuses to remove build dependencies of installed formulae, reusing autoremove's keep rule through InstalledDependent…
Full details: Title check
Explanation
The title names the main change: brew uninstall now refuses to remove formulae that are build dependencies of installed formulae. The changeset adds a build-dependent check to uninstall.rb and moves the keep logic into InstalledDependents, which matches the title.
Full details: Description check
Explanation
The description states the purpose and behaviour of the change: brew uninstall now refuses to remove build dependencies of installed formulae, reusing autoremove's keep rule through InstalledDependents, and brew bundle cleanup now uses formulae_safe_to_remove. It also gives a usage example, benchmark results, and a note on the Formula#installed_runtime_dependency_names move. This matches the changeset closely, so the check passes.
Recent review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Organization UI
Review profile: CHILL
Plan: Advanced
Run ID: 6a4b33b7-97d1-49d8-b1dd-7f44ed3dfff0
Commits
Reviewing files that changed from the base of the PR and between 5e796ab and 96f61e5.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
160-160: Minor · Functional Correctness · Quick win
Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.
Handle the case where no keg remains after removal.
The guard on removed_kegs can leave keg as nil if every installed keg is in removed_kegs. The callers formulae_safe_to_remove and find_installed_build_dependents filter such formulae out first. The method is public and takes removed_kegs directly, so another caller can reach this state. In that case tab is nil and pours_bottle becomes nil. The method then treats the formula as source-built and keeps its build dependencies. Skip formulae with no remaining keg, or document the precondition.
Proposed fix
- keg = (formula.installed_kegs - removed_kegs).last if keg && removed_kegs.include?(keg)+ keg = (formula.installed_kegs - removed_kegs).last if keg && removed_kegs.include?(keg)+ next if keg.nil? && removed_kegs.any?
Review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Organization UI
Review profile: CHILL
Plan: Advanced
Run ID: 2b396aee-da07-4a7b-9ee9-73592ce478f3
Commits
Reviewing files that changed from the base of the PR and between 37e0355 and d323516.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
- `brew autoremove` keeps the build dependencies of formulae built
from source, but `brew uninstall` removed them without a word, so
the next upgrade had to build them again.
- Refuse like for runtime dependents, with the same
`--ignore-dependencies` override.
- Move autoremove's keep rules into `InstalledDependents`, so that
`uninstall` and `bundle cleanup` share them without importing
from `utils/autoremove`.
- Make `brew bundle cleanup` keep what autoremove keeps instead of
its own narrower rule, so that its uninstall isn't refused.
- Read the dependency names of a receipt in one `Formula` method
instead of three copies in `autoremove` and `leaves`.
- Keep the dependencies of formulae with `.keepme` references in
`brew bundle cleanup`, so that its uninstall isn't refused, and
read its formulae from `Homebrew::Bundle::Brew.formulae` instead
of `Formula.installed`.
- Don't let a build dependency in an untrusted tap stop the scan
that every `brew uninstall` now runs.
- Still count a dependent that keeps another keg, and let an older
keg go while the one that `opt` points to stays.
- Rename `find_some_installed_build_dependents`, as it returns all
of them.
- Bound the loop in `formulae_safe_to_remove` and split up the
lines that were hard to read.
- A build dependency whose formula has no usable spec, such as a
tap formula that lost its URL, raised `FormulaSpecificationError`
and stopped the scan that every `brew uninstall` runs.
- Keep it by its loader name, like an untrusted one.
- `Formula[]` loads the stable spec, so `brew bundle cleanup` missed
a build dependency that only the `head` spec declares for a
formula installed from HEAD, and `brew uninstall` then refused its
list. `Formulary.resolve` loads the installed spec.
- A keg whose formula has no usable spec stopped the build
dependents check of `brew uninstall`. Fall back to the keg name,
as for the other lookup failures.
- The build dependents check read each formula's install receipt
from the keg that `opt` points to, even when the request removes
that keg and an older one stays.
- Read the receipt that is left after the removal, which is the one
`brew autoremove` would read afterwards, so that both still agree
on what to keep.
- `Formulary.resolve` raised `TapFormulaUnavailableError` for an
installed formula whose file is gone from its tap, which stopped
`brew bundle cleanup`.
- Fall back to the plain name, which loads the formula from its keg
like `Formula.installed` does.
- Load a formula that was deleted from its tap with
`Formulary.from_rack`, so the keg fallback in `brew bundle cleanup`
is explicit.
- Pull the two ternaries in `DependentsMessage#output` into variables.
- Keep one call per line in a wrapped chain for the new
`Homebrew/MultilineMethodChain` cop.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Uninstall is slower, I have nothing to autoremove so I wouldn't trust those numbers:
▶ cd /usr/local/Homebrew &&
base=$(git merge-base origin/main uninstall-build-dependents | cut -c1-10)&&forcmdin'uninstall --dry-run shfmt''autoremove --dry-run';do
hyperfine --warmup 3 --runs 15 \
--parameter-list ref "$base",uninstall-build-dependents \
--setup 'git checkout -q {ref}' \
--cleanup 'git checkout -q uninstall-build-dependents' \
"brew $cmd # {ref}"done
Benchmark 1: brew uninstall --dry-run shfmt # 62a9ad2642 (15 runs)
mean ± σ min … max
Wall Time 1.870 s ± 0.040 s 1.819 s … 1.991 s
Memory 90.7 MiB ± 0.3 MiB 90.1 MiB … 91.1 MiB
Benchmark 2: brew uninstall --dry-run shfmt # uninstall-build-dependents (15 runs)
mean ± σ min … max
Wall Time 1.972 s ± 0.061 s 1.904 s … 2.131 s +5.5%
Memory 90.8 MiB ± 0.2 MiB 90.4 MiB … 91.4 MiB 0.0%
Benchmark 1: brew autoremove --dry-run # 62a9ad2642 (15 runs)
mean ± σ min … max
Wall Time 1.423 s ± 0.051 s 1.387 s … 1.594 s
Memory 85.3 MiB ± 0.2 MiB 85.0 MiB … 85.7 MiB
Benchmark 2: brew autoremove --dry-run # uninstall-build-dependents (15 runs)
mean ± σ min … max
Wall Time 1.422 s ± 0.023 s 1.388 s … 1.462 s -0.1%
Memory 85.4 MiB ± 0.2 MiB 85.0 MiB … 85.8 MiB +0.1%
From Claude: "Your machine is close to the worst case: 96 of your 117 formulae are built from source, so almost every build dependency gets looked up. A machine that pours bottles skips most of that work."
- Look up each build dependency once per call, as many formulae share
them. With 117 installed formulae this takes 34 lookups instead of
143 and the `brew autoremove` rule drops from about 70 to 40 ms.
- Only list the kegs of formulae that are being uninstalled. Together
the `brew uninstall` check drops from about 80 to 20 ms.
- Refuse to remove a formula's last keg that others need to build even
when `opt` does not point to it, as `brew autoremove` keeps it and a
build links it again.
- An older keg can still go while another keg stays.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
brew autoremovekeeps the build dependencies of formulae that were built from source, because the next upgrade needs them again and they take a while to install.brew uninstallhas no such check.brew uninstall cmakeremoves it without a word, even when the installed formulae that need it to build have no bottle, and the next upgrade of any of them then starts by building cmake again.brew uninstallnow refuses in that case, as it does for runtime dependents, with the same--ignore-dependenciesoverride:The rule is autoremove's own. Its keep logic moves from
Utils::AutoremoveintoInstalledDependents, whereuninstallalready looks up runtime dependents, so that both commands share it.uninstallrefuses exactly the build dependencies thatautoremovekeeps, and autoremove's own uninstall step cannot trip over the new check. Build dependencies are read from the current formula, so one that only the installed version needed does not block. Nothing changes where every formula was poured and still has a bottle, because pouring does not use build dependencies.brew bundle cleanuphad its own, narrower copy of the keep rule and hands its whole list to onebrew uninstall, which the new check would refuse. It now asksInstalledDependents.formulae_safe_to_removeinstead. The receipt lookup thatautoremoveandleaveseach spelled out moves intoFormula#installed_runtime_dependency_names.Benchmark with hyperfine (what
brew benchmark --execruns), 3 warmups and 15 runs each, on 117 installed formulae of which 96 were built from source:main(62a9ad2)brew uninstall --dry-run shfmtbrew autoremove --dry-runThe new check costs about 20 ms here. The shared rule now looks up each build dependency once (34 lookups instead of 143), which makes up for it because
uninstallruns the autoremove rule afterwards too.brew benchmarkresults.brewcommands to reproduce the bug?brew lgtm(style, typechecking and tests) locally?Claude Code (Fable 5.2) made the code and spec changes from my instructions. It also ran the checks on my machine:
brew lgtm, andbrew uninstall --dry-runfrom this branch against my installed kegs for the output above. I reviewed the diff.