cli/parser: rescue formula specification errors

This came up recently where an outdated formula definition
caused the program to crash with an ambiguous message when
a user wanted to upgrade a cask instead. Catching these errors
allows them to get handled later on improving error messages
and defaults. Now if the only formula with the given name is
invalid it will default to using the cask unless --formula is
specified.

- https://github.com/Homebrew/brew/issues/16123
This commit is contained in:
apainintheneck 2023-10-21 17:15:26 -07:00
parent 46a8310390
commit 779d90082d
2 changed files with 3 additions and 2 deletions

View File

@ -140,7 +140,8 @@ module Homebrew
warn_if_cask_conflicts(name, "formula") if only != :formula
return formula
rescue FormulaUnreadableError, FormulaClassUnavailableError,
TapFormulaUnreadableError, TapFormulaClassUnavailableError => e
TapFormulaUnreadableError, TapFormulaClassUnavailableError,
FormulaSpecificationError => e
# Need to rescue before `FormulaUnavailableError` (superclass of this)
# The formula was found, but there's a problem with its implementation
unreadable_error ||= e

View File

@ -653,7 +653,7 @@ module Homebrew
begin
Formulary.factory(arg, spec, flags: argv.select { |a| a.start_with?("--") })
rescue FormulaUnavailableError
rescue FormulaUnavailableError, FormulaSpecificationError
nil
end
end.compact.uniq(&:name)