diff --git a/Library/Homebrew/bundle/commands/exec.rb b/Library/Homebrew/bundle/commands/exec.rb index 2babca0d67..c3b3553be6 100644 --- a/Library/Homebrew/bundle/commands/exec.rb +++ b/Library/Homebrew/bundle/commands/exec.rb @@ -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}\"" diff --git a/Library/Homebrew/test/bundle/commands/exec_spec.rb b/Library/Homebrew/test/bundle/commands/exec_spec.rb index f7ae3e9d2b..b68b991f61 100644 --- a/Library/Homebrew/test/bundle/commands/exec_spec.rb +++ b/Library/Homebrew/test/bundle/commands/exec_spec.rb @@ -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")