From: "jeremyevans0 (Jeremy Evans)" Date: 2022-09-17T03:55:30+00:00 Subject: [ruby-core:109929] [Ruby master Bug#19006] Inconsistent behaviour of autoload in wrapped script Issue #19006 has been updated by jeremyevans0 (Jeremy Evans). When loading `foo.rb` with the `MyModule` wrapper, you are loading the equivalent of: ```ruby module MyModule module Foo autoload :Bar, "foo/bar" end end ``` `foo/bar.rb` defines `::Foo::Bar`, not `::MyModule::Foo::Bar`, so I think the error you receive when you attempt to load `MyModule::Foo::Bar` is expected. `load`'s wrapped module is not transitive to `load`/`require`/`autoload` calls inside the loaded file. It was never designed to be transitive, nor documented as being transitive, so I don't think the current behavior is a bug. Making the behavior transitive would be a feature request, IMO. FWIW, I agree that the lack of transitivity makes `load`'s wrapped module not very useful in practice. However, I don't see that as a problem. ---------------------------------------- Bug #19006: Inconsistent behaviour of autoload in wrapped script https://bugs.ruby-lang.org/issues/19006#change-99177 * Author: shioyama (Chris Salzberg) * Status: Open * Priority: Normal * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN ---------------------------------------- Suppose I have two files, `foo.rb` and `bar.rb`: ```ruby # foo.rb puts "loading Foo..." module Foo autoload :Bar, "foo/bar" end ``` and ```ruby # foo/bar.rb puts "loading Foo::Bar..." module Foo module Bar end end ``` I can `require "foo"` and access both `Foo` and `Foo::Bar`: ```ruby require "foo" # loading Foo... #=> true Foo::Bar # loading Foo::Bar... #=> Foo::Bar ``` However, if I _load_ `foo` under a wrap module with `load`: ```ruby MyModule = Module.new load "./foo.rb", MyModule # loading Foo... #=> true ``` ... I'm now unable to access `Foo::Bar` anywhere, because whereas the constant is autoloaded from `MyModule::Foo::Bar`, it is required from the top-level as `Foo::Bar`: ```ruby MyModule::Foo::Bar # loading Foo::Bar #=> uninitialized constant MyModule::Foo::Bar (NameError) ``` This means that `autoload` is basically useless inside anything loaded with the `wrap` argument to `load`, because the file being autoloaded can't know in advance what the base namespace will be. I would argue that it makes much more sense to apply the wrap module (`top_wrapper`) to any autoloaded file loaded when `top_wrapper` is set. In the example above, this would mean that accessing `MyModule::Foo::Bar` would work, since `MyModule` would apply when the autoload triggers to load `foo/bar`. -- https://bugs.ruby-lang.org/ Unsubscribe: