Skip to content

Avoid space-containing absolute path in RUBYOPT#9696

Open
hsbt wants to merge 2 commits into
masterfrom
set-rubyopt-space-safe
Open

Avoid space-containing absolute path in RUBYOPT#9696
hsbt wants to merge 2 commits into
masterfrom
set-rubyopt-space-safe

Conversation

@hsbt

@hsbt hsbt commented Jul 15, 2026

Copy link
Copy Markdown
Member

Bundler::SharedHelpers#set_rubyopt injects -r<absolute path to bundler/setup> into RUBYOPT. Ruby splits RUBYOPT on whitespace with no quoting mechanism, so when bundler lives under an install prefix containing a space (for example C:\Program Files), the path tears apart and every child ruby spawned under bundle exec dies at boot with invalid switch in RUBYOPT: -i.

The absolute path was introduced in a5da088 so that child processes never leak to a different bundler copy. That property is kept for all currently working setups: the absolute form is still used whenever the path has no whitespace. Only in the space-containing case, which today fails unconditionally, does set_rubyopt fall back to requiring bundler/setup by feature name. set_rubylib already puts bundler's lib directory first on the child's load path via RUBYLIB (which is PATH_SEPARATOR-separated and therefore safe with spaces), so the child still resolves the same bundler copy. When bundler is the default gem in rubylibdir, RUBYLIB is not modified and the feature name resolves through the same ruby's default load path, again the same copy. The BUNDLER_SETUP variable is unaffected since it is consumed as a plain require argument in lib/rubygems.rb where spaces are harmless.

Verified on Windows with a mswin Ruby 4.0.5 and the patched bundler copied under V:\tmp\ruby with space\patched lib\:

bundler dir = "V:/tmp/ruby with space/patched lib/bundler"
RUBYOPT = "-rbundler/setup"
RUBYLIB = "V:/tmp/ruby with space/patched lib"
child: child ok, Bundler loaded from V:/tmp/ruby with space/patched lib/bundler/shared_helpers.rb
OK site5: child ruby boots and loads the intended bundler copy
control: old-style RUBYOPT child => exit 1, ruby.exe: invalid switch in RUBYOPT: -i (RuntimeError)

Related to #9695, which fixes the argv quoting half of the same space-path breakage.

🤖 Generated with Claude Code

RUBYOPT is split on whitespace with no quoting mechanism, so the
absolute `-r` path injected by set_rubyopt tears apart when bundler
lives under an install prefix containing a space, and every child ruby
under `bundle exec` dies at boot with "invalid switch in RUBYOPT".
Fall back to requiring bundler/setup by feature name in that case;
set_rubylib already puts our lib directory first on the child's load
path, so the same bundler copy is still loaded.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 15, 2026 07:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

The whitespace context stubs File.expand_path globally, so
Gem.bin_path called from set_bundle_variables tries to open the fake
path as a real gemspec stub and fails with ENOENT. Stub bin_path too,
matching the existing special characters context.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants