Skip to content

Commit 41529af

Browse files
Merge pull request #8832 from rubygems/deivid-rodriguez/fix-warnings
Fix more warnings when running old Bundler with latest RubyGems
2 parents d9acc80 + 4b1df58 commit 41529af

File tree

7 files changed

+89
-35
lines changed

7 files changed

+89
-35
lines changed

.github/workflows/install-rubygems.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ jobs:
8181
run: bundle install --help | grep -q BUNDLE-INSTALL
8282
- name: Check bundler fallback man pages are properly picked up
8383
run: sudo rm $(which man) && bundle install --help
84+
- name: Install older bundler
85+
run: gem install bundler:2.6.9
86+
- name: Run older bundler without warnings
87+
run: |
88+
BUNDLER_VERSION=2.6.9 ruby -rbundler/setup -e1 2> errors.txt || (cat errors.txt && exit 1)
89+
test ! -s errors.txt || (cat errors.txt && exit 1)
90+
bundle _2.6.9_ -v 2> errors.txt || (cat errors.txt && exit 1)
91+
test ! -s errors.txt || (cat errors.txt && exit 1)
8492
- name: Build bundler
8593
run: gem build bundler.gemspec
8694
working-directory: ./bundler

Manifest.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,6 @@ hide_lib_for_update/note.txt
366366
lib/rubygems.rb
367367
lib/rubygems/available_set.rb
368368
lib/rubygems/basic_specification.rb
369-
lib/rubygems/bundler_integration.rb
370369
lib/rubygems/bundler_version_finder.rb
371370
lib/rubygems/ci_detector.rb
372371
lib/rubygems/command.rb

lib/rubygems.rb

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ def self.bin_path(name, exec_name = nil, *requirements)
249249
find_spec_for_exe(name, exec_name, requirements).bin_file exec_name
250250
end
251251

252+
def self.find_and_activate_spec_for_exe(name, exec_name, requirements)
253+
spec = find_spec_for_exe name, exec_name, requirements
254+
Gem::LOADED_SPECS_MUTEX.synchronize do
255+
spec.activate
256+
finish_resolve
257+
end
258+
spec
259+
end
260+
private_class_method :find_and_activate_spec_for_exe
261+
252262
def self.find_spec_for_exe(name, exec_name, requirements)
253263
raise ArgumentError, "you must supply exec_name" unless exec_name
254264

@@ -273,6 +283,35 @@ def self.find_spec_for_exe(name, exec_name, requirements)
273283
end
274284
private_class_method :find_spec_for_exe
275285

286+
##
287+
# Find and load the full path to the executable for gem +name+. If the
288+
# +exec_name+ is not given, an exception will be raised, otherwise the
289+
# specified executable's path is returned. +requirements+ allows
290+
# you to specify specific gem versions.
291+
#
292+
# A side effect of this method is that it will activate the gem that
293+
# contains the executable.
294+
#
295+
# This method should *only* be used in bin stub files.
296+
297+
def self.activate_and_load_bin_path(name, exec_name = nil, *requirements)
298+
spec = find_and_activate_spec_for_exe name, exec_name, requirements
299+
300+
if spec.name == "bundler"
301+
# Make sure there's no version of Bundler in `$LOAD_PATH` that's different
302+
# from the version we just activated. If that was the case (it happens
303+
# when testing Bundler from ruby/ruby), we would load Bundler extensions
304+
# to RubyGems from the copy in `$LOAD_PATH` but then load the binstub from
305+
# an installed copy, causing those copies to be mixed and yet more
306+
# redefinition warnings.
307+
#
308+
require_path = $LOAD_PATH.resolve_feature_path("bundler").last.delete_suffix("/bundler.rb")
309+
Gem.load_bundler_extensions(spec.version) if spec.full_require_paths.include?(require_path)
310+
end
311+
312+
load spec.bin_file(exec_name)
313+
end
314+
276315
##
277316
# Find the full path to the executable for gem +name+. If the +exec_name+
278317
# is not given, an exception will be raised, otherwise the
@@ -285,12 +324,7 @@ def self.find_spec_for_exe(name, exec_name, requirements)
285324
# This method should *only* be used in bin stub files.
286325

