ruby / psych

A libyaml wrapper for Ruby
MIT License
566 stars 206 forks source link

rails console on reload: undefined method `end_line=' for nil (NoMethodError) #685

Open thisIsLoading opened 3 months ago

thisIsLoading commented 3 months ago

Hi,

everytime i reload! the rails console i get this error:

irb(main):008> reload!
Reloading...
(irb):8:in `<main>': undefined method `end_line=' for nil (NoMethodError)

      node.end_line   = @end_line
          ^^^^^^^^^^^^^
irb(main):009> 

it was annoying but didnt bother me enough to dig a little deeper. but today was the day. so i searched my whole computer for that line and ended up in the psych gem folder right here:

https://github.com/ruby/psych/blob/ea79a95e2da83be7fca102f23503713caa6820d4/lib/psych/tree_builder.rb#L134

    def set_end_location(node)
      node.end_line   = @end_line
      node.end_column = @end_column
    end`

it is obviously not the actual cause of the issue but has anyone an idea how i could figure out where the issue is?

thanks in advance!

tinbka commented 3 weeks ago

I case anyone comes to this page after me, this just means you have a syntax error in your code. I got this when I got an incorrectly misplaced closing braces in a long expression in IRB. That's weird that the issue opener got this from reload! though, I thought reload! usually reports syntax errors just fine.

pedros007 commented 3 weeks ago

I've had the same problem for years. I finally took time to research and found this very issue. To find the "syntax error" I ran this a rails console session:

begin
  reload!
rescue => e
  puts e.full_message(highlight: true, order: :top)
end

and buried in the stack trace was the line of my problematic application code:

FactoryBot.define do
  factory :coords do
    x {123}
    y {456}   #  <=== this is the problematic line!
  end
end

It turns out y is defined for psych!

https://github.com/ruby/psych/blob/ac15c01f60d73a58de039bc4b7fe0f42ca85fb63/lib/psych/y.rb#L4-L7

I can make my factory work by changing my factory from y { 456 } to add_attribute(:y) { 456 }... but perhaps psych's y is an overly generic method name that only runs in irb?

thisIsLoading commented 3 weeks ago

@pedros007 thank you so much for posting this. i just confirmed this to be the root cause on my end as well. i have a grid system which stores the length and height in x and y and i totally will not change this on my end because that would mean ripping apart the guts and a nightmare to put it all back together

FactoryBot.define do
  factory :widget do
    dashboard { nil }
    x { 1 }
    y { 1 }
    width { 1 }
    height { 1 }
    category { 1 }
    config { "" }
  end
end

i agree that psych should not be using just y here.