Skip to content

Commit 4b1df58

Browse files
Fix more warnings when running old Bundler with latest RubyGems
Also fix platform warnings when Bundler's entrypoint is bundler's binstub.
1 parent 8683fae commit 4b1df58

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

.github/workflows/install-rubygems.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ jobs:
8787
run: |
8888
BUNDLER_VERSION=2.6.9 ruby -rbundler/setup -e1 2> errors.txt || (cat errors.txt && exit 1)
8989
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)
9092
- name: Build bundler
9193
run: gem build bundler.gemspec
9294
working-directory: ./bundler

lib/rubygems.rb

Lines changed: 40 additions & 6 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
##

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)