Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 7daa4ad

Browse files
committed
bundle/commands/exec: improve PATH handling.
- don't add `pkg-config` to the PATH, it's not needed as a shim already exists in Homebrew for this and we're overriding it here - any package with a version specified in an environment variable should be prepended to the PATH, not appended
1 parent f1844ed commit 7daa4ad

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

lib/bundle/commands/exec.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require "exceptions"
55
require "extend/ENV"
66
require "utils"
7+
require "PATH"
78

89
module Bundle
910
module Commands
@@ -98,10 +99,6 @@ def run(*args, global: false, file: nil, subcommand: "")
9899
ENV.prepend_path "PATH", Pathname.new(dep_root)/"shims"
99100
end
100101

101-
# Setup pkg-config, if present, to help locate packages
102-
pkgconfig = Formulary.factory("pkg-config")
103-
ENV.prepend_path "PATH", pkgconfig.opt_bin.to_s if pkgconfig.any_version_installed?
104-
105102
# Ensure the Ruby path we saved goes before anything else, if the command was in the PATH
106103
ENV.prepend_path "PATH", command_path if command_path.present?
107104

@@ -122,7 +119,21 @@ def run(*args, global: false, file: nil, subcommand: "")
122119
opt = %r{/opt/#{formula_name}([/:$])}
123120
next unless value.match(opt)
124121

125-
ENV[key] = value.gsub(opt, "/Cellar/#{formula_name}/#{formula_version}\\1")
122+
cellar = "/Cellar/#{formula_name}/#{formula_version}\\1"
123+
124+
if key == "PATH"
125+
rejected_opts = []
126+
path = PATH.new(ENV.fetch("PATH"))
127+
.reject do |value|
128+
rejected_opts << value if value.match?(opt)
129+
end
130+
rejected_opts.each do |value|
131+
path.prepend(value.gsub(opt, cellar))
132+
end
133+
ENV["PATH"] = path.to_s
134+
else
135+
ENV[key] = value.gsub(opt, cellar)
136+
end
126137
end
127138
end
128139

spec/stub/PATH.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
class PATH
4+
def initialize(*paths)
5+
@paths = parse(paths)
6+
end
7+
8+
def prepend(*paths)
9+
@paths = parse(paths + @paths)
10+
end
11+
12+
def reject(&block)
13+
self.class.new(@paths.reject(&block))
14+
end
15+
16+
def to_s
17+
@paths.join(File::PATH_SEPARATOR)
18+
end
19+
20+
private
21+
22+
def parse(paths)
23+
paths.flatten
24+
.compact
25+
.flat_map { |p| Pathname(p).to_path.split(File::PATH_SEPARATOR) }
26+
.uniq
27+
end
28+
end

0 commit comments

Comments
 (0)