287326
def self.activate_bin_path(name, exec_name = nil, *requirements) # :nodoc:
288-
spec = find_spec_for_exe name, exec_name, requirements
289-
Gem::LOADED_SPECS_MUTEX.synchronize do
290-
spec.activate
291-
finish_resolve
292-
end
293-
spec.bin_file exec_name
327+
find_and_activate_spec_for_exe(name, exec_name, requirements).bin_file exec_name
294328
end
295329

296330
##
@@ -636,6 +670,30 @@ def self.load_safe_marshal
636670
@safe_marshal_loaded = true
637671
end
638672

673+
##
674+
# Load Bundler extensions to RubyGems, making sure to avoid redefinition
675+
# warnings in platform constants
676+
677+
def self.load_bundler_extensions(version)
678+
return unless version <= "2.6.9"
679+
680+
previous_platforms = {}
681+
682+
platform_const_list = ["JAVA", "MSWIN", "MSWIN64", "MINGW", "X64_MINGW_LEGACY", "X64_MINGW", "UNIVERSAL_MINGW", "WINDOWS", "X64_LINUX", "X64_LINUX_MUSL"]
683+
684+
platform_const_list.each do |platform|
685+
previous_platforms[platform] = Gem::Platform.const_get(platform)
686+
Gem::Platform.send(:remove_const, platform)
687+
end
688+
689+
require "bundler/rubygems_ext"
690+
691+
platform_const_list.each do |platform|
692+
Gem::Platform.send(:remove_const, platform) if Gem::Platform.const_defined?(platform)
693+
Gem::Platform.const_set(platform, previous_platforms[platform])
694+
end
695+
end
696+
639697
##
640698
# The file name and line number of the caller of the caller of this method.
641699
#
@@ -1144,7 +1202,7 @@ def self.use_gemdeps(path = nil)
11441202

11451203
ENV["BUNDLE_GEMFILE"] ||= File.expand_path(path)
11461204
require_relative "rubygems/user_interaction"
1147-
require_relative "rubygems/bundler_integration"
1205+
require "bundler"
11481206
begin
11491207
Gem::DefaultUserInteraction.use_ui(ui) do
11501208
Bundler.ui.silence do

lib/rubygems/bundler_integration.rb

Lines changed: 0 additions & 23 deletions
This file was deleted.

lib/rubygems/core_ext/kernel_require.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ def require(path) # :doc:
6464
rp
6565
end
6666

67-
Kernel.send(:gem, name, Gem::Requirement.default_prerelease) unless
68-
resolved_path
67+
next if resolved_path
68+
69+
Kernel.send(:gem, name, Gem::Requirement.default_prerelease)
70+
71+
Gem.load_bundler_extensions(Gem.loaded_specs[name].version) if name == "bundler"
6972

7073
next
7174
end

lib/rubygems/installer.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ def check_executable_overwrite(filename) # :nodoc:
228228
ruby_executable = true
229229
existing = io.read.slice(/
230230
^\s*(
231+
Gem\.activate_and_load_bin_path\( |
231232
load \s Gem\.activate_bin_path\(
232233
)
233234
(['"])(.*?)(\2),
@@ -769,7 +770,11 @@ def app_script_text(bin_file_name)
769770
end
770771
end
771772
772-
load Gem.activate_bin_path('#{spec.name}', '#{bin_file_name}', version)
773+
if Gem.respond_to?(:activate_and_load_bin_path)
774+
Gem.activate_and_load_bin_path('#{spec.name}', '#{bin_file_name}', version)
775+
else
776+
load Gem.activate_bin_path('#{spec.name}', '#{bin_file_name}', version)
777+
end
773778
TEXT
774779
end
775780

test/rubygems/test_gem_installer.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ def test_app_script_text
4848
end
4949
end
5050
51-
load Gem.activate_bin_path('a', 'executable', version)
51+
if Gem.respond_to?(:activate_and_load_bin_path)
52+
Gem.activate_and_load_bin_path('a', 'executable', version)
53+
else
54+
load Gem.activate_bin_path('a', 'executable', version)
55+
end
5256
EOF
5357

5458
wrapper = installer.app_script_text "executable"

0 commit comments

Comments
 (0)