Skip to content

Commit d562e44

Browse files
committed
Move gem install bundler logic into Bash.
We've been getting regular but racey errors when installing Bundler inside Ruby. Also, it shouldn't ever be needed with Portable Ruby as we always vendor Bundler there. Move the relevant logic into Bash and add various fast developer-only checks to ensure that Portable Ruby is installed correctly so we can fix things in CI before they end up putting weird configurations onto users.
1 parent 58a6c82 commit d562e44

File tree

6 files changed

+28
-28
lines changed

6 files changed

+28
-28
lines changed

Library/Homebrew/Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,4 @@ RUBY VERSION
221221
ruby 3.4.7p58
222222

223223
BUNDLED WITH
224-
2.6.8
224+
2.6.9

Library/Homebrew/dev-cmd/vendor-gems.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ class VendorGems < AbstractCommand
2525

2626
sig { override.void }
2727
def run
28-
Homebrew.install_bundler!
29-
3028
ENV["BUNDLE_WITH"] = Homebrew.valid_gem_groups.join(":")
31-
ENV["BUNDLER_VERSION"] = HOMEBREW_BUNDLER_VERSION
3229

3330
ohai "cd #{HOMEBREW_LIBRARY_PATH}"
3431
HOMEBREW_LIBRARY_PATH.cd do

Library/Homebrew/standalone/init.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ def self.from_rubylibdir(feature)
5858
HOMEBREW_LIBRARY_PATH = Pathname(dir).parent.realpath.freeze
5959
HOMEBREW_USING_PORTABLE_RUBY = RbConfig.ruby.include?("/vendor/portable-ruby/").freeze
6060

61+
HOMEBREW_BUNDLER_VERSION = ENV.fetch("HOMEBREW_BUNDLER_VERSION").freeze
62+
ENV["BUNDLER_VERSION"] = HOMEBREW_BUNDLER_VERSION
63+
6164
require_relative "../utils/gems"
6265
Homebrew.setup_gem_environment!(setup_path: false)
6366

@@ -77,7 +80,7 @@ def self.from_rubylibdir(feature)
7780
require_relative "../vendor/bundle/bundler/setup"
7881
Homebrew::FastBootRequire.from_archdir("portable_ruby_gems") if HOMEBREW_USING_PORTABLE_RUBY
7982
$LOAD_PATH.unshift "#{HOMEBREW_LIBRARY_PATH}/vendor/bundle/#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/" \
80-
"bundler-#{Homebrew::HOMEBREW_BUNDLER_VERSION}/lib"
83+
"bundler-#{HOMEBREW_BUNDLER_VERSION}/lib"
8184
$LOAD_PATH.uniq!
8285

8386
# These warnings are nice but often flag problems that are not even our responsibly,

Library/Homebrew/test/rubocop_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
allowlist = %w[
1111
HOMEBREW_TESTS
1212
HOMEBREW_USE_RUBY_FROM_PATH
13+
HOMEBREW_BUNDLER_VERSION
1314
]
1415
ENV.delete(key) if key.start_with?("HOMEBREW_") && allowlist.exclude?(key)
1516
end

Library/Homebrew/utils/gems.rb

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
Homebrew::FastBootRequire.from_rubylibdir("English")
99

1010
module Homebrew
11-
# Keep in sync with the `Gemfile.lock`'s BUNDLED WITH.
12-
# After updating this, run `brew vendor-gems --update=--bundler`.
13-
HOMEBREW_BUNDLER_VERSION = "2.6.8" # Pinned to <2.6.9 until Ruby 3.5.
14-
1511
# Bump this whenever a committed vendored gem is later added to or exclusion removed from gitignore.
1612
# This will trigger it to reinstall properly if `brew install-bundler-gems` needs it.
1713
VENDOR_VERSION = 7
@@ -41,7 +37,6 @@ def self.bundler_definition
4137
private_class_method :bundler_definition
4238

4339
def self.valid_gem_groups
44-
install_bundler!
4540
require "bundler"
4641

