diff --git a/lib/bundle/commands/exec.rb b/lib/bundle/commands/exec.rb index a7c695be6..ddbb816d6 100644 --- a/lib/bundle/commands/exec.rb +++ b/lib/bundle/commands/exec.rb @@ -35,7 +35,15 @@ def run(*args, global: false, file: nil) Formulary.factory(entry.name) end - ENV.keg_only_deps = ENV.deps.select(&:keg_only?) + + # Allow setting all dependencies to be keg-only + # (i.e. should be explicitly in HOMEBREW_*PATHs ahead of HOMEBREW_PREFIX) + ENV.keg_only_deps = if ENV["HOMEBREW_BUNDLE_EXEC_ALL_KEG_ONLY_DEPS"].present? + ENV.delete("HOMEBREW_BUNDLE_EXEC_ALL_KEG_ONLY_DEPS") + ENV.deps + else + ENV.deps.select(&:keg_only?) + end ENV.setup_build_environment # Enable compiler flag filtering @@ -58,6 +66,27 @@ def run(*args, global: false, file: nil) # 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? + # Replace the formula versions from the environment variables + formula_versions = {} + ENV.each do |key, value| + match = key.match(/^HOMEBREW_BUNDLE_EXEC_FORMULA_VERSION_(.+)$/) + next if match.blank? + + formula_name = match[1] + next if formula_name.blank? + + ENV.delete(key) + formula_versions[formula_name.downcase] = value + end + formula_versions.each do |formula_name, formula_version| + ENV.each do |key, value| + opt = %r{/opt/#{formula_name}([/:$])} + next unless value.match(opt) + + ENV[key] = value.gsub(opt, "/Cellar/#{formula_name}/#{formula_version}\\1") + end + end + exec(*args) end end