ruby / irb

interactive Ruby
BSD 2-Clause "Simplified" License
393 stars 119 forks source link

Filter backtrace before formatting in #handle_exception #916

Closed joshbroughton closed 8 months ago

joshbroughton commented 8 months ago

Why is this change needed?

The current implementation of handle_exception formats the exception backtrace using Exception#full_message prior to applying filter_backtrace on split lines of the full_message. Other projects that use irb intuitively expect that backtrace filtering will act on objects that conform to the Exception#backtrace API, and this discrepancy has introduced a bug in Rails where backtraces in the Rails console are entirely silenced (https://github.com/rails/rails/issues/49259)

Closes #913

Why this solution?

We considered working around this in Rails, but the backtrace_cleaner class in Rails is not only used for the console; it still needs to work in other contexts where backtrace cleaning is actually being applied to Exception#backtrace.

This proposed change is the least amount of change to fix the behaviour - the filter now acts on Exception#backtrace, but the rest of the logic for formatting and order remains unchanged and relies on Exception#full_message. This seems to be a reasonable use case for Exception#full_message and avoids needing to rewrite the formatting and ordering logic that is already working.

The filtering previously did not apply to irb bugs (it was within unless irb_bug), so it was added to the else branch of the irb_bug conditional to preserve that behaviour.

Testing

This branch passes the tests run with bundle exec rake. It looks like handle_exception is tested by the tests eval_input within test_context.rb, which I briefly looked over and the test coverage seems good to me. This is my first contribution to irb, please let me know if there are other testing requirements.

With respect to fixing the bug in Rails, here is a screenshot showing the correct/expected backtrace in the Rails console in a fresh Rails test app pointing to the version of irb proposed in this PR.

Screenshot 2024-04-03 at 11 17 05 AM
tompng commented 8 months ago

Thank you! looks good :+1: