Feature #21105
closedImprove Ruby Stack Trace to Include Exact Error Position (Column Number)
Description
Subject¶
Improve Ruby Stack Trace to Include Exact Error Position (Column Number)
Description¶
Currently, when an exception occurs in Ruby, the stack trace provides the file name and line number but does not indicate the exact position (column number) where the error occurred within the line. This lack of precision can make debugging more challenging, especially in cases where multiple method calls or expressions are present on the same line.
Example¶
class Example
def self.run
nil.some_method_call # Error occurs here
end
end
Example.run
Expected Behavior¶
The stack trace should include the column number where the error occurred, e.g.:
trace.rb:4:10:in `run': undefined method `some_method_call' for nil:NilClass (NoMethodError)
Benefits¶
More precise debugging.
Easier identification of errors in complex one-liner expressions.
Better tooling support for editors and debuggers.
Additional Notes¶
Would it be possible to add this enhancement in a future Ruby version?
Thank you for considering this request!
Updated by Eregon (Benoit Daloze) 21 days ago
It is already shown:
class Example
def self.run
nil.foo.bar.baz
end
end
Example.run
example.rb:3:in `run': undefined method `foo' for nil (NoMethodError)
nil.foo.bar.baz
^^^^
from example.rb:7:in `<main>'
Adding columns would add a lot of noise and would not be clearer than that.
So I think we can close this as "already done, just in a different/clearer way"
Updated by matz (Yukihiro Matsumoto) 10 days ago
- Status changed from Open to Closed
I like introducing the column number in the error message, but in reality, it is hard to get column numbers from the error object, and I don't think it's worth the extensive overhaul to make that possible.
Probably we will add this in the future wishlist.
Matz.
Updated by mame (Yusuke Endoh) 10 days ago
I think it is worth to display column info as trace.rb:3:7: in 'run': ...
. When you click on it in the vscode terminal, it will open the file and jumpto that location.
However, there are numerous problems to making this happen.
- Which specific column position should be displayed: For NoMethodError, near the beginning of the method name seems appropriate. However, for ArgumentError, you may want to indicate the position of the argument that caused the problem. Appropriate column locations would change on a node-by-node and exception-by-exception basis.
- Overhead: Currently, the interpreter bytecode only holds the node IDs, not the column information. We need to store column information for each bytecode instruction, which will require memory overhead, or, to reopen and reparse the source file to restore column information each time a backtrace is generated, which will require performance overhead.
- Compatibility: There are a small number of projects that parse backtrace strings using regular expressions. The addition of column numbers will cause that parsing to stop working.
Updated by ennder (Jérôme BATAILLE) 2 days ago
· Edited
Sorry to comment on a closed issue.
As a complement, I find it very useful when there is multiple objects calling the same method for exemple :
class Example
def self.runnable?
check_one.same_method_call? && check_two.same_method_call?
end
end
Example.runnable?
# The position in the line helps to know which object is nil
trace.rb:4:46:in `runnable?': undefined method `same_method_call?' for nil:NilClass (NoMethodError)