rubygems / bundler

Manage your Ruby application's gem dependencies
https://bundler.io
MIT License
4.88k stars 1.99k forks source link

Do `require "rubygems"` only when needed #7520

Closed mame closed 4 years ago

mame commented 4 years ago

What was the end-user problem that led to this PR?

The require causes circular require. See https://github.com/bundler/bundler/pull/7505#issuecomment-568832933

$ touch empty_file

$ RUBYGEMS_GEMDEPS=empty_file ./local/bin/ruby -w -e ''
/home/mame/work/ruby/local/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92: warning: /home/mame/work/ruby/local/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92: warning: loading in progress, circular require considered harmful - /home/mame/work/ruby/local/lib/ruby/2.7.0/rubygems.rb
    from <internal:gem_prelude>:1:in  `<internal:gem_prelude>'
    from <internal:gem_prelude>:1:in  `require'
    from /home/mame/work/ruby/local/lib/ruby/2.7.0/rubygems.rb:1417:in  `<top (required)>'
    from /home/mame/work/ruby/local/lib/ruby/2.7.0/rubygems.rb:1203:in  `use_gemdeps'
    from /home/mame/work/ruby/local/lib/ruby/2.7.0/rubygems/user_interaction.rb:47:in  `use_ui'
    from /home/mame/work/ruby/local/lib/ruby/2.7.0/rubygems.rb:1204:in  `block in use_gemdeps'
    from /home/mame/work/ruby/local/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in  `require'
    from /home/mame/work/ruby/local/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in  `require'
    from /home/mame/work/ruby/local/lib/ruby/2.7.0/bundler.rb:11:in  `<top (required)>'
    from /home/mame/work/ruby/local/lib/ruby/2.7.0/bundler.rb:11:in  `require_relative'
    from /home/mame/work/ruby/local/lib/ruby/2.7.0/bundler/rubygems_integration.rb:3:in  `<top (required)>'
    from /home/mame/work/ruby/local/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in  `require'
    from /home/mame/work/ruby/local/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in  `require'

What was your diagnosis of the problem?

Do not require "rubygems" blindly.

What is your fix for the problem, implemented in this PR?

Add unless defined?(Gem) to make sure it does require "rubygems" only when rubygems is not required.

Why did you choose this fix out of the possible options?

This is needed to release Ruby 2.7.0 so I chose the simplest fix.

hsbt commented 4 years ago

@bundlerbot merge

ghost commented 4 years ago

Build succeeded

nurse commented 4 years ago

"1h 9m 55s" oh... https://dev.azure.com/bundler/bundler/_build/results?buildId=2993&view=logs&j=2d2b3007-3c5c-5840-9bb0-2b1ea49925f3&t=3f67303d-caa6-535b-2055-750b67f1a4bf