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

Conversation

amomchilov
Copy link

Fixes #739

Didn't change any tests, because I couldn't find any tests that covered this behaviour.

Comment on lines 320 to 326
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
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
@@ -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.
@amomchilov amomchilov changed the title Shush the backtrace_location, too May 2, 2025
@amomchilov amomchilov force-pushed the amomchilov/shush-backtrace_locations branch from 5bc96f5 to 774c1d5 Compare May 2, 2025 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant