Skip to content

Commit fec5b64

Browse files
authored
Merge pull request #19547 from Homebrew/bundle-exec-path-lookup
bundle/exec: fix command `PATH` lookup
2 parents 14fe53b + a4f2797 commit fec5b64

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

Library/Homebrew/bundle/commands/exec.rb

+8-11
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,6 @@ def run(*args, global: false, file: nil, subcommand: "")
5959

6060
command = args.first
6161

62-
# For commands which aren't either absolute or relative
63-
if command.exclude? "/"
64-
# Save the command path, since this will be blown away by superenv
65-
command_path = which(command)
66-
raise "command was not found in your PATH: #{command}" if command_path.blank?
67-
68-
command_path = command_path.dirname.to_s
69-
end
70-
7162
@dsl = Brewfile.read(global:, file:)
7263

7364
require "formula"
@@ -111,8 +102,11 @@ def run(*args, global: false, file: nil, subcommand: "")
111102
end
112103
# rubocop:enable Homebrew/MoveToExtendOS
113104

114-
# Ensure the Ruby path we saved goes before anything else, if the command was in the PATH
115-
ENV.prepend_path "PATH", command_path if command_path.present?
105+
# For commands which aren't either absolute or relative
106+
# Add the command directory to PATH, since it may get blown away by superenv
107+
if command.exclude?("/") && (which_command = which(command)).present?
108+
ENV.prepend_path "PATH", which_command.dirname.to_s
109+
end
116110

117111
# Replace the formula versions from the environment variables
118112
formula_versions = {}
@@ -155,6 +149,9 @@ def run(*args, global: false, file: nil, subcommand: "")
155149
ENV.append_path "PATH", homebrew_path
156150
end
157151

152+
# For commands which aren't either absolute or relative
153+
raise "command was not found in your PATH: #{command}" if command.exclude?("/") && which(command).nil?
154+
158155
if subcommand == "env"
159156
ENV.each do |key, value|
160157
puts "export #{key}=\"#{value}\""

Library/Homebrew/test/bundle/commands/exec_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777

7878
it "prepends the path of the requested command to PATH before running" do
7979
expect(described_class).to receive(:exec).with("bundle", "install").and_return(nil)
80-
expect(described_class).to receive(:which).and_return(Pathname("/usr/local/bin/bundle"))
80+
expect(described_class).to receive(:which).twice.and_return(Pathname("/usr/local/bin/bundle"))
8181
allow(ENV).to receive(:prepend_path).with(any_args).and_call_original
8282
expect(ENV).to receive(:prepend_path).with("PATH", "/usr/local/bin").once.and_call_original
8383
described_class.run("bundle", "install")

0 commit comments

Comments
 (0)