From ae17d3cffd6ad1c7c855f456190e1474f8b774ff Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Fri, 9 Sep 2022 21:54:32 -0700 Subject: [PATCH] autoremove: ignore build deps when built from src Now the build dependencies of formula that were installed from source will not be removed by `brew autoremove`. This is especially helpful for those who've installed brew in an alternative prefix. --- Library/Homebrew/cmd/leaves.rb | 2 +- Library/Homebrew/formula.rb | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/cmd/leaves.rb b/Library/Homebrew/cmd/leaves.rb index ec134de4a1..621d64ae84 100644 --- a/Library/Homebrew/cmd/leaves.rb +++ b/Library/Homebrew/cmd/leaves.rb @@ -37,7 +37,7 @@ module Homebrew def leaves args = leaves_args.parse - leaves_list = Formula.formulae_with_no_formula_dependents(Formula.installed) + leaves_list = Formula.installed - Formula.installed.flat_map(&:runtime_formula_dependencies) leaves_list.select!(&method(:installed_on_request?)) if args.installed_on_request? leaves_list.select!(&method(:installed_as_dependency?)) if args.installed_as_dependency? diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 269e689c76..7279db522b 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -1853,12 +1853,21 @@ class Formula .flat_map { |f| [f, *f.runtime_formula_dependencies].compact } end - # An array of all installed {Formula} without {Formula} dependents + # An array of all installed {Formula} without runtime {Formula} + # dependents for bottles and without build {Formula} dependents + # for those built from source. # @private def self.formulae_with_no_formula_dependents(formulae) return [] if formulae.blank? - formulae - formulae.flat_map(&:runtime_formula_dependencies) + formulae - formulae.each_with_object([]) do |formula, dependents| + dependents.concat(formula.runtime_formula_dependencies) + + # Include build dependencies when the formula is not a bottle + unless Tab.for_keg(formula.any_installed_keg).poured_from_bottle + dependents.concat(formula.deps.select(&:build?).map(&:to_formula)) + end + end end # Recursive function that returns an array of {Formula} without