Raise exception if the constant was not defined.#5949
Conversation
aa997d9 to
092894e
Compare
4196e0e to
a27a534
Compare
a27a534 to
a8b9f59
Compare
|
@fxn if you have time to fix the tests, looks like we can merge this. |
|
On it! |
|
@casperisfine what do you think about raising |
|
Could you add a spec under spec/ruby for this? Would also help to see what the message looks like. |
I'd say a subclass is only worth it if there a good use case for rescuing it particularly. Otherwise a different message is good enough. |
|
@casperisfine but Shopify rescues |
|
@fxn, no that was for |
|
New specs up! I think this is a good change. When you define an autoload, in my opinion it is right to require the constant you claimed to be defined, and exactly in the receiver. Also, cool that the internal metadata is deleted to leave a clean internal state. I kept the existing specs, and added new ones for 3.2. Please let me know if you prefer to delete them instead. |
Could you clarify this? Because I've seen code such as: module AppNamespace
autoload :TopLevelGemNameSpace, "gem_name"
endAs a way to lazy load a gem. So changing this would break code. (I don't remember where I've seen this though, but I've seen it more than once). |
|
@byroot This is what this patch does: module Foo
autoload :Date, 'date'
Date # => autoload "date" failed to resolve Foo::Date (NameError)
endIt is also what Zeitwerk has been enforcing for three years already. To me, this makes sense. |
Ok, so we meant the same thing. And yes, some code rely on that today. I'll see if I can find some real work example next week. |
|
Take this too in current Ruby: module Foo
autoload :Date, 'date'
p Foo.constants # => [:Date]
Date
p Foo.constants # => []
endMakes no sense! Because autoload is not fully aligned with Ruby semantics in this edge case, in my view.
This patch makes semantics more consistent. |
I don't disagree, I'd go as far as to say I agree, but backward compatibility is taken very seriously by ruby-core, so this MUST be discussed and approved. If it's approved I'll happily go fix the gems that depends on this, but I don't think we "have the mandate" to break this yet. |
Hahaha.
Oh, absolutely. |
|
One example I finally remembered is Thor (vendored in bundler) https://github.com/rubygems/rubygems/blob/96e5cff3df491c4d943186804c6d03b57fcb459b/bundler/lib/bundler/vendor/thor/lib/thor/runner.rb#L9 Similarly there's this Breaking rubygems is a pretty big deal :/ |
|
I mentioned https://bugs.ruby-lang.org/issues/15790 in a comment above. That was fixed in 3.1 by #4715. |
|
I remember a Line 26 in 931b013 Given the CI is green though, it seems that pattern is still allowed maybe? (both RubyGems & Bundler are tested in CRuby's CI AFAIK). That Gem::Requirement autoload is very weird, I wonder if it actually ever worked, or it just worked due to |
I suspect it's a test order thing. Something else must require the necessary parts properly earlier in the test suite? Either way, I think this should go though a deprecation period. The PR description should be updated to clearly state what behavior is being changed, and an associated issue on Redmine should be created to it can be discussed with more core members (I feel this needs Matz approval). |
|
Re that # autoload.rb
module Foo
class R
end
end
# main.rb
module Foo
end
class Foo::Bar
autoload :R, File.expand_path('autoload', __dir__)
p R
end(Replacing
Good as you did, we need to keep testing 3.1 and before. |
|
@fxn well done. This is looking really good to me. Obviously we don't want to break things but if it was already non-functional/broken I would say there is probably some wiggle room to propose a way forward for 3.2 with this PR. It sounds like @byroot has some good opinions about that. Of course, there is a chance to break things, but there is no way to avoid it since this by nature is preventing a class of functionality that was "allowed" before. I think we should definitely consider practical concerns if they show up, but short of real world failures, I'd support merging this for 3.2. Bearing in mind that people (1) aren't forced to upgrade to 3.2 and (2) can fix libraries to be compatible in a relatively straight forward way. |
|
FYI |
|
@ko1 Thanks! I have checked the ones related to The gem The gem This leaves us with two gems that would need to be updated: I need to work now, will check |
|
These are just example, I don't think we can properly assert the extend of the breakage with just a grep. I don't see any urge to make this breaking change, it removes a weird cruft yes, but it's not in the way of anything, nor does it break anyone's code. So simply deprecating with a warning message makes sense to me. But again, this needs to be discussed, who wish to open the Redmine issue? |
|
I have checked I'll open a ticket in Redmine. |
|
I can remove the Regarding the one in |
|
@fxn Let's rework this branch for |
No description provided.