Bug #22295
open`ruby -r<gem>` does not activate gems under RUBY_BOX=1
Description
Command-line -r (including via RUBYOPT) fails to load installed gems when the box is enabled.
$ gem install foo # any regular (non-default) gem
$ ruby -rfoo -e 1 # OK
$ RUBY_BOX=1 ruby -rfoo -e 1
-e:in 'Ruby::Box#require': cannot load such file -- foo (LoadError)
$ RUBY_BOX=1 ruby -e 'require "foo"' # OK
$ RUBY_BOX=1 RUBYOPT=-rfoo ruby -e 1 # same LoadError
Default gems are unaffected because their files are already on the load path.
With the box enabled, command-line -r is processed by Ruby::Box#require instead of the toplevel require. That path does not go through the RubyGems require override, so no gem activation happens. This is the same gem-unawareness as item 3 of https://bugs.ruby-lang.org/issues/21760, except that here the interpreter routes plain ruby -r<gem> there by itself, so it breaks without any explicit use of the Ruby::Box API.
Impact on the RubyGems test suite¶
I hit this while making the RubyGems and Bundler test suites pass under RUBY_BOX=1 (https://bugs.ruby-lang.org/issues/22275). Injecting a gem into a subprocess with RUBYOPT=-r... is a normal thing for a test harness to do, and every such test fails under the box.
One Bundler test runs bundle install in a subprocess with RUBYOPT=-rnative_child so that a system gem is already activated in that process, and the subprocess now dies with the LoadError above. The activation is what the test is about, so it cannot be rewritten without losing what it covers. Elsewhere I worked around the same failure by moving -r into a require inside -e, which is fine case by case but is not a general fix.
What should the behavior be?¶
Two things are unclear to me. Should command-line -r load gems under the box, the way it does without it? And should -r stay confined to the main box, or reach each user box as well? Today it is main box only, so a feature given on the command line is invisible to a box created later. I would like the intended behavior decided before a patch is written.
Updated by hsbt (Hiroshi SHIBATA) about 14 hours ago
- Related to Bug #21760: Ruby::Box: a couple of require-related problems added
Updated by hsbt (Hiroshi SHIBATA) about 14 hours ago
- Related to Misc #22275: Ruby::Box support plan for RubyGems and Bundler added