Skip to content

uninstall: refuse to remove build dependencies - #24713

Merged
MikeMcQuaid merged 9 commits into
mainfrom
uninstall-build-dependents
Nov 22, 2026
Merged

MikeMcQuaid merged 9 commits into
mainfrom
uninstall-build-dependents

Conversation

@HaraldNordgren

@HaraldNordgren HaraldNordgren commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

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 cmake
Error: Refusing to uninstall /usr/local/Cellar/cmake/4.4.4
because 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:

Command main (62a9ad2) This branch
brew uninstall --dry-run shfmt 1.954 s ± 0.112 s 1.833 s ± 0.017 s
brew autoremove --dry-run 1.443 s ± 0.013 s 1.424 s ± 0.013 s

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 followed our Contributing guidelines?
  • 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.


@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

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.
Build-dependent uninstall checks
Library/Homebrew/installed_dependents.rb, Library/Homebrew/dependents_message.rb, Library/Homebrew/uninstall.rb, Library/Homebrew/test/uninstall_spec.rb, Library/Homebrew/test/installed_dependents_spec.rb
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 integration
Library/Homebrew/bundle/subcommand/cleanup.rb, Library/Homebrew/bundle/brew.rb, Library/Homebrew/test/cmd/bundle/cleanup_subcommand_spec.rb, Library/Homebrew/test/bundle/brew_spec.rb
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: Passed 4

Check name Status Explanation
Title check Passed 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 Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed 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.

Files selected for processing: 2

  • Library/Homebrew/installed_dependents.rb
  • Library/Homebrew/test/installed_dependents_spec.rb

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

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: e30a33b0-8dc5-4827-8b42-8d51b011f9bd
Commits

Reviewing files that changed from the base of the PR and between 46af3f4 and 2dde3be.

Files selected for processing: 17

  • Library/Homebrew/bundle/brew.rb
  • Library/Homebrew/bundle/subcommand/cleanup.rb
  • Library/Homebrew/cleanup.rb
  • Library/Homebrew/cmd/leaves.rb
  • Library/Homebrew/dependents_message.rb
  • Library/Homebrew/formula.rb
  • Library/Homebrew/installed_dependents.rb
  • Library/Homebrew/test/bundle/brew_spec.rb
  • Library/Homebrew/test/cleanup_spec.rb
  • Library/Homebrew/test/cmd/bundle/cleanup_subcommand_spec.rb
  • Library/Homebrew/test/cmd/leaves_spec.rb
  • Library/Homebrew/test/formula_spec.rb
  • Library/Homebrew/test/installed_dependents_spec.rb
  • Library/Homebrew/test/uninstall_spec.rb
  • Library/Homebrew/test/utils/autoremove_spec.rb
  • Library/Homebrew/uninstall.rb
  • Library/Homebrew/utils/autoremove.rb
Files with no reviewable changes: 3

  • Library/Homebrew/bundle/brew.rb
  • Library/Homebrew/test/bundle/brew_spec.rb
  • Library/Homebrew/test/utils/autoremove_spec.rb

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@HaraldNordgren
HaraldNordgren force-pushed the uninstall-build-dependents branch from 2dde3be to 8ecbdc0 Compare November 20, 2026 16:21

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

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: 14d6fd6c-9cab-4958-9f75-02c95e4fc53a
Commits

Reviewing files that changed from the base of the PR and between 2dde3be and 8ecbdc0.

Files selected for processing: 8

  • Library/Homebrew/bundle/subcommand/cleanup.rb
  • Library/Homebrew/formula.rb
  • Library/Homebrew/installed_dependents.rb
  • Library/Homebrew/test/cmd/bundle/cleanup_subcommand_spec.rb
  • Library/Homebrew/test/formula_spec.rb
  • Library/Homebrew/test/installed_dependents_spec.rb
  • Library/Homebrew/test/uninstall_spec.rb
  • Library/Homebrew/uninstall.rb

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@HaraldNordgren
HaraldNordgren force-pushed the uninstall-build-dependents branch from 8ecbdc0 to 37e0355 Compare November 20, 2026 17:28

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

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: a3403272-d837-4a8c-a72f-c8fab3812d2a
Commits

