From f87e60f1f4fd9e48f78c9a544109b225b4b2bc20 Mon Sep 17 00:00:00 2001 From: Joshua Broughton Date: Fri, 5 Apr 2024 10:25:45 -0600 Subject: [PATCH] [ruby/irb] Filter backtrace before format in handle_exception (https://github.com/ruby/irb/pull/916) handle_exception now applies the filter_backtrace to exception backtraces prior to formatting the lines with Exception#full_message This fixes a bug in upstream projects, notably Rails, where the backtrace filtering logic expects the lines to be formatted as Exception#backtrace. https://github.com/ruby/irb/commit/805ee008f9 Co-authored-by: Hartley McGuire --- lib/irb.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/irb.rb b/lib/irb.rb index c7d36e7445..0855c59de0 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -1222,6 +1222,13 @@ module IRB irb_bug = true else irb_bug = false + # This is mostly to make IRB work nicely with Rails console's backtrace filtering, which patches WorkSpace#filter_backtrace + # In such use case, we want to filter the exception's backtrace before its displayed through Exception#full_message + # And we clone the exception object in order to avoid mutating the original exception + # TODO: introduce better API to expose exception backtrace externally + backtrace = exc.backtrace.map { |l| @context.workspace.filter_backtrace(l) }.compact + exc = exc.clone + exc.set_backtrace(backtrace) end if RUBY_VERSION < '3.0.0' @@ -1246,7 +1253,6 @@ module IRB lines = m.split("\n").reverse end unless irb_bug - lines = lines.map { |l| @context.workspace.filter_backtrace(l) }.compact if lines.size > @context.back_trace_limit omit = lines.size - @context.back_trace_limit lines = lines[0..(@context.back_trace_limit - 1)]