Git Product home page Git Product logo

Comments (9)

JonRowe avatar JonRowe commented on August 15, 2024 2

Makes me wonder, what is the difference that makes RSpec load these files specifically from require statements?

RSpec adds ./lib and (a configurable) spec path to the load path, this allows people working on normal ruby project to do require 'name' in specs, and similarly allows require 'spec_helper' to work.

This convention is sometimes also used for monkey patching as you say like lib/<gem>/<ext-file-with-same-name-as-original>.rb to override or backport a fix, which typically isn't an issue either, as these files usually have the same contents as the original.

If it works from Rails, I take it you were requiring relatively or with full path names? As e.g. require 'date' will only work once. Personally I would avoid shaddowing file names unless you are doing a full monkey patch with original contents, to ensure it will work reliably across tools.

from rspec-rails.

hakunin avatar hakunin commented on August 15, 2024 1

irb -f

Yep, works fine in irb:

➜ irb -f
irb(main):001:0> require 'date'
=> true
irb(main):002:0> Date.today + 1
=> #<Date: 2024-07-02 ((2460494j,0s,0n),+0s,2299161j)>

The error happens at:

  alias_method :plus_without_duration, :+ # <-- here
  alias_method :+, :plus_with_duration

Which is strange.

from rspec-rails.

pirj avatar pirj commented on August 15, 2024 1

Got it. Happy to have helped.
Is there anything actionable left?

from rspec-rails.

pirj avatar pirj commented on August 15, 2024

That’s weird. On 2.7.8 in irb -f after require 'date' I can Date.today + 1. Can you?
Wondering what you’ll be able to find.

from rspec-rails.

pirj avatar pirj commented on August 15, 2024

Just for the reference https://github.com/rails/rails/blob/53410537594be0c0a528cff53dce433dd386cb6a/activesupport/lib/active_support/core_ext/date/calculations.rb#L97

from rspec-rails.

hakunin avatar hakunin commented on August 15, 2024

Grepped through our gems, disabled all the gems that extend the Date class, but didn't help.
Checked if the file where it fails is being loaded twice - and it isn't 🙃

from rspec-rails.

pirj avatar pirj commented on August 15, 2024

Can you set a binding.irb breakpoint just before that alias_method to see if new + 1 works?

from rspec-rails.

hakunin avatar hakunin commented on August 15, 2024

Can you set a binding.irb breakpoint just before that alias_method to see if new + 1 works?

Yes, it fails on the missing plus.

I grepped the whole codebase and gems for the alias_method with a :+ and the only thing I am getting is:

grep -r "alias_method :+" . -n
./.bundle/ruby/2.7.0/gems/nokogiri-1.15.5-x86_64-linux/lib/nokogiri/xml/node_set.rb:431:      alias_method :+, :|
./.bundle/ruby/2.7.0/gems/activesupport-6.1.5/lib/active_support/core_ext/time/calculations.rb:289:  alias_method :+, :plus_with_duration
./.bundle/ruby/2.7.0/gems/activesupport-6.1.5/lib/active_support/core_ext/date/calculations.rb:101:  alias_method :+, :plus_with_duration
./.bundle/ruby/2.7.0/gems/xpath-3.2.0/lib/xpath/dsl.rb:65:    alias_method :+, :union
./.bundle/ruby/2.7.0/gems/addressable-2.8.5/lib/addressable/uri.rb:1971:    alias_method :+, :join

So what's happening doesn't make a lot of sense.
I checked RUBY_VERSION => "2.7.6"

Solution

Finally, I found out that we had a lib/date.rb file, which somehow loaded the first time require 'date' was run. (even before railties?)

But this only happens when running rspec, not when the rails app is run. Thank you so much for helping me figure this out!

from rspec-rails.

hakunin avatar hakunin commented on August 15, 2024

Turns out the project I work on has lib/<gem>/<ext-file-with-same-name-as-original>.rb in a few places even.
And Rails has no problem with it, because it's only loaded explicitly from initializers.
Makes me wonder, what is the difference that makes RSpec load these files specifically from require statements?

from rspec-rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.