Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions libexec/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,12 @@ def patch_executables
next unless File.file?(exe)

content = File.read(exe)
next unless content.start_with?("#!/bin/sh") && content.include?("#!/usr/bin/env ruby")
next unless content.start_with?("#!/bin/sh")

# Match any Ruby shebang the way RubyGems' check_executable_overwrite
# does (/^#!.*ruby/): minitest 6 ships "#!/usr/bin/env -S ruby".
patched = content.sub(
%r{(#!/usr/bin/env ruby\n)\n(require 'rubygems')},
/^(#!.*ruby.*\n)\n(require 'rubygems')/,
"\\1#\n# This file was generated by RubyGems.\n#\n\\2"
)
File.write(exe, patched) if patched != content
Expand Down Expand Up @@ -684,6 +686,7 @@ def test_installation!
bundle = File.join(test_root, "bin", "bundle")
env = { "PATH" => "/usr/bin:/bin", "GEM_HOME" => nil, "GEM_PATH" => nil }

check_binstub_markers!(test_root)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
assert_equal(version.split("-").first, capture(ruby, "-e", "print RUBY_VERSION", env: env).strip) unless version.include?("preview")
assert_equal(ruby, capture(ruby, "-e", "print RbConfig.ruby", env: env).strip)
assert_equal("3632233996", capture(ruby, "-rzlib", "-e", "print Zlib.crc32('test')", env: env).strip)
Expand Down Expand Up @@ -724,6 +727,24 @@ def install_default_native_gem(ruby, gem_name, env)
raise
end

# RubyGems only overwrites a gem executable when the line after the Ruby
# shebang and a blank line says "This file was generated by RubyGems"
# (Gem::Installer#check_executable_overwrite). Without it, `gem update` of
# a bundled gem fails with "conflicts with .../bin/NAME".
def check_binstub_markers!(root)
Dir.glob(File.join(root, "bin", "*")).each do |exe|
next unless File.file?(exe)

lines = File.binread(exe).lines
next unless lines.any? { |line| line.include?("Gem.activate_bin_path") || line.include?("Gem.bin_path") }

shebang = lines.index { |line| line.match?(/^#!.*ruby/) }
next if shebang && lines[shebang + 2]&.include?("This file was generated by RubyGems")

raise PackageError, "#{exe} lacks the RubyGems marker after its Ruby shebang, so gem update cannot overwrite it"
end
end

def check_no_homebrew_paths!(root, ruby, env)
forbidden = %w[/home/linuxbrew/.linuxbrew /opt/homebrew /usr/local/Homebrew liblto_plugin.so]
config = capture(ruby, "-rrbconfig", "-e", "puts((RbConfig::CONFIG.values + RbConfig::MAKEFILE_CONFIG.values).compact)", env: env)
Expand Down
Loading