bundle/exec: fix command PATH lookup

We are checking `PATH` for the command too early, since the code below
it mutates `PATH`.

Let's defer the check to later to fix this.
This commit is contained in:
Carlo Cabrera 2025-03-19 21:27:45 +08:00 committed by Carlo Cabrera
parent 14fe53b9fd
commit a4f2797612
No known key found for this signature in database
GPG Key ID: C74D447FC549A1D0
2 changed files with 9 additions and 12 deletions

View File

@ -59,15 +59,6 @@ module Homebrew
command = args.first
# For commands which aren't either absolute or relative
if command.exclude? "/"
# Save the command path, since this will be blown away by superenv
command_path = which(command)
raise "command was not found in your PATH: #{command}" if command_path.blank?
command_path = command_path.dirname.to_s
end
@dsl = Brewfile.read(global:, file:)
require "formula"
@ -111,8 +102,11 @@ module Homebrew
end
# rubocop:enable Homebrew/MoveToExtendOS
# Ensure the Ruby path we saved goes before anything else, if the command was in the PATH
ENV.prepend_path "PATH", command_path if command_path.present?
# For commands which aren't either absolute or relative
# Add the command directory to PATH, since it may get blown away by superenv
if command.exclude?("/") && (which_command = which(command)).present?
ENV.prepend_path "PATH", which_command.dirname.to_s
end
# Replace the formula versions from the environment variables
formula_versions = {}
@ -155,6 +149,9 @@ module Homebrew
ENV.append_path "PATH", homebrew_path
end
# For commands which aren't either absolute or relative
raise "command was not found in your PATH: #{command}" if command.exclude?("/") && which(command).nil?
if subcommand == "env"
ENV.each do |key, value|
puts "export #{key}=\"#{value}\""

View File

@ -77,7 +77,7 @@ RSpec.describe Homebrew::Bundle::Commands::Exec do
it "prepends the path of the requested command to PATH before running" do
expect(described_class).to receive(:exec).with("bundle", "install").and_return(nil)
expect(described_class).to receive(:which).and_return(Pathname("/usr/local/bin/bundle"))
expect(described_class).to receive(:which).twice.and_return(Pathname("/usr/local/bin/bundle"))
allow(ENV).to receive(:prepend_path).with(any_args).and_call_original
expect(ENV).to receive(:prepend_path).with("PATH", "/usr/local/bin").once.and_call_original
described_class.run("bundle", "install")