brew/Library/Homebrew/utils/backtrace.rb
apainintheneck 85bd4c7e1f utils/backtrace: scrub sorbet-runtime from backtrace
Ever since we started using this at runtime it's been polluting
the backtrace output. This makes it harder to debug errors and
increases the amount of info users have to paste into the box
when filing an issue.

This is a very direct approach. Essentially, we strip out
everything related to the `sorbet-runtime` gem whenever the top
line in the backtrace is unrelated to sorbet-runtime.

The hope is that this will allow errors related to sorbet to
be diagnosed easily while also reducing the backtrace size
for all other types of errors.

Sometimes it is useful to see the full backtrace though.
For those cases, we include the full backtrace when
`--verbose` is passed in and print a warning that the
Sorbet lines have been removed from the backtrace the
first time they are removed.

Note: This requires gems to be set up so that the call to
`Gem.paths.home` works correctly. For that reason, it must
be included after `utils/gems` which is included in
`standalone/load_path` already.
2023-09-21 21:07:22 -07:00

37 lines
1.2 KiB
Ruby

# typed: true
# frozen_string_literal: true
module Utils
module Backtrace
# Cleans `sorbet-runtime` gem paths from the backtrace unless...
# 1. `verbose` is set
# 2. first backtrace line starts with `sorbet-runtime`
# - This implies that the error is related to Sorbet.
sig { params(error: Exception).returns(T.nilable(T::Array[String])) }
def self.clean(error)
backtrace = error.backtrace
return backtrace if Context.current.verbose?
return backtrace if backtrace.blank?
return backtrace if backtrace.fetch(0).start_with?(sorbet_runtime_path)
old_backtrace_length = backtrace.length
backtrace.reject { |line| line.start_with?(sorbet_runtime_path) }
.tap { |new_backtrace| print_backtrace_message if old_backtrace_length > new_backtrace.length }
end
def self.sorbet_runtime_path
@sorbet_runtime_path ||= "#{Gem.paths.home}/gems/sorbet-runtime"
end
def self.print_backtrace_message
return if @print_backtrace_message
opoo "Removed Sorbet lines from backtrace!"
puts "Rerun with --verbose to see the original backtrace" unless Homebrew::EnvConfig.no_env_hints?
@print_backtrace_message = true
end
end
end