ruby / error_highlight

The gem enhances Exception#message by adding a short explanation where the exception is raised
MIT License
150 stars 23 forks source link

Handle nil nd_parent in spot_colon2 #16

Closed jeremyevans closed 2 years ago

jeremyevans commented 2 years ago

This fixes an issue in Sequel's tests on ruby 3.1.0-preview1, where a test was showing the wrong output in an exception message. The related code rescues NameError to provide more information, then reraises. Here's the output of Sequel's test without changes to error_highlight:

  1) Failure:
Sequel::Model::Associations::AssociationReflection::#associated_class#test_0006_should include association inspect output if an exception would be raised [/data/code/sequel/spec/model/associ
ation_reflection_spec.rb:75]:
Expected "undefined method `last_lineno' for nil:NilClass\n\n      if nd_parent.last_lineno == @node.last_lineno\n                  ^^^^^^^^^^^^" to include # encoding: UTF-8
"#<Sequel::Model::Associations::ManyToOneAssociationReflection #<Class:0x000002594112e458>.many_to_one :c>".

This change makes the Sequel test pass. There probably is a better fix.

I tried to reproduce this outside of Sequel's tests in order to create a test for error_highlight, but I wasn't able to do so quickly.

mame commented 2 years ago

Thank you for your report. I think I got a clue. The following code will trigger an unknown bug of error_highlight (or RubyVM::AST or something).

module Foo
  Object.module_eval("::Bar", __FILE__, __LINE__)
end

The error message changes if error_highlight is enabled:

$ ruby --disable-error_highlight t.rb
t.rb:2:in `<module:Foo>': uninitialized constant Bar (NameError)
        from t.rb:2:in `module_eval'
        from t.rb:2:in `<module:Foo>'
        from t.rb:1:in `<main>'

$ ruby t.rb
t.rb:2:in `<module:Foo>': NameError
        from t.rb:2:in `module_eval'
        from t.rb:2:in `<module:Foo>'
        from t.rb:1:in `<main>'

I'll continue to investigate the issue tonight

mame commented 2 years ago

@jeremyevans I think the issue was fixed by the combination of https://github.com/ruby/error_highlight/commit/d2140d795ad0a06398db81739201877d431755db and https://github.com/ruby/ruby/commit/6a51c3e80c0901851c252ed4d1387e43939a452f. Could you try ruby/ruby master with your code base? Let me know if it is not fixed yet.

mame commented 2 years ago

Ah, no, the issue is not fixed yet. Continue to investigate...

mame commented 2 years ago

Argh, a pilot error. I used the older ruby to check if the issue is fixed or not. I think it's fixed after all.

jeremyevans commented 2 years ago

I've built and installed ruby-head locally and checked and confirmed the problem is fixed. Thank you very much for fixing this!