4742
Bundler.with_unbundled_env do
@@ -161,22 +156,6 @@ def self.find_in_path(executable)
161156
end
162157
private_class_method :find_in_path
163158

164-
def self.install_bundler!
165-
old_bundler_version = ENV.fetch("BUNDLER_VERSION", nil)
166-
167-
setup_gem_environment!
168-
169-
ENV["BUNDLER_VERSION"] = HOMEBREW_BUNDLER_VERSION # Set so it correctly finds existing installs
170-
install_gem_setup_path!(
171-
"bundler",
172-
version: HOMEBREW_BUNDLER_VERSION,
173-
executable: "bundle",
174-
setup_gem_environment: false,
175-
)
176-
ensure
177-
ENV["BUNDLER_VERSION"] = old_bundler_version
178-
end
179-
180159
def self.user_gem_groups
181160
@user_gem_groups ||= if GEM_GROUPS_FILE.exist?
182161
GEM_GROUPS_FILE.readlines(chomp: true)
@@ -241,8 +220,6 @@ def self.install_bundler_gems!(only_warn_on_failure: false, setup_path: true, gr
241220
return
242221
end
243222

244-
install_bundler!
245-
246223
# Combine the passed groups with the ones stored in settings.
247224
groups |= (user_gem_groups & valid_gem_groups)
248225
groups.sort!

Library/Homebrew/utils/ruby.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# shellcheck disable=SC2154
55
export HOMEBREW_REQUIRED_RUBY_VERSION="3.4"
66
HOMEBREW_PORTABLE_RUBY_VERSION="$(cat "${HOMEBREW_LIBRARY}/Homebrew/vendor/portable-ruby-version")"
7+
export HOMEBREW_BUNDLER_VERSION="2.6.9"
78

89
# Disable Ruby options we don't need.
910
export HOMEBREW_RUBY_DISABLE_OPTIONS="--disable=gems,rubyopt"
@@ -150,9 +151,30 @@ If there's no Homebrew Portable Ruby available for your processor:
150151
brew vendor-install ruby || odie "${install_fail}"
151152
HOMEBREW_RUBY_PATH="${vendor_ruby_path}"
152153
TERMINFO_DIRS="${vendor_ruby_terminfo}"
154+
155+
if [[ -n "${HOMEBREW_DEVELOPER}" && -x "${vendor_ruby_path}" ]]
156+
then
157+
if [[ ! -f "${vendor_ruby_root}/bin/bundle" ]]
158+
then
159+
odie "Homebrew Portable Ruby is installed but bundle is not!"
160+
elif [[ ! -d "${vendor_ruby_root}/lib/ruby/gems/${HOMEBREW_REQUIRED_RUBY_VERSION}.0/gems/bundler-${HOMEBREW_BUNDLER_VERSION}" ]]
161+
then
162+
odie "Homebrew Portable Ruby is installed but bundler ${HOMEBREW_BUNDLER_VERSION} is not!"
163+
elif ! grep -q " ${HOMEBREW_BUNDLER_VERSION}" "${HOMEBREW_LIBRARY}/Homebrew/Gemfile.lock"
164+
then
165+
odie "Homebrew Portable Ruby is installed but bundler ${HOMEBREW_BUNDLER_VERSION} is not in the Gemfile.lock!"
166+
fi
167+
fi
153168
fi
154169
fi
155170

171+
homebrew_ruby_bin="$(dirname "${HOMEBREW_RUBY_PATH}")"
172+
if [[ ! -f "${homebrew_ruby_bin}/bundle" ]]
173+
then
174+
"${homebrew_ruby_bin}/gem" install bundler -v "${HOMEBREW_BUNDLER_VERSION}"
175+
fi
176+
PATH="${homebrew_ruby_bin}:${PATH}"
177+
156178
export HOMEBREW_RUBY_PATH HOMEBREW_BOOTSNAP_GEM_PATH
157179
[[ -n "${HOMEBREW_LINUX}" && -n "${TERMINFO_DIRS}" ]] && export TERMINFO_DIRS
158180
}

0 commit comments

Comments
 (0)