ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "shioyama (Chris Salzberg)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:109935] [Ruby master Feature#19006] Inconsistent behaviour of autoload in wrapped script
Date: Sat, 17 Sep 2022 05:13:40 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-99182.20220917051340.13031@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-19006.20220917014939.13031@ruby-lang.org

Issue #19006 has been updated by shioyama (Chris Salzberg).


Rethinking this, `autoload` can fairly easily be patched to make this work without changes at the language level, so this isn't strictly necessary. In terms of transitivity, `require` is the more important one.

I'm happy to close this, thanks for the feedback!

----------------------------------------
Feature #19006: Inconsistent behaviour of autoload in wrapped script
https://bugs.ruby-lang.org/issues/19006#change-99182

* Author: shioyama (Chris Salzberg)
* Status: Open
* Priority: Normal
----------------------------------------
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/

      parent reply	other threads:[~2022-09-17  5:13 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-17  1:49 [ruby-core:109924] [Ruby master Bug#19006] Inconsistent behaviour of autoload in wrapped script shioyama (Chris Salzberg)
2022-09-17  2:35 ` [ruby-core:109928] " shioyama (Chris Salzberg)
2022-09-17  3:55 ` [ruby-core:109929] " jeremyevans0 (Jeremy Evans)
2022-09-17  4:42 ` [ruby-core:109932] [Ruby master Feature#19006] " shioyama (Chris Salzberg)
2022-09-17  5:13 ` shioyama (Chris Salzberg) [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-99182.20220917051340.13031@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).