Reviewing files that changed from the base of the PR and between 8ecbdc0 and 37e0355.

Files selected for processing: 2

  • Library/Homebrew/installed_dependents.rb
  • Library/Homebrew/test/installed_dependents_spec.rb

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@HaraldNordgren
HaraldNordgren force-pushed the uninstall-build-dependents branch from 37e0355 to d323516 Compare November 20, 2026 20:08

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Additional comments: 1

Library/Homebrew/installed_dependents.rb-160-160: 1

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.

Files selected for processing: 4

  • Library/Homebrew/bundle/subcommand/cleanup.rb
  • Library/Homebrew/installed_dependents.rb
  • Library/Homebrew/test/cmd/bundle/cleanup_subcommand_spec.rb
  • Library/Homebrew/test/installed_dependents_spec.rb

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@HaraldNordgren
HaraldNordgren force-pushed the uninstall-build-dependents branch from d323516 to 79fa1c0 Compare November 20, 2026 22:06

@MikeMcQuaid MikeMcQuaid left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Almost there. Can you benchmark uninstall/autoremove performance before and after?

Comment thread Library/Homebrew/dependents_message.rb Resolved
- `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.
@HaraldNordgren
HaraldNordgren force-pushed the uninstall-build-dependents branch from 79fa1c0 to 34c8764 Compare November 21, 2026 18:41

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

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: 5e9cb75c-4b3a-472e-9122-ae8fcfd0f5f5
Commits

Reviewing files that changed from the base of the PR and between 79fa1c0 and 34c8764.

Files selected for processing: 7

  • Library/Homebrew/bundle/subcommand/cleanup.rb
  • Library/Homebrew/cleanup.rb
  • Library/Homebrew/dependents_message.rb
  • Library/Homebrew/installed_dependents.rb
  • Library/Homebrew/test/formula_spec.rb
  • Library/Homebrew/test/installed_dependents_spec.rb
  • Library/Homebrew/utils/autoremove.rb

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@HaraldNordgren

HaraldNordgren commented Nov 21, 2026 •

Copy link
Copy Markdown
Member Author

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) &&
for cmd in '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."

But I'll see what I can do about the speed!

- 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.
@HaraldNordgren

Copy link
Copy Markdown
Member Author

Wow, refactored it an now both are faster than baseline!

▶ cd /usr/local/Homebrew &&
base=$(git merge-base origin/main uninstall-build-dependents | cut -c1-10) &&
for cmd in '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.910 s   ±   0.043 s      1.863 s   …   2.038 s
  Memory        90.8 MiB ±     0.4 MiB     90.3 MiB …    91.6 MiB

Benchmark 2: brew uninstall --dry-run shfmt # uninstall-build-dependents (15 runs)
                mean     ±       σ          min     …     max
  Wall Time    1.838 s   ±   0.030 s      1.782 s   …   1.902 s             -3.8%
  Memory        90.8 MiB ±     0.4 MiB     90.3 MiB …    91.6 MiB           -0.1%

Benchmark 1: brew autoremove --dry-run # 62a9ad2642 (15 runs)
                mean     ±       σ          min     …     max
  Wall Time    1.494 s   ±   0.109 s      1.408 s   …   1.867 s
  Memory        83.0 MiB ±     0.8 MiB     82.4 MiB …    85.5 MiB

Benchmark 2: brew autoremove --dry-run # uninstall-build-dependents (15 runs)
                mean     ±       σ          min     …     max
  Wall Time    1.444 s   ±   0.029 s      1.402 s   …   1.497 s             -3.3%
  Memory        84.1 MiB ±     1.2 MiB     82.4 MiB …    85.5 MiB           +1.4%

- 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.

@MikeMcQuaid MikeMcQuaid left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@MikeMcQuaid
MikeMcQuaid added this pull request to the merge queue Nov 22, 2026
Merged via the queue into main with commit 5427312 Nov 22, 2026
56 checks passed
@MikeMcQuaid
MikeMcQuaid deleted the uninstall-build-dependents branch November 22, 2026 12:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants