From 779d90082d03aa2fbf98a02d4461767854850134 Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Sat, 21 Oct 2023 17:15:26 -0700 Subject: [PATCH] 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 --- Library/Homebrew/cli/named_args.rb | 3 ++- Library/Homebrew/cli/parser.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/cli/named_args.rb b/Library/Homebrew/cli/named_args.rb index e26a8d15f0b35..c6f6f6097628f 100644 --- a/Library/Homebrew/cli/named_args.rb +++ b/Library/Homebrew/cli/named_args.rb @@ -140,7 +140,8 @@ def load_formula_or_cask(name, only: nil, method: nil, warn: nil) 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 diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index c667283ac0784..993df1a3e88ee 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -653,7 +653,7 @@ def formulae(argv) begin Formulary.factory(arg, spec, flags: argv.select { |a| a.start_with?("--") }) - rescue FormulaUnavailableError + rescue FormulaUnavailableError, FormulaSpecificationError nil end end.compact.uniq(&:name)