From a776d5f02b3a193590a3c092531f798c83fcb656 Mon Sep 17 00:00:00 2001 From: Bo Anderson Date: Sat, 8 Oct 2022 18:57:40 +0100 Subject: [PATCH] Workaround macOS system Ruby issue picking incorrect native gem archs --- .../rubygems/defaults/operating_system.rb | 20 +++++++++++++++++++ Library/Homebrew/shims/ruby/bundle | 15 ++++++++++++++ Library/Homebrew/style.rb | 6 +++++- Library/Homebrew/utils/gems.rb | 1 + 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 Library/Homebrew/rubyext/rubygems/defaults/operating_system.rb create mode 100755 Library/Homebrew/shims/ruby/bundle diff --git a/Library/Homebrew/rubyext/rubygems/defaults/operating_system.rb b/Library/Homebrew/rubyext/rubygems/defaults/operating_system.rb new file mode 100644 index 0000000000..c708b12616 --- /dev/null +++ b/Library/Homebrew/rubyext/rubygems/defaults/operating_system.rb @@ -0,0 +1,20 @@ +# typed: strict +# frozen_string_literal: true + +require "etc" + +# Fixes universal-ruby getting confused whether to install arm64 or x86_64 macOS versions. +# https://github.com/rubygems/rubygems/issues/4234 +# This can be removed when either: +# - We stop using system Ruby +# - System Ruby is updated with this patch (shipped with Ruby 3.1 or later): +# https://github.com/ruby/ruby/commit/96ce1d9a0ff64494753ad4730f36a0cd7e7a89e7 +# - The Rubygems PR https://github.com/rubygems/rubygems/pull/4238 is merged +# AND we install a new enough Rubygems which includes the said patch, instead of relying the system's version. +platform = Gem::Platform.local.dup +platform.cpu = Etc.uname[:machine] if platform.os == "darwin" && platform.cpu == "universal" +Gem.platforms[Gem.platforms.index(Gem::Platform.local)] = platform + +# This doesn't currently exist in system Ruby but it's safer to check. +orig_file = File.join(RbConfig::CONFIG["rubylibdir"], "rubygems", "defaults", "operating_system") +require orig_file if File.exist?(orig_file) diff --git a/Library/Homebrew/shims/ruby/bundle b/Library/Homebrew/shims/ruby/bundle new file mode 100755 index 0000000000..cc6eea1f26 --- /dev/null +++ b/Library/Homebrew/shims/ruby/bundle @@ -0,0 +1,15 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# Insert our system Ruby patch script, if neeeded. +# Fixes universal-ruby getting confused whether to install arm64 or x86_64 macOS versions. +# https://github.com/rubygems/rubygems/issues/4234 +if RUBY_PLATFORM.match?(/universal\..*-darwin/) + rubylib = File.expand_path("../../rubyext", File.dirname(__FILE__)) + ENV["RUBYLIB"] = rubylib + $LOAD_PATH.unshift(rubylib) unless $LOAD_PATH.include?(rubylib) + require "rubygems/defaults/operating_system" if defined?(Gem) # Reload if already loaded. +end + +require "rubygems" +load Gem.activate_bin_path("bundler", "bundle", ">= 0.a") diff --git a/Library/Homebrew/style.rb b/Library/Homebrew/style.rb index ce38f1a1af..cdbabeb93f 100644 --- a/Library/Homebrew/style.rb +++ b/Library/Homebrew/style.rb @@ -263,7 +263,11 @@ module Homebrew HOMEBREW_REPOSITORY/"Dockerfile", *HOMEBREW_LIBRARY.glob("Homebrew/*.sh"), *HOMEBREW_LIBRARY.glob("Homebrew/shims/**/*").map(&:realpath).uniq - .reject { |path| path.directory? || path.basename.to_s == "cc" }, + .reject(&:directory?) + .reject { |path| path.basename.to_s == "cc" } + .select do |path| + %r{^#! ?/bin/(?:ba)?sh( |$)}.match?(path.read(13)) || path.extname == ".sh" + end, *HOMEBREW_LIBRARY.glob("Homebrew/{dev-,}cmd/*.sh"), *HOMEBREW_LIBRARY.glob("Homebrew/{cask/,}utils/*.sh"), ] diff --git a/Library/Homebrew/utils/gems.rb b/Library/Homebrew/utils/gems.rb index 3d69a02104..6930819df1 100644 --- a/Library/Homebrew/utils/gems.rb +++ b/Library/Homebrew/utils/gems.rb @@ -60,6 +60,7 @@ module Homebrew paths = ENV.fetch("PATH").split(":") paths.unshift(ruby_bindir) unless paths.include?(ruby_bindir) paths.unshift(Gem.bindir) unless paths.include?(Gem.bindir) + paths.unshift(HOMEBREW_SHIMS_PATH/"ruby") unless paths.include?(HOMEBREW_SHIMS_PATH/"ruby") ENV["PATH"] = paths.compact.join(":") # Set envs so the above binaries can be invoked.