Use ORIGINAL_PATHS over envs; reject nil PATH

This commit is contained in:
Bo Anderson 2022-06-15 05:40:43 +01:00
parent 8ada737d40
commit 02164a35db
No known key found for this signature in database
GPG Key ID: 3DB94E204E137D65
37 changed files with 66 additions and 65 deletions

View File

@ -53,8 +53,8 @@ begin
args = Homebrew::CLI::Parser.new.parse(ARGV.dup.freeze, ignore_invalid_options: true)
Context.current = args.context
path = PATH.new(ENV["PATH"])
homebrew_path = PATH.new(ENV["HOMEBREW_PATH"])
path = PATH.new(ENV.fetch("PATH"))
homebrew_path = PATH.new(ENV.fetch("HOMEBREW_PATH"))
# Add shared wrappers.
path.prepend(HOMEBREW_SHIMS_PATH/"shared")

View File

@ -39,7 +39,7 @@ module Cask
executable_path,
**args,
env: { "PATH" => PATH.new(
HOMEBREW_PREFIX/"bin", HOMEBREW_PREFIX/"sbin", ENV["PATH"]
HOMEBREW_PREFIX/"bin", HOMEBREW_PREFIX/"sbin", ENV.fetch("PATH")
) },
)
end

View File

@ -78,7 +78,7 @@ class Caveats
s << " #{Utils::Shell.export_value("CPPFLAGS", "-I#{f.opt_include}")}\n" if f.include.directory?
if which("pkg-config", ENV["HOMEBREW_PATH"]) &&
if which("pkg-config", ORIGINAL_PATHS) &&
((f.lib/"pkgconfig").directory? || (f.share/"pkgconfig").directory?)
s << <<~EOS
@ -109,7 +109,7 @@ class Caveats
def function_completion_caveats(shell)
return unless keg
return unless which(shell.to_s, ENV["HOMEBREW_PATH"])
return unless which(shell.to_s, ORIGINAL_PATHS)
completion_installed = keg.completion_installed?(shell)
functions_installed = keg.functions_installed?(shell)

View File

@ -391,7 +391,7 @@ module Homebrew
end
def cleanup_portable_ruby
rubies = [which("ruby"), which("ruby", ENV["HOMEBREW_PATH"])].compact
rubies = [which("ruby"), which("ruby", ORIGINAL_PATHS)].compact
system_ruby = Pathname.new("/usr/bin/ruby")
rubies << system_ruby if system_ruby.exist?

View File

@ -42,7 +42,7 @@ module Homebrew
# As this command is simplifying user-run commands then let's just use a
# user path, too.
ENV["PATH"] = ENV["HOMEBREW_PATH"]
ENV["PATH"] = PATH.new(ORIGINAL_PATHS).to_s
if args.no_named?
git_log HOMEBREW_REPOSITORY, args: args

View File

@ -93,11 +93,11 @@ module Commands
# Ruby commands which are run by being `require`d.
def external_ruby_cmd_path(cmd)
which("brew-#{cmd}.rb", PATH.new(ENV["PATH"]).append(Tap.cmd_directories))
which("brew-#{cmd}.rb", PATH.new(ENV.fetch("PATH")).append(Tap.cmd_directories))
end
def external_cmd_path(cmd)
which("brew-#{cmd}", PATH.new(ENV["PATH"]).append(Tap.cmd_directories))
which("brew-#{cmd}", PATH.new(ENV.fetch("PATH")).append(Tap.cmd_directories))
end
def path(cmd)

View File

@ -67,7 +67,7 @@ module Homebrew
# As this command is simplifying user-run commands then let's just use a
# user path, too.
ENV["PATH"] = ENV["HOMEBREW_PATH"]
ENV["PATH"] = PATH.new(ORIGINAL_PATHS).to_s
# Use the user's browser, too.
ENV["BROWSER"] = Homebrew::EnvConfig.browser

View File

@ -108,7 +108,7 @@ module Homebrew
# As this command is simplifying user-run commands then let's just use a
# user path, too.
ENV["PATH"] = ENV["HOMEBREW_PATH"]
ENV["PATH"] = PATH.new(ORIGINAL_PATHS).to_s
# Use the user's browser, too.
ENV["BROWSER"] = Homebrew::EnvConfig.browser

View File

@ -36,7 +36,7 @@ module Homebrew
# As this command is simplifying user-run commands then let's just use a
# user path, too.
ENV["PATH"] = ENV["HOMEBREW_PATH"]
ENV["PATH"] = PATH.new(ORIGINAL_PATHS).to_s
args.named.to_formulae.each do |formula|
current_revision = formula.revision

View File

@ -38,7 +38,7 @@ module Homebrew
ENV.setup_build_environment
if superenv?(args.env)
# superenv stopped adding brew's bin but generally users will want it
ENV["PATH"] = PATH.new(ENV["PATH"]).insert(1, HOMEBREW_PREFIX/"bin")
ENV["PATH"] = PATH.new(ENV.fetch("PATH")).insert(1, HOMEBREW_PREFIX/"bin")
end
ENV["VERBOSE"] = "1" if args.verbose?

View File

@ -123,7 +123,7 @@ module Homebrew
safe_system "git", "reset", "--hard", start_commit
# update ENV["PATH"]
ENV["PATH"] = PATH.new(ENV["PATH"]).prepend(curdir/"bin")
ENV["PATH"] = PATH.new(ENV.fetch("PATH")).prepend(curdir/"bin")
# run brew help to install portable-ruby (if needed)
quiet_system "brew", "help"

View File

@ -1139,7 +1139,7 @@ class CVSDownloadStrategy < VCSDownloadStrategy
private
def env
{ "PATH" => PATH.new("/usr/bin", Formula["cvs"].opt_bin, ENV["PATH"]) }
{ "PATH" => PATH.new("/usr/bin", Formula["cvs"].opt_bin, ENV.fetch("PATH")) }
end
sig { returns(String) }
@ -1214,7 +1214,7 @@ class MercurialDownloadStrategy < VCSDownloadStrategy
private
def env
{ "PATH" => PATH.new(Formula["mercurial"].opt_bin, ENV["PATH"]) }
{ "PATH" => PATH.new(Formula["mercurial"].opt_bin, ENV.fetch("PATH")) }
end
sig { returns(String) }
@ -1280,7 +1280,7 @@ class BazaarDownloadStrategy < VCSDownloadStrategy
def env
{
"PATH" => PATH.new(Formula["bazaar"].opt_bin, ENV["PATH"]),
"PATH" => PATH.new(Formula["bazaar"].opt_bin, ENV.fetch("PATH")),
"BZR_HOME" => HOMEBREW_TEMP,
}
end
@ -1345,7 +1345,7 @@ class FossilDownloadStrategy < VCSDownloadStrategy
private
def env
{ "PATH" => PATH.new(Formula["fossil"].opt_bin, ENV["PATH"]) }
{ "PATH" => PATH.new(Formula["fossil"].opt_bin, ENV.fetch("PATH")) }
end
sig { returns(String) }

View File

@ -28,7 +28,7 @@ module Stdenv
self["HOMEBREW_ENV"] = "std"
PATH.new(ENV["HOMEBREW_PATH"]).reverse_each { |p| prepend_path "PATH", p }
ORIGINAL_PATHS.reverse_each { |p| prepend_path "PATH", p }
prepend_path "PATH", HOMEBREW_SHIMS_PATH/"shared"
# Set the default pkg-config search path, overriding the built-in paths

View File

@ -421,9 +421,9 @@ class Formula
return unless head.downloader.cached_location.exist?
path = if ENV["HOMEBREW_ENV"]
ENV["PATH"]
ENV.fetch("PATH")
else
ENV["HOMEBREW_PATH"]
PATH.new(ORIGINAL_PATHS)
end
with_env(PATH: path) do
@ -1103,7 +1103,7 @@ class Formula
TMP: HOMEBREW_TEMP,
_JAVA_OPTIONS: "-Djava.io.tmpdir=#{HOMEBREW_TEMP}",
HOMEBREW_PATH: nil,
PATH: ENV["HOMEBREW_PATH"],
PATH: PATH.new(ORIGINAL_PATHS),
}
with_env(new_env) do
@ -2077,7 +2077,7 @@ class Formula
TEMP: HOMEBREW_TEMP,
TMP: HOMEBREW_TEMP,
TERM: "dumb",
PATH: PATH.new(ENV["PATH"], HOMEBREW_PREFIX/"bin"),
PATH: PATH.new(ENV.fetch("PATH"), HOMEBREW_PREFIX/"bin"),
HOMEBREW_PATH: nil,
}.merge(common_stage_test_env)
test_env[:_JAVA_OPTIONS] += " -Djava.io.tmpdir=#{HOMEBREW_TEMP}"

View File

@ -299,7 +299,7 @@ module FormulaCellarChecks
objdump = Formula["llvm"].opt_bin/"llvm-objdump" if Formula["llvm"].any_version_installed?
objdump ||= Formula["binutils"].opt_bin/"objdump" if Formula["binutils"].any_version_installed?
objdump ||= which("objdump")
objdump ||= which("objdump", ENV["HOMEBREW_PATH"])
objdump ||= which("objdump", ORIGINAL_PATHS)
unless objdump
return <<~EOS

View File

@ -119,8 +119,8 @@ require "cli/args"
require "PATH"
ENV["HOMEBREW_PATH"] ||= ENV["PATH"]
ORIGINAL_PATHS = PATH.new(ENV["HOMEBREW_PATH"]).map do |p|
ENV["HOMEBREW_PATH"] ||= ENV.fetch("PATH")
ORIGINAL_PATHS = PATH.new(ENV.fetch("HOMEBREW_PATH")).map do |p|
Pathname.new(p).expand_path
rescue
nil

View File

@ -103,7 +103,7 @@ class Requirement
parent = satisfied_result_parent
return unless parent
return if ["#{HOMEBREW_PREFIX}/bin", "#{HOMEBREW_PREFIX}/bin"].include?(parent.to_s)
return if PATH.new(ENV["PATH"]).include?(parent.to_s)
return if PATH.new(ENV.fetch("PATH")).include?(parent.to_s)
ENV.prepend_path("PATH", parent)
end

View File

@ -34,7 +34,7 @@ describe Cask::Artifact::Installer, :cask do
expect(command).to receive(:run!).with(
executable,
a_hash_including(
env: { "PATH" => PATH.new("#{HOMEBREW_PREFIX}/bin", "#{HOMEBREW_PREFIX}/sbin", ENV["PATH"]) },
env: { "PATH" => PATH.new("#{HOMEBREW_PREFIX}/bin", "#{HOMEBREW_PREFIX}/sbin", ENV.fetch("PATH")) },
),
)

View File

@ -13,7 +13,7 @@ describe "brew custom-external-command", :integration_test do
SH
FileUtils.chmod "+x", file
expect { brew cmd, "PATH" => "#{path}#{File::PATH_SEPARATOR}#{ENV["PATH"]}" }
expect { brew cmd, "PATH" => "#{path}#{File::PATH_SEPARATOR}#{ENV.fetch("PATH")}" }
.to output("I am #{cmd}.\n").to_stdout
.and not_to_output.to_stderr
.and be_a_success

View File

@ -20,7 +20,7 @@ describe Homebrew::Diagnostic::Checks do
FileUtils.chmod 0755, anaconda
FileUtils.chmod 0755, python
ENV["PATH"] = "#{path}#{File::PATH_SEPARATOR}#{ENV["PATH"]}"
ENV["PATH"] = "#{path}#{File::PATH_SEPARATOR}#{ENV.fetch("PATH")}"
expect(checks.check_for_anaconda).to match("Anaconda")
end
@ -75,10 +75,12 @@ describe Homebrew::Diagnostic::Checks do
specify "#check_user_path_3" do
sbin = HOMEBREW_PREFIX/"sbin"
ENV["HOMEBREW_PATH"] =
(sbin/"something").mkpath
homebrew_path =
"#{HOMEBREW_PREFIX}/bin#{File::PATH_SEPARATOR}" +
ENV["HOMEBREW_PATH"].gsub(/(?:^|#{Regexp.escape(File::PATH_SEPARATOR)})#{Regexp.escape(sbin)}/, "")
(sbin/"something").mkpath
stub_const("ORIGINAL_PATHS", PATH.new(homebrew_path).map { |path| Pathname.new(path).expand_path }.compact)
expect(checks.check_user_path_1).to be_nil
expect(checks.check_user_path_2).to be_nil
@ -93,8 +95,7 @@ describe Homebrew::Diagnostic::Checks do
file = "#{path}/foo-config"
FileUtils.touch file
FileUtils.chmod 0755, file
ENV["HOMEBREW_PATH"] = ENV["PATH"] =
"#{path}#{File::PATH_SEPARATOR}#{ENV["PATH"]}"
ENV["PATH"] = "#{path}#{File::PATH_SEPARATOR}#{ENV.fetch("PATH")}"
expect(checks.check_for_config_scripts)
.to match('"config" scripts exist')

View File

@ -150,7 +150,7 @@ RSpec.configure do |config|
skip "Subversion is not installed." unless quiet_system svn_shim, "--version"
svn_shim_path = Pathname(Utils.popen_read(svn_shim, "--homebrew=print-path").chomp.presence)
svn_paths = PATH.new(ENV["PATH"])
svn_paths = PATH.new(ENV.fetch("PATH"))
svn_paths.prepend(svn_shim_path.dirname)
if OS.mac?
@ -164,7 +164,7 @@ RSpec.configure do |config|
svnadmin = which("svnadmin", svn_paths)
skip "svnadmin is not installed." unless svnadmin
ENV["PATH"] = PATH.new(ENV["PATH"])
ENV["PATH"] = PATH.new(ENV.fetch("PATH"))
.append(svn.dirname)
.append(svnadmin.dirname)
end

View File

@ -73,7 +73,7 @@ RSpec.shared_context "integration test" do # rubocop:disable RSpec/ContextWordin
env["PATH"],
(HOMEBREW_LIBRARY_PATH/"test/support/helper/cmd").realpath.to_s,
(HOMEBREW_PREFIX/"bin").realpath.to_s,
ENV["PATH"],
ENV.fetch("PATH"),
].compact.join(File::PATH_SEPARATOR)
env.merge!(

View File

@ -274,17 +274,19 @@ describe "globally-scoped helper methods" do
describe "#with_env" do
it "sets environment variables within the block" do
expect(ENV["PATH"]).not_to eq("/bin")
expect(ENV.fetch("PATH")).not_to eq("/bin")
with_env(PATH: "/bin") do
expect(ENV["PATH"]).to eq("/bin")
expect(ENV.fetch("PATH", nil)).to eq("/bin")
end
end
it "restores ENV after the block" do
with_env(PATH: "/bin") do
expect(ENV["PATH"]).to eq("/bin")
expect(ENV.fetch("PATH", nil)).to eq("/bin")
end
expect(ENV["PATH"]).not_to eq("/bin")
path = ENV.fetch("PATH", nil)
expect(path).not_to be_nil
expect(path).not_to eq("/bin")
end
it "restores ENV if an exception is raised" do
@ -294,7 +296,9 @@ describe "globally-scoped helper methods" do
end
}.to raise_error(StandardError)
expect(ENV["PATH"]).not_to eq("/bin")
path = ENV.fetch("PATH", nil)
expect(path).not_to be_nil
expect(path).not_to eq("/bin")
end
end

View File

@ -23,7 +23,7 @@ module UnpackStrategy
def extract_to_dir(unpack_dir, basename:, verbose:)
system_command! "cabextract",
args: ["-d", unpack_dir, "--", path],
env: { "PATH" => PATH.new(Formula["cabextract"].opt_bin, ENV["PATH"]) },
env: { "PATH" => PATH.new(Formula["cabextract"].opt_bin, ENV.fetch("PATH")) },
verbose: verbose
end

View File

@ -39,7 +39,7 @@ module UnpackStrategy
system_command! "fossil",
args: ["open", path, *args],
chdir: unpack_dir,
env: { "PATH" => PATH.new(Formula["fossil"].opt_bin, ENV["PATH"]) },
env: { "PATH" => PATH.new(Formula["fossil"].opt_bin, ENV.fetch("PATH")) },
verbose: verbose
end
end

View File

@ -32,7 +32,7 @@ module UnpackStrategy
"-force-overwrite", "-quiet", "-no-directory",
"-output-directory", unpack_dir, "--", path
],
env: { "PATH" => PATH.new(Formula["unar"].opt_bin, ENV["PATH"]) },
env: { "PATH" => PATH.new(Formula["unar"].opt_bin, ENV.fetch("PATH")) },
verbose: verbose
end
end

View File

@ -29,7 +29,7 @@ module UnpackStrategy
def extract_to_dir(unpack_dir, basename:, verbose:)
system_command! "lha",
args: ["xq2w=#{unpack_dir}", path],
env: { "PATH" => PATH.new(Formula["lha"].opt_bin, ENV["PATH"]) },
env: { "PATH" => PATH.new(Formula["lha"].opt_bin, ENV.fetch("PATH")) },
verbose: verbose
end
end

View File

@ -31,7 +31,7 @@ module UnpackStrategy
quiet_flags = verbose ? [] : ["-q"]
system_command! "lzip",
args: ["-d", *quiet_flags, unpack_dir/basename],
env: { "PATH" => PATH.new(Formula["lzip"].opt_bin, ENV["PATH"]) },
env: { "PATH" => PATH.new(Formula["lzip"].opt_bin, ENV.fetch("PATH")) },
verbose: verbose
end
end

View File

@ -31,7 +31,7 @@ module UnpackStrategy
quiet_flags = verbose ? [] : ["-q"]
system_command! "unlzma",
args: [*quiet_flags, "--", unpack_dir/basename],
env: { "PATH" => PATH.new(Formula["xz"].opt_bin, ENV["PATH"]) },
env: { "PATH" => PATH.new(Formula["xz"].opt_bin, ENV.fetch("PATH")) },
verbose: verbose
end
end

View File

@ -17,7 +17,7 @@ module UnpackStrategy
def extract_to_dir(unpack_dir, basename:, verbose:)
system_command! "hg",
args: ["--cwd", path, "archive", "--subrepos", "-y", "-t", "files", unpack_dir],
env: { "PATH" => PATH.new(Formula["mercurial"].opt_bin, ENV["PATH"]) },
env: { "PATH" => PATH.new(Formula["mercurial"].opt_bin, ENV.fetch("PATH")) },
verbose: verbose
end
end

View File

@ -29,7 +29,7 @@ module UnpackStrategy
def extract_to_dir(unpack_dir, basename:, verbose:)
system_command! "7zr",
args: ["x", "-y", "-bd", "-bso0", path, "-o#{unpack_dir}"],
env: { "PATH" => PATH.new(Formula["p7zip"].opt_bin, ENV["PATH"]) },
env: { "PATH" => PATH.new(Formula["p7zip"].opt_bin, ENV.fetch("PATH")) },
verbose: verbose
end
end

View File

@ -29,7 +29,7 @@ module UnpackStrategy
def extract_to_dir(unpack_dir, basename:, verbose:)
system_command! "unrar",
args: ["x", "-inul", path, unpack_dir],
env: { "PATH" => PATH.new(Formula["unrar"].opt_bin, ENV["PATH"]) },
env: { "PATH" => PATH.new(Formula["unrar"].opt_bin, ENV.fetch("PATH")) },
verbose: verbose
end
end

View File

@ -31,7 +31,7 @@ module UnpackStrategy
quiet_flags = verbose ? [] : ["-q"]
system_command! "unxz",
args: [*quiet_flags, "-T0", "--", unpack_dir/basename],
env: { "PATH" => PATH.new(Formula["xz"].opt_bin, ENV["PATH"]) },
env: { "PATH" => PATH.new(Formula["xz"].opt_bin, ENV.fetch("PATH")) },
verbose: verbose
end
end

View File

@ -37,7 +37,7 @@ module UnpackStrategy
quiet_flags = verbose ? [] : ["-qq"]
result = system_command! "unzip",
args: [*quiet_flags, "-o", path, "-d", unpack_dir],
env: { "PATH" => PATH.new(unzip&.opt_bin, ENV["PATH"]) },
env: { "PATH" => PATH.new(unzip&.opt_bin, ENV.fetch("PATH")) },
verbose: verbose,
print_stderr: false

View File

@ -31,7 +31,7 @@ module UnpackStrategy
quiet_flags = verbose ? [] : ["-q"]
system_command! "unzstd",
args: [*quiet_flags, "-T0", "--rm", "--", unpack_dir/basename],
env: { "PATH" => PATH.new(Formula["zstd"].opt_bin, ENV["PATH"]) },
env: { "PATH" => PATH.new(Formula["zstd"].opt_bin, ENV.fetch("PATH")) },
verbose: verbose
end
end

View File

@ -305,7 +305,7 @@ module Kernel
end
def with_homebrew_path(&block)
with_env(PATH: PATH.new(ENV["HOMEBREW_PATH"]), &block)
with_env(PATH: PATH.new(ORIGINAL_PATHS), &block)
end
def with_custom_locale(locale, &block)
@ -329,7 +329,7 @@ module Kernel
end
end
def which(cmd, path = ENV["PATH"])
def which(cmd, path = ENV.fetch("PATH"))
PATH.new(path).each do |p|
begin
pcmd = File.expand_path(cmd, p)
@ -343,7 +343,7 @@ module Kernel
nil
end
def which_all(cmd, path = ENV["PATH"])
def which_all(cmd, path = ENV.fetch("PATH"))
PATH.new(path).map do |p|
begin
pcmd = File.expand_path(cmd, p)
@ -362,7 +362,7 @@ module Kernel
# Find Atom, Sublime Text, VS Code, Textmate, BBEdit / TextWrangler, or vim
editor = %w[atom subl code mate edit vim].find do |candidate|
candidate if which(candidate, ENV["HOMEBREW_PATH"])
candidate if which(candidate, ORIGINAL_PATHS)
end
editor ||= "vim"
@ -499,7 +499,7 @@ module Kernel
executable = [
which(name),
which(name, ENV["HOMEBREW_PATH"]),
which(name, ORIGINAL_PATHS),
HOMEBREW_PREFIX/"bin/#{name}",
].compact.first
return executable if executable.exist?
@ -508,11 +508,7 @@ module Kernel
end
def paths
@paths ||= PATH.new(ENV["HOMEBREW_PATH"]).map do |p|
File.expand_path(p).chomp("/")
rescue ArgumentError
onoe "The following PATH component is invalid: #{p}"
end.uniq.compact
@paths ||= ORIGINAL_PATHS.uniq.map(&:to_s)
end
def parse_author!(author)

View File

@ -124,7 +124,7 @@ module Utils
gnupg_bin = HOMEBREW_PREFIX/"opt/gnupg/bin"
return unless gnupg_bin.directory?
ENV["PATH"] = PATH.new(ENV["PATH"])
ENV["PATH"] = PATH.new(ENV.fetch("PATH"))
.prepend(gnupg_bin)
end