Bug #21323
closedirb fails to start with Namespace
Description
$ irb
irb(main):001> 6 * 7
=> 42
$ RUBY_NAMESPACE=1 irb
/home/eregon/prefix/ruby-master/bin/ruby: warning: Namespace is experimental, and the behavior may change in the future!
See doc/namespace.md for know issues, etc.
/home/eregon/prefix/ruby-master/lib/ruby/3.5.0+0/did_you_mean/core_ext/name_error.rb:11: warning: irb is not part of the default gems since Ruby 3.5.0. Install irb from RubyGems.
/home/eregon/prefix/ruby-master/lib/ruby/3.5.0+0/did_you_mean/core_ext/name_error.rb:11: warning: irb is not part of the default gems since Ruby 3.5.0. Install irb from RubyGems.
<internal:/home/eregon/prefix/ruby-master/lib/ruby/3.5.0+0/rubygems/core_ext/kernel_require.rb>:141:in 'Kernel#require': cannot load such file -- irb (LoadError)
Did you mean? erb
from <internal:/home/eregon/prefix/ruby-master/lib/ruby/3.5.0+0/rubygems/core_ext/kernel_require.rb>:141:in 'Kernel#require'
from /home/eregon/prefix/ruby-master/lib/ruby/gems/3.5.0+0/gems/irb-1.15.2/exe/irb:7:in 'Namespace::Loader#require'
from /home/eregon/prefix/ruby-master/lib/ruby/gems/3.5.0+0/gems/irb-1.15.2/exe/irb:7:in '<top (required)>'
from /home/eregon/.rubies/ruby-master/bin/irb:25:in 'Kernel#load'
from /home/eregon/.rubies/ruby-master/bin/irb:25:in 'Namespace::Loader#load'
from /home/eregon/.rubies/ruby-master/bin/irb:25:in '<main>'
<internal:/home/eregon/prefix/ruby-master/lib/ruby/3.5.0+0/rubygems/core_ext/kernel_require.rb>:136:in 'Kernel#require': cannot load such file -- irb (LoadError)
Did you mean? erb
from <internal:/home/eregon/prefix/ruby-master/lib/ruby/3.5.0+0/rubygems/core_ext/kernel_require.rb>:136:in 'Kernel#require'
from /home/eregon/prefix/ruby-master/lib/ruby/gems/3.5.0+0/gems/irb-1.15.2/exe/irb:7:in 'Namespace::Loader#require'
from /home/eregon/prefix/ruby-master/lib/ruby/gems/3.5.0+0/gems/irb-1.15.2/exe/irb:7:in '<top (required)>'
from /home/eregon/.rubies/ruby-master/bin/irb:25:in 'Kernel#load'
from /home/eregon/.rubies/ruby-master/bin/irb:25:in 'Namespace::Loader#load'
from /home/eregon/.rubies/ruby-master/bin/irb:25:in '<main>'
zsh: exit 1 RUBY_NAMESPACE=1 irb
Updated by Eregon (Benoit Daloze) 10 months ago
Looks somewhat similar, benchmark loads fine in main namespace but not in other namespace:
$ RUBY_NAMESPACE=1 ruby -ve 'require "benchmark"; p Benchmark::VERSION; ns = Namespace.new; File.write "ns.rb", "require :benchmark.to_s; p Benchmark::VERSION"; ns.require "./ns"'
ruby 3.5.0dev (2025-05-10T07:50:29Z namespace-on-read-.. bd4f57f96b) +PRISM [x86_64-linux]
ruby: warning: Namespace is experimental, and the behavior may change in the future!
See doc/namespace.md for know issues, etc.
"0.4.0"
/home/eregon/prefix/ruby-master/lib/ruby/3.5.0+0/did_you_mean/core_ext/name_error.rb:11: warning: benchmark is not part of the default gems since Ruby 3.5.0. Install benchmark from RubyGems.
/home/eregon/prefix/ruby-master/lib/ruby/3.5.0+0/did_you_mean/core_ext/name_error.rb:11: warning: benchmark is not part of the default gems since Ruby 3.5.0. Install benchmark from RubyGems.
<internal:/home/eregon/prefix/ruby-master/lib/ruby/3.5.0+0/rubygems/core_ext/kernel_require.rb>:141:in 'Kernel#require': cannot load such file -- benchmark (LoadError)
from <internal:/home/eregon/prefix/ruby-master/lib/ruby/3.5.0+0/rubygems/core_ext/kernel_require.rb>:141:in 'Kernel#require'
from /home/eregon/ns.rb:1:in 'Namespace::Loader#require'
from /home/eregon/ns.rb:1:in '<top (required)>'
from -e:1:in 'Namespace#require'
from -e:1:in '<main>'
<internal:/home/eregon/prefix/ruby-master/lib/ruby/3.5.0+0/rubygems/core_ext/kernel_require.rb>:136:in 'Kernel#require': cannot load such file -- benchmark (LoadError)
from <internal:/home/eregon/prefix/ruby-master/lib/ruby/3.5.0+0/rubygems/core_ext/kernel_require.rb>:136:in 'Kernel#require'
from /home/eregon/ns.rb:1:in 'Namespace::Loader#require'
from /home/eregon/ns.rb:1:in '<top (required)>'
from -e:1:in 'Namespace#require'
from -e:1:in '<main>'
Updated by Eregon (Benoit Daloze) 10 months ago
- Related to Bug #21324: Namespace loads RubyGems in root Namespace but it should not added
Updated by retro (Josef Šimánek) 9 months ago
Have this been fixed already?
[retro@retro2 ~]❤ ruby -v
ruby 3.5.0dev (2025-05-23T23:31:28Z master 87d340f0e1) +PRISM [x86_64-linux]
[retro@retro2 ~]❤ RUBY_NAMESPACE=1 irb
/home/retro/.rubies/ruby-master/bin/ruby: warning: Namespace is experimental, and the behavior may change in the future!
See doc/namespace.md for known issues, etc.
irb(main):001> n = Namespace.new
=> #<Namespace:3,user,optional>
Updated by hsbt (Hiroshi SHIBATA) 9 months ago
- Tags set to namespace
Updated by Eregon (Benoit Daloze) 9 months ago
- Status changed from Open to Closed
Right, this seems fixed on master.
Would be interesting to know what was the fix.
Updated by jneen (Jeanine Adkisson) 17 days ago
I ran into this today with both Pry (evaluating .pryrc) and Rack (evaluating config.ru), via the puma executable.
From what I can tell, both gem executables are (I think improperly) loaded in the root box, through a plain call to load in Gem.activate_and_load_bin_path. I think this is improper because it can result in the entire app being run in the root box - which might not even cause an error in many cases! These gems, however, go on to eval code on TOPLEVEL_BINDING - for example the contents of .pryrc, the commands typed into the console, or config.ru in Rack's case. Since TOPLEVEL_BINDING is attached to the user namespace, this code then cannot see any of the constants that were defined during the gem load.
My guess is that the error would be "patched" by TOPLEVEL_BINDING being box-local - but only by causing all user code to be loaded in the root box, which I do not believe is the intention.
Updated by jneen (Jeanine Adkisson) 17 days ago
· Edited
In fact, I believe this is still happening somewhat with IRB, in that the entire program seems to be run in the root box, and TOPLEVEL_BINDING changes are not visible:
$ ./bin/irb
irb(main):001> TOPLEVEL_BINDING.eval("A = 1")
=> 1
irb(main):002> A
=> 1
irb(main):003>
$ RUBY_BOX=1 ./bin/irb
/private/tmp/ruby/bin/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.
irb(main):001> TOPLEVEL_BINDING.eval("A = 1")
=> 1
irb(main):002> A
(irb):2:in '<main>': uninitialized constant A (NameError)
from /private/tmp/ruby/lib/ruby/gems/4.0.0/gems/irb-1.16.0/exe/irb:9:in '<top (required)>'
from /private/tmp/ruby/lib/ruby/site_ruby/4.0.0/rubygems.rb:324:in 'Kernel.load'
from /private/tmp/ruby/lib/ruby/site_ruby/4.0.0/rubygems.rb:324:in 'Gem.activate_and_load_bin_path'
from ./bin/irb:25:in '<main>'
irb(main):003> Ruby::Box.current
=> #<Ruby::Box:1,root>