zeitwerk icon indicating copy to clipboard operation
zeitwerk copied to clipboard

RUBY_BOX=1 vs autovivification

Open palkan opened this issue 4 months ago • 4 comments

Hey @fxn,

Hope you're doing well 🎄.

I've started to explore the newish Ruby::Box thing and... it's not going smoothly 😁.

I've encountered a problem with auto-vivification: with RUBY_BOX=1 (Ruby 4.0.0), intermediate modules are not created on-the-go (if I add them manually, all the other autoloading bits work fine).

You can reproduce it with the Zeitwerk test suite as follows:

$ RUBY_BOX=1 bundle exec ruby -Ilib:test test/lib/zeitwerk/test_autovivification.rb

  1) Error:
TestAutovivification#test_autoloads_a_simple_constant_in_an_autovivified_module__Object_:
LoadError: cannot load such file -- /Users/vladimirdementyev/dev/zeitwerk/test/tmp/admin
    test/lib/zeitwerk/test_autovivification.rb:11:in 'Ruby::Box#require'
    test/lib/zeitwerk/test_autovivification.rb:11:in 'block (2 levels) in <class:TestAutovivification>'
    test/support/loader_test.rb:108:in 'block in LoaderTest#with_setup'
    test/support/loader_test.rb:71:in 'block in LoaderTest#with_files'
    test/support/loader_test.rb:62:in 'Dir.chdir'
    test/support/loader_test.rb:62:in 'LoaderTest#with_files'
    test/support/loader_test.rb:86:in 'LoaderTest#with_setup'
    test/lib/zeitwerk/test_autovivification.rb:10:in 'block in <class:TestAutovivification>'

Please, let me know if you think we should post it onbugs.ruby-lang.org—I'll do that.

palkan avatar Jan 08 '26 21:01 palkan

Hey man!

I am in bed, but probably the decoration of Kernel#require is gone.

Without the decoration, autoloads happen because Ruby does that, but nothing else, callbacks, reloading, ....

fxn avatar Jan 08 '26 21:01 fxn

probably the decoration of Kernel#require is gone.

Ah, yeah, you're right. Adding a similar patch for Ruby::Box#require fixes the autovivification test.

palkan avatar Jan 09 '26 06:01 palkan

I see:

% ruby -e 'pp method(:require).source_location' 
["<internal:/Users/fxn/.local/share/mise/installs/ruby/4.0.0/lib/ruby/4.0.0/rubygems/core_ext/kernel_require.rb>", 36]

% RUBY_BOX=1 ruby -e 'pp method(:require).source_location'
ruby: warning: Ruby::Box is experimental, and the behavior may change in the future!
See https://docs.ruby-lang.org/en/4.0/Ruby/Box.html for known issues, etc.
nil

Hmmm, I think we might ask in the Ruby bug tracker to understand what is the expected thing to do.

fxn avatar Jan 09 '26 07:01 fxn

Reported in https://bugs.ruby-lang.org/issues/21830, let's see.

fxn avatar Jan 09 '26 09:01 fxn

I gues I'm 3 weeks late. Just started experimenting with Ruby::Box today and discovered the impact it has on everything uses zeitwert.

MadBomber avatar Jan 30 '26 03:01 MadBomber

Resolution is that this has to be addressed in Ruby. We can close here.

fxn avatar Feb 19 '26 07:02 fxn