Skip to content

Shush the backtrace_locations, too #740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/spring/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,10 @@ def shush_backtraces
ensure
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found this confusing. Is there any reason to use ensure + $!, rather than just rescue => e + raise e?

I thought perhaps because a re-raise would introduce a new frame into the backtrace, but it doesn't

demo
#!/opt/rubies/3.4.2/bin/ruby

begin
 raise
rescue => e
  raise e
end

# Prints:
# demo.rb:4:in '<main>': unhandled exception

# As you see, line 5 and 6 aren't in the backtrace.
if $!
lib = File.expand_path("..", __FILE__)
$!.backtrace.reject! { |line| line.start_with?(lib) } unless $!.backtrace.frozen?
unless $!.backtrace.frozen?
$!.backtrace.reject! { |line| line.start_with?(lib) }
$!.backtrace_locations.reject! { |line| line.path.start_with?(lib) }
end
end
Comment on lines 320 to 326
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we feel about giving $! a more reasonable name?

Suggested change
if $!
lib = File.expand_path("..", __FILE__)
$!.backtrace.reject! { |line| line.start_with?(lib) } unless $!.backtrace.frozen?
unless $!.backtrace.frozen?
$!.backtrace.reject! { |line| line.start_with?(lib) }
$!.backtrace_locations.reject! { |line| line.path.start_with?(lib) }
end
end
error = $!
if error
lib = File.expand_path("..", __FILE__)
unless error.backtrace.frozen?
error.backtrace.reject! { |line| line.start_with?(lib) }
error.backtrace_locations.reject! { |line| line.path.start_with?(lib) }
end
end
end
end
Expand All @@ -330,7 +333,10 @@ def shush_backtraces
ensure
if $!
lib = File.expand_path("..", __FILE__)
$!.backtrace.reject! { |line| line.start_with?(lib) } unless $!.backtrace.frozen?
unless $!.backtrace.frozen?
$!.backtrace.reject! { |line| line.start_with?(lib) }
$!.backtrace_locations.reject! { |line| line.path.start_with?(lib) }
end
end
end
end
Expand Down