From 11d6785beab1eed09de63ab9b122d4f01b8566e0 Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Mon, 15 Jul 2024 13:44:01 -0400 Subject: [PATCH] Add utils/backtrace requires This is primarily intended to resolve the `uninitialized constant Utils::Backtrace` error in `formula_versions.rb:60` but I expanded it to try to cover all existing usage of `Utils::Backtrace`. I've followed the existing pattern, where `utils/backtrace` is required in the context of where it's used. Many of these cases use `Backtrace` in a conditional manner, so I've tried to ensure that the `require` follows suit. --- Library/Homebrew/brew.rb | 12 ++++++++---- Library/Homebrew/cmd/update-report.rb | 10 ++++++++-- Library/Homebrew/formula_installer.rb | 18 +++++++++++++++++- Library/Homebrew/formula_versions.rb | 2 ++ Library/Homebrew/livecheck/livecheck.rb | 10 ++++++++-- Library/Homebrew/migrator.rb | 10 ++++++++-- Library/Homebrew/utils/repology.rb | 1 + 7 files changed, 52 insertions(+), 11 deletions(-) diff --git a/Library/Homebrew/brew.rb b/Library/Homebrew/brew.rb index fe2a2df7a6e14..16059f829cf12 100644 --- a/Library/Homebrew/brew.rb +++ b/Library/Homebrew/brew.rb @@ -147,8 +147,10 @@ Homebrew::Help.help cmd, remaining_args: args&.remaining, usage_error: e.message rescue SystemExit => e onoe "Kernel.exit" if args&.debug? && !e.success? - require "utils/backtrace" - $stderr.puts Utils::Backtrace.clean(e) if args&.debug? || ARGV.include?("--debug") + if args&.debug? || ARGV.include?("--debug") + require "utils/backtrace" + $stderr.puts Utils::Backtrace.clean(e) + end raise rescue Interrupt $stderr.puts # seemingly a newline is typical @@ -185,8 +187,10 @@ raise if e.message.empty? onoe e - require "utils/backtrace" - $stderr.puts Utils::Backtrace.clean(e) if args&.debug? || ARGV.include?("--debug") + if args&.debug? || ARGV.include?("--debug") + require "utils/backtrace" + $stderr.puts Utils::Backtrace.clean(e) + end exit 1 rescue Exception => e # rubocop:disable Lint/RescueException diff --git a/Library/Homebrew/cmd/update-report.rb b/Library/Homebrew/cmd/update-report.rb index a76484e158883..7bd939f1ffc6a 100644 --- a/Library/Homebrew/cmd/update-report.rb +++ b/Library/Homebrew/cmd/update-report.rb @@ -174,7 +174,10 @@ def output_update_report begin reporter = Reporter.new(tap) rescue Reporter::ReporterRevisionUnsetError => e - onoe "#{e.message}\n#{Utils::Backtrace.clean(e)&.join("\n")}" if Homebrew::EnvConfig.developer? + if Homebrew::EnvConfig.developer? + require "utils/backtrace" + onoe "#{e.message}\n#{Utils::Backtrace.clean(e)&.join("\n")}" + end next end if reporter.updated? @@ -624,7 +627,10 @@ def migrate_tap_migration system HOMEBREW_BREW_FILE, "link", new_full_name, "--overwrite" end rescue Exception => e # rubocop:disable Lint/RescueException - onoe "#{e.message}\n#{Utils::Backtrace.clean(e)&.join("\n")}" if Homebrew::EnvConfig.developer? + if Homebrew::EnvConfig.developer? + require "utils/backtrace" + onoe "#{e.message}\n#{Utils::Backtrace.clean(e)&.join("\n")}" + end end next end diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb index 8e6b56049516b..423d43a7a5001 100644 --- a/Library/Homebrew/formula_installer.rb +++ b/Library/Homebrew/formula_installer.rb @@ -1034,7 +1034,12 @@ def link(keg) ofail "An unexpected error occurred during the `brew link` step" puts "The formula built, but is not symlinked into #{HOMEBREW_PREFIX}" puts e - puts Utils::Backtrace.clean(e) if debug? + + if debug? + require "utils/backtrace" + puts Utils::Backtrace.clean(e) + end + @show_summary_heading = true ignore_interrupts do keg.unlink @@ -1080,6 +1085,8 @@ def install_service rescue Exception => e # rubocop:disable Lint/RescueException puts e ofail "Failed to install service files" + + require "utils/backtrace" odebug e, Utils::Backtrace.clean(e) end @@ -1090,7 +1097,10 @@ def fix_dynamic_linkage(keg) ofail "Failed to fix install linkage" puts "The formula built, but you may encounter issues using it or linking other" puts "formulae against it." + + require "utils/backtrace" odebug e, Utils::Backtrace.clean(e) + @show_summary_heading = true end @@ -1101,7 +1111,10 @@ def clean rescue Exception => e # rubocop:disable Lint/RescueException opoo "The cleaning step did not complete successfully" puts "Still, the installation was successful, so we will link it into your prefix." + + require "utils/backtrace" odebug e, Utils::Backtrace.clean(e) + Homebrew.failed = true @show_summary_heading = true end @@ -1171,7 +1184,10 @@ def post_install opoo "The post-install step did not complete successfully" puts "You can try again using:" puts " brew postinstall #{formula.full_name}" + + require "utils/backtrace" odebug e, Utils::Backtrace.clean(e), always_display: Homebrew::EnvConfig.developer? + Homebrew.failed = true @show_summary_heading = true end diff --git a/Library/Homebrew/formula_versions.rb b/Library/Homebrew/formula_versions.rb index d7c7878a2cd4d..be56bb1bccd9f 100644 --- a/Library/Homebrew/formula_versions.rb +++ b/Library/Homebrew/formula_versions.rb @@ -55,6 +55,8 @@ def formula_at_revision(revision, formula_relative_path = relative_path, &_block nostdout { Formulary.from_contents(name, path, contents, ignore_errors: true) } end rescue *IGNORED_EXCEPTIONS => e + require "utils/backtrace" + # We rescue these so that we can skip bad versions and # continue walking the history odebug "#{e} in #{name} at revision #{revision}", Utils::Backtrace.clean(e) diff --git a/Library/Homebrew/livecheck/livecheck.rb b/Library/Homebrew/livecheck/livecheck.rb index faec077d2d2a5..eca212bea582b 100644 --- a/Library/Homebrew/livecheck/livecheck.rb +++ b/Library/Homebrew/livecheck/livecheck.rb @@ -409,7 +409,10 @@ def run_checks( name += " (cask)" if ambiguous_casks.include?(formula_or_cask) onoe "#{Tty.blue}#{name}#{Tty.reset}: #{e}" - $stderr.puts Utils::Backtrace.clean(e) if debug && !e.is_a?(Livecheck::Error) + if debug && !e.is_a?(Livecheck::Error) + require "utils/backtrace" + $stderr.puts Utils::Backtrace.clean(e) + end print_resources_info(resource_version_info, verbose:) if check_for_resources nil end @@ -1056,7 +1059,10 @@ def resource_version( status_hash(resource, "error", [e.to_s], verbose:) elsif !quiet onoe "#{Tty.blue}#{resource.name}#{Tty.reset}: #{e}" - $stderr.puts Utils::Backtrace.clean(e) if debug && !e.is_a?(Livecheck::Error) + if debug && !e.is_a?(Livecheck::Error) + require "utils/backtrace" + $stderr.puts Utils::Backtrace.clean(e) + end nil end end diff --git a/Library/Homebrew/migrator.rb b/Library/Homebrew/migrator.rb index 9da558d5f990d..ce40e3c9d34fd 100644 --- a/Library/Homebrew/migrator.rb +++ b/Library/Homebrew/migrator.rb @@ -226,7 +226,10 @@ def migrate rescue Exception => e # rubocop:disable Lint/RescueException onoe "The migration did not complete successfully." puts e - puts Utils::Backtrace.clean(e) if debug? + if debug? + require "utils/backtrace" + puts Utils::Backtrace.clean(e) + end puts "Backing up..." ignore_interrupts { backup_oldname } ensure @@ -357,7 +360,10 @@ def link_newname rescue Exception => e # rubocop:disable Lint/RescueException onoe "An unexpected error occurred during linking" puts e - puts Utils::Backtrace.clean(e) if debug? + if debug? + require "utils/backtrace" + puts Utils::Backtrace.clean(e) + end ignore_interrupts { new_keg.unlink(verbose: verbose?) } raise end diff --git a/Library/Homebrew/utils/repology.rb b/Library/Homebrew/utils/repology.rb index 62095821d6800..cd139933236bc 100644 --- a/Library/Homebrew/utils/repology.rb +++ b/Library/Homebrew/utils/repology.rb @@ -36,6 +36,7 @@ def self.single_package_query(name, repository:) data = JSON.parse(output) { name => data } rescue => e + require "utils/backtrace" error_output = [errors, "#{e.class}: #{e}", Utils::Backtrace.clean(e)].compact if Homebrew::EnvConfig.developer? $stderr.puts(*error_output)