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.
This commit is contained in:
apainintheneck 2022-09-09 21:54:32 -07:00
parent 2d0c32fa91
commit ae17d3cffd
2 changed files with 12 additions and 3 deletions

View File

@ -37,7 +37,7 @@ module Homebrew
def leaves def leaves
args = leaves_args.parse 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_on_request?)) if args.installed_on_request?
leaves_list.select!(&method(:installed_as_dependency?)) if args.installed_as_dependency? leaves_list.select!(&method(:installed_as_dependency?)) if args.installed_as_dependency?

View File

@ -1853,12 +1853,21 @@ class Formula
.flat_map { |f| [f, *f.runtime_formula_dependencies].compact } .flat_map { |f| [f, *f.runtime_formula_dependencies].compact }
end 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 # @private
def self.formulae_with_no_formula_dependents(formulae) def self.formulae_with_no_formula_dependents(formulae)
return [] if formulae.blank? 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 end
# Recursive function that returns an array of {Formula} without # Recursive function that returns an array of {Formula} without