RUBY_BOX=1 vs autovivification
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.
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, ....
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.
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.
Reported in https://bugs.ruby-lang.org/issues/21830, let's see.
I gues I'm 3 weeks late. Just started experimenting with Ruby::Box today and discovered the impact it has on everything uses zeitwert.
Resolution is that this has to be addressed in Ruby. We can close here.