From: "kjtsanaktsidis (KJ Tsanaktsidis)" Date: 2022-11-10T13:50:45+00:00 Subject: [ruby-core:110691] [Ruby master Feature#19117] Include the method owner in backtraces, not just the method name Issue #19117 has been updated by kjtsanaktsidis (KJ Tsanaktsidis). +1 to this! I also literally just opened https://bugs.ruby-lang.org/issues/19119 tonight as well which touches on this (as well as some other things) - but maybe to keep the discussion here, some fun cases to consider include Refinements, singleton classes (which are not singleton classes of a class, but rather of a non-class object), and anonymous classes. I think the current implementation of `rb_class_path` will print a string like "#" or "#" in at least some situations like this. This is not all that useful IMO, and those addresses are not even stable across the lifetime of a single program (they're just `%p` formatting the `VALUE`, which will change if the object gets compacted, right?). The other reason to care about having addresses like that in names - it will break aggregation of stack frames in e.g. profiling tools, or exception trackers. If you have a function like `def foo(n); def n.bork; raise "bork"; end; n.bork; end;` it would come up with a new stack frame every time in e.g. Sentry or Rollbar or what have you. Instead, I would propose printing something like... (shamelessly stolen mostly from Backtracie)... * `"#"` for a refinement module adding methods to Bar * `"#"` for a particular instance of Foo * `"#"` for Foo's singleton class. * `"#"` for an anonymous subclass * The usual classpath string otherwise These three commits show what that might look like - * https://github.com/ruby/ruby/pull/6706/commits/a513e8e2cca0e23223d6af861d9f53f60cf36608 * https://github.com/ruby/ruby/pull/6706/commits/2865a8589b82ab7f5cc5a87dd58cb0b1bde4f8a4 * https://github.com/ruby/ruby/pull/6706/commits/f4f91326fe0469fcd514e0e2ea34699b2cbcab1a ---------------------------------------- Feature #19117: Include the method owner in backtraces, not just the method name https://bugs.ruby-lang.org/issues/19117#change-100032 * Author: byroot (Jean Boussier) * Status: Open * Priority: Normal ---------------------------------------- ``` module Foo class Bar def inspect 1 + '1' end end end p Foo::Bar.new ``` This code produce the following backtrace: ``` /tmp/foo.rb:4:in `+': String can't be coerced into Integer (TypeError) from /tmp/foo.rb:4:in `inspect' from /tmp/foo.rb:9:in `p' from /tmp/foo.rb:9:in `
' ``` This works, but on large codebases and large backtraces the method name isn't always all that revealing, most of the time you need to open many of the locations listed in the backtrace to really understand what is going on. I propose that we also include the owner name: ``` /tmp/foo.rb:4:in `Integer#+': String can't be coerced into Integer (TypeError) from /tmp/foo.rb:4:in `Foo::Bar#inspect' from /tmp/foo.rb:9:in `Kernel#p' from /tmp/foo.rb:9:in `
' ``` I believe that in many case it would allow to much better understand the backtrace without having to jump back and forth between it and the source code. This is inspired by @ivoanjo 's `backtracie` gem: https://github.com/ivoanjo/backtracie -- https://bugs.ruby-lang.org/ Unsubscribe: