-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16014 from apainintheneck/scrub-sorbet-runtime-fr…
…om-backtrace utils/backtrace: scrub sorbet-runtime from backtrace
- Loading branch information
Showing
13 changed files
with
147 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# frozen_string_literal: true | ||
|
||
require "utils/backtrace" | ||
|
||
describe Utils::Backtrace do | ||
let(:backtrace_no_sorbet_paths) do | ||
[ | ||
"/Library/Homebrew/downloadable.rb:75:in", | ||
"/Library/Homebrew/downloadable.rb:50:in", | ||
"/Library/Homebrew/cmd/fetch.rb:236:in", | ||
"/Library/Homebrew/cmd/fetch.rb:201:in", | ||
"/Library/Homebrew/cmd/fetch.rb:178:in", | ||
"/Library/Homebrew/simulate_system.rb:29:in", | ||
"/Library/Homebrew/cmd/fetch.rb:166:in", | ||
"/Library/Homebrew/cmd/fetch.rb:163:in", | ||
"/Library/Homebrew/cmd/fetch.rb:163:in", | ||
"/Library/Homebrew/cmd/fetch.rb:94:in", | ||
"/Library/Homebrew/cmd/fetch.rb:94:in", | ||
"/Library/Homebrew/brew.rb:94:in", | ||
] | ||
end | ||
|
||
let(:backtrace_with_sorbet_paths) do | ||
[ | ||
"/Library/Homebrew/downloadable.rb:75:in", | ||
"/Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/sorbet-runtime-0.5.10461/lib/call_validation.rb:157:in", | ||
"/Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/sorbet-runtime-0.5.10461/lib/call_validation.rb:157:in", | ||
"/Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/sorbet-runtime-0.5.10461/lib/_methods.rb:270:in", | ||
"/Library/Homebrew/downloadable.rb:50:in", | ||
"/Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/sorbet-runtime-0.5.10461/lib/call_validation.rb:157:in", | ||
"/Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/sorbet-runtime-0.5.10461/lib/call_validation.rb:157:in", | ||
"/Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/sorbet-runtime-0.5.10461/lib/_methods.rb:270:in", | ||
"/Library/Homebrew/cmd/fetch.rb:236:in", | ||
"/Library/Homebrew/cmd/fetch.rb:201:in", | ||
"/Library/Homebrew/cmd/fetch.rb:178:in", | ||
"/Library/Homebrew/simulate_system.rb:29:in", | ||
"/Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/sorbet-runtime-0.5.10461/lib/call_validation.rb:157:in", | ||
"/Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/sorbet-runtime-0.5.10461/lib/call_validation.rb:157:in", | ||
"/Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/sorbet-runtime-0.5.10461/lib/_methods.rb:270:in", | ||
"/Library/Homebrew/cmd/fetch.rb:166:in", | ||
"/Library/Homebrew/cmd/fetch.rb:163:in", | ||
"/Library/Homebrew/cmd/fetch.rb:163:in", | ||
"/Library/Homebrew/cmd/fetch.rb:94:in", | ||
"/Library/Homebrew/cmd/fetch.rb:94:in", | ||
"/Library/Homebrew/brew.rb:94:in", | ||
] | ||
end | ||
|
||
let(:backtrace_with_sorbet_error) do | ||
backtrace_with_sorbet_paths.drop(1) | ||
end | ||
|
||
def exception_with(backtrace:) | ||
exception = StandardError.new | ||
exception.set_backtrace(backtrace) if backtrace | ||
exception | ||
end | ||
|
||
before do | ||
allow(described_class).to receive(:sorbet_runtime_path) | ||
.and_return("/Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/sorbet-runtime") | ||
allow(Context).to receive(:current).and_return(Context::ContextStruct.new(verbose: false)) | ||
end | ||
|
||
it "handles nil backtrace" do | ||
exception = exception_with backtrace: nil | ||
expect(described_class.clean(exception)).to be_nil | ||
end | ||
|
||
it "handles empty array backtrace" do | ||
exception = exception_with backtrace: [] | ||
expect(described_class.clean(exception)).to eq [] | ||
end | ||
|
||
it "removes sorbet paths when top error is not from sorbet" do | ||
exception = exception_with backtrace: backtrace_with_sorbet_paths | ||
expect(described_class.clean(exception)).to eq backtrace_no_sorbet_paths | ||
end | ||
|
||
it "includes sorbet paths when top error is not from sorbet and verbose is set" do | ||
allow(Context).to receive(:current).and_return(Context::ContextStruct.new(verbose: true)) | ||
exception = exception_with backtrace: backtrace_with_sorbet_paths | ||
expect(described_class.clean(exception)).to eq backtrace_with_sorbet_paths | ||
end | ||
|
||
it "doesn't change backtrace when error is from sorbet" do | ||
exception = exception_with backtrace: backtrace_with_sorbet_error | ||
expect(described_class.clean(exception)).to eq backtrace_with_sorbet_error | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters