diff --git a/lib/bundler.rb b/lib/bundler.rb index c1d7c46e4d..1140de5467 100644 --- a/lib/bundler.rb +++ b/lib/bundler.rb @@ -495,18 +495,27 @@ module Bundler end def which(executable) - if File.file?(executable) && File.executable?(executable) - executable - elsif paths = ENV["PATH"] + executable_path = find_executable(executable) + return executable_path if executable_path + + if (paths = ENV["PATH"]) quote = '"' paths.split(File::PATH_SEPARATOR).find do |path| path = path[1..-2] if path.start_with?(quote) && path.end_with?(quote) - executable_path = File.expand_path(executable, path) - return executable_path if File.file?(executable_path) && File.executable?(executable_path) + executable_path = find_executable(File.expand_path(executable, path)) + return executable_path if executable_path end end end + def find_executable(path) + extensions = RbConfig::CONFIG["EXECUTABLE_EXTS"]&.split + extensions = [RbConfig::CONFIG["EXEEXT"]] unless extensions&.any? + candidates = extensions.map {|ext| "#{path}#{ext}" } + + candidates.find {|candidate| File.file?(candidate) && File.executable?(candidate) } + end + def read_file(file) SharedHelpers.filesystem_access(file, :read) do File.open(file, "r:UTF-8", &:read) @@ -559,7 +568,7 @@ module Bundler def git_present? return @git_present if defined?(@git_present) - @git_present = Bundler.which("git#{RbConfig::CONFIG["EXEEXT"]}") + @git_present = Bundler.which("git") end def feature_flag diff --git a/spec/bundler/bundler/bundler_spec.rb b/spec/bundler/bundler/bundler_spec.rb index 7cfc12a6f6..27bcb92659 100644 --- a/spec/bundler/bundler/bundler_spec.rb +++ b/spec/bundler/bundler/bundler_spec.rb @@ -174,7 +174,13 @@ RSpec.describe Bundler do end end - let(:expected) { "executable" } + let(:expected) do + if Gem.win_platform? + "executable.exe" + else + "executable" + end + end before do ENV["PATH"] = path.join(File::PATH_SEPARATOR) @@ -200,7 +206,7 @@ RSpec.describe Bundler do context "when the executable in inside a quoted path" do let(:expected) do if Gem.win_platform? - "C:/e/executable" + "C:/e/executable.exe" else "/e/executable" end