From b0d744bcf77db16e97ab77e391622b56136949e1 Mon Sep 17 00:00:00 2001 From: jdx <216188+jdx@users.noreply.github.com> Date: Fri, 27 Nov 2026 16:07:39 -0600 Subject: [PATCH] fix: patch binstubs with any Ruby shebang for RubyGems overwrite detection minitest 6, bundled from Ruby 4.0, starts its executable with "#!/usr/bin/env -S ruby". patch_executables only matched "#!/usr/bin/env ruby", so bin/minitest never got the "generated by RubyGems" marker and `gem update minitest` failed with a conflict. Match the shebang the way RubyGems does (/^#!.*ruby/), and fail the build when a shipped gem binstub lacks the marker after its shebang. Fixes #80 Co-Authored-By: Claude Sonnet 5.5 --- libexec/package.rb | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/libexec/package.rb b/libexec/package.rb index 1e7e072..0124f3d 100755 --- a/libexec/package.rb +++ b/libexec/package.rb @@ -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 @@ -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) 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) @@ -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)