From 02164a35dbc2320e9f4eb2e27ff952e0a157b6fa Mon Sep 17 00:00:00 2001 From: Bo Anderson Date: Wed, 15 Jun 2022 05:40:43 +0100 Subject: [PATCH] Use ORIGINAL_PATHS over envs; reject nil PATH --- Library/Homebrew/brew.rb | 4 ++-- Library/Homebrew/cask/artifact/installer.rb | 2 +- Library/Homebrew/caveats.rb | 4 ++-- Library/Homebrew/cleanup.rb | 2 +- Library/Homebrew/cmd/log.rb | 2 +- Library/Homebrew/commands.rb | 4 ++-- Library/Homebrew/dev-cmd/bump-cask-pr.rb | 2 +- Library/Homebrew/dev-cmd/bump-formula-pr.rb | 2 +- Library/Homebrew/dev-cmd/bump-revision.rb | 2 +- Library/Homebrew/dev-cmd/sh.rb | 2 +- Library/Homebrew/dev-cmd/update-test.rb | 2 +- Library/Homebrew/download_strategy.rb | 8 ++++---- Library/Homebrew/extend/ENV/std.rb | 2 +- Library/Homebrew/formula.rb | 8 ++++---- Library/Homebrew/formula_cellar_checks.rb | 2 +- Library/Homebrew/global.rb | 4 ++-- Library/Homebrew/requirement.rb | 2 +- .../test/cask/artifact/installer_spec.rb | 2 +- .../test/cmd/custom-external-command_spec.rb | 2 +- Library/Homebrew/test/diagnostic_checks_spec.rb | 11 ++++++----- Library/Homebrew/test/spec_helper.rb | 4 ++-- .../spec/shared_context/integration_test.rb | 2 +- Library/Homebrew/test/utils_spec.rb | 14 +++++++++----- Library/Homebrew/unpack_strategy/cab.rb | 2 +- Library/Homebrew/unpack_strategy/fossil.rb | 2 +- Library/Homebrew/unpack_strategy/generic_unar.rb | 2 +- Library/Homebrew/unpack_strategy/lha.rb | 2 +- Library/Homebrew/unpack_strategy/lzip.rb | 2 +- Library/Homebrew/unpack_strategy/lzma.rb | 2 +- Library/Homebrew/unpack_strategy/mercurial.rb | 2 +- Library/Homebrew/unpack_strategy/p7zip.rb | 2 +- Library/Homebrew/unpack_strategy/rar.rb | 2 +- Library/Homebrew/unpack_strategy/xz.rb | 2 +- Library/Homebrew/unpack_strategy/zip.rb | 2 +- Library/Homebrew/unpack_strategy/zstd.rb | 2 +- Library/Homebrew/utils.rb | 16 ++++++---------- Library/Homebrew/utils/git.rb | 2 +- 37 files changed, 66 insertions(+), 65 deletions(-) diff --git a/Library/Homebrew/brew.rb b/Library/Homebrew/brew.rb index 6f9778109a..b514d00621 100644 --- a/Library/Homebrew/brew.rb +++ b/Library/Homebrew/brew.rb @@ -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") diff --git a/Library/Homebrew/cask/artifact/installer.rb b/Library/Homebrew/cask/artifact/installer.rb index 91ffe92f57..95c57418f4 100644 --- a/Library/Homebrew/cask/artifact/installer.rb +++ b/Library/Homebrew/cask/artifact/installer.rb @@ -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 diff --git a/Library/Homebrew/caveats.rb b/Library/Homebrew/caveats.rb index 690cda4959..66b4fcf1a7 100644 --- a/Library/Homebrew/caveats.rb +++ b/Library/Homebrew/caveats.rb @@ -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) diff --git a/Library/Homebrew/cleanup.rb b/Library/Homebrew/cleanup.rb index e0d65cefc3..4eb4689c9b 100644 --- a/Library/Homebrew/cleanup.rb +++ b/Library/Homebrew/cleanup.rb @@ -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? diff --git a/Library/Homebrew/cmd/log.rb b/Library/Homebrew/cmd/log.rb index 69a435fe0a..9602e87d7c 100644 --- a/Library/Homebrew/cmd/log.rb +++ b/Library/Homebrew/cmd/log.rb @@ -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 diff --git a/Library/Homebrew/commands.rb b/Library/Homebrew/commands.rb index 64b0dfa7f6..383c5cb768 100644 --- a/Library/Homebrew/commands.rb +++ b/Library/Homebrew/commands.rb @@ -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) diff --git a/Library/Homebrew/dev-cmd/bump-cask-pr.rb b/Library/Homebrew/dev-cmd/bump-cask-pr.rb index bf3193b397..e1d4e304b5 100644 --- a/Library/Homebrew/dev-cmd/bump-cask-pr.rb +++ b/Library/Homebrew/dev-cmd/bump-cask-pr.rb @@ -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 diff --git a/Library/Homebrew/dev-cmd/bump-formula-pr.rb b/Library/Homebrew/dev-cmd/bump-formula-pr.rb index 988236544f..df48b5905d 100644 --- a/Library/Homebrew/dev-cmd/bump-formula-pr.rb +++ b/Library/Homebrew/dev-cmd/bump-formula-pr.rb @@ -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 diff --git a/Library/Homebrew/dev-cmd/bump-revision.rb b/Library/Homebrew/dev-cmd/bump-revision.rb index 29113aa938..897d8e67cb 100644 --- a/Library/Homebrew/dev-cmd/bump-revision.rb +++ b/Library/Homebrew/dev-cmd/bump-revision.rb @@ -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 diff --git a/Library/Homebrew/dev-cmd/sh.rb b/Library/Homebrew/dev-cmd/sh.rb index d1eaf4c812..71699997fd 100644 --- a/Library/Homebrew/dev-cmd/sh.rb +++ b/Library/Homebrew/dev-cmd/sh.rb @@ -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? diff --git a/Library/Homebrew/dev-cmd/update-test.rb b/Library/Homebrew/dev-cmd/update-test.rb index a7b798d49e..4e090cc948 100644 --- a/Library/Homebrew/dev-cmd/update-test.rb +++ b/Library/Homebrew/dev-cmd/update-test.rb @@ -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" diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 3e69e4881e..a11c177654 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -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) } diff --git a/Library/Homebrew/extend/ENV/std.rb b/Library/Homebrew/extend/ENV/std.rb index e4b4abb6c8..ba4caa1a80 100644 --- a/Library/Homebrew/extend/ENV/std.rb +++ b/Library/Homebrew/extend/ENV/std.rb @@ -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 diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 30872d1fd0..25ff2c3920 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -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}" diff --git a/Library/Homebrew/formula_cellar_checks.rb b/Library/Homebrew/formula_cellar_checks.rb index 46d9ac63ef..ed02d4e48f 100644 --- a/Library/Homebrew/formula_cellar_checks.rb +++ b/Library/Homebrew/formula_cellar_checks.rb @@ -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 diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb index 2c0ac5d08a..08a898c3aa 100644 --- a/Library/Homebrew/global.rb +++ b/Library/Homebrew/global.rb @@ -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 diff --git a/Library/Homebrew/requirement.rb b/Library/Homebrew/requirement.rb index 2abb8dc0a9..de890cc9e0 100644 --- a/Library/Homebrew/requirement.rb +++ b/Library/Homebrew/requirement.rb @@ -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 diff --git a/Library/Homebrew/test/cask/artifact/installer_spec.rb b/Library/Homebrew/test/cask/artifact/installer_spec.rb index 7f47a31d9c..8d24d826ef 100644 --- a/Library/Homebrew/test/cask/artifact/installer_spec.rb +++ b/Library/Homebrew/test/cask/artifact/installer_spec.rb @@ -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")) }, ), ) diff --git a/Library/Homebrew/test/cmd/custom-external-command_spec.rb b/Library/Homebrew/test/cmd/custom-external-command_spec.rb index 84e735de63..c4bec250ac 100644 --- a/Library/Homebrew/test/cmd/custom-external-command_spec.rb +++ b/Library/Homebrew/test/cmd/custom-external-command_spec.rb @@ -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 diff --git a/Library/Homebrew/test/diagnostic_checks_spec.rb b/Library/Homebrew/test/diagnostic_checks_spec.rb index 17b34b0398..0821fbb4f2 100644 --- a/Library/Homebrew/test/diagnostic_checks_spec.rb +++ b/Library/Homebrew/test/diagnostic_checks_spec.rb @@ -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') diff --git a/Library/Homebrew/test/spec_helper.rb b/Library/Homebrew/test/spec_helper.rb index b58a6ab87c..2aef66d976 100644 --- a/Library/Homebrew/test/spec_helper.rb +++ b/Library/Homebrew/test/spec_helper.rb @@ -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 diff --git a/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb b/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb index 939ff4f086..b828228370 100644 --- a/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb +++ b/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb @@ -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!( diff --git a/Library/Homebrew/test/utils_spec.rb b/Library/Homebrew/test/utils_spec.rb index 64c068c02a..e26ebbacb9 100644 --- a/Library/Homebrew/test/utils_spec.rb +++ b/Library/Homebrew/test/utils_spec.rb @@ -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 diff --git a/Library/Homebrew/unpack_strategy/cab.rb b/Library/Homebrew/unpack_strategy/cab.rb index 89feaf0df6..82265231d5 100644 --- a/Library/Homebrew/unpack_strategy/cab.rb +++ b/Library/Homebrew/unpack_strategy/cab.rb @@ -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 diff --git a/Library/Homebrew/unpack_strategy/fossil.rb b/Library/Homebrew/unpack_strategy/fossil.rb index 869a056285..ddaf147795 100644 --- a/Library/Homebrew/unpack_strategy/fossil.rb +++ b/Library/Homebrew/unpack_strategy/fossil.rb @@ -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 diff --git a/Library/Homebrew/unpack_strategy/generic_unar.rb b/Library/Homebrew/unpack_strategy/generic_unar.rb index 4628cd517d..0322d5c9ef 100644 --- a/Library/Homebrew/unpack_strategy/generic_unar.rb +++ b/Library/Homebrew/unpack_strategy/generic_unar.rb @@ -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 diff --git a/Library/Homebrew/unpack_strategy/lha.rb b/Library/Homebrew/unpack_strategy/lha.rb index 7b92af7290..543f4e4540 100644 --- a/Library/Homebrew/unpack_strategy/lha.rb +++ b/Library/Homebrew/unpack_strategy/lha.rb @@ -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 diff --git a/Library/Homebrew/unpack_strategy/lzip.rb b/Library/Homebrew/unpack_strategy/lzip.rb index 71f561c9df..da8a991b9e 100644 --- a/Library/Homebrew/unpack_strategy/lzip.rb +++ b/Library/Homebrew/unpack_strategy/lzip.rb @@ -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 diff --git a/Library/Homebrew/unpack_strategy/lzma.rb b/Library/Homebrew/unpack_strategy/lzma.rb index b8ee36b210..7ef147b61b 100644 --- a/Library/Homebrew/unpack_strategy/lzma.rb +++ b/Library/Homebrew/unpack_strategy/lzma.rb @@ -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 diff --git a/Library/Homebrew/unpack_strategy/mercurial.rb b/Library/Homebrew/unpack_strategy/mercurial.rb index 4538c8e8d9..cc27a11395 100644 --- a/Library/Homebrew/unpack_strategy/mercurial.rb +++ b/Library/Homebrew/unpack_strategy/mercurial.rb @@ -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 diff --git a/Library/Homebrew/unpack_strategy/p7zip.rb b/Library/Homebrew/unpack_strategy/p7zip.rb index 3a5ef46f00..618ea06f23 100644 --- a/Library/Homebrew/unpack_strategy/p7zip.rb +++ b/Library/Homebrew/unpack_strategy/p7zip.rb @@ -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 diff --git a/Library/Homebrew/unpack_strategy/rar.rb b/Library/Homebrew/unpack_strategy/rar.rb index 7cd1d02635..bd2b8042bb 100644 --- a/Library/Homebrew/unpack_strategy/rar.rb +++ b/Library/Homebrew/unpack_strategy/rar.rb @@ -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 diff --git a/Library/Homebrew/unpack_strategy/xz.rb b/Library/Homebrew/unpack_strategy/xz.rb index ec81be5673..61e1b2c3ab 100644 --- a/Library/Homebrew/unpack_strategy/xz.rb +++ b/Library/Homebrew/unpack_strategy/xz.rb @@ -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 diff --git a/Library/Homebrew/unpack_strategy/zip.rb b/Library/Homebrew/unpack_strategy/zip.rb index 7e8815a88a..6f8e6f7426 100644 --- a/Library/Homebrew/unpack_strategy/zip.rb +++ b/Library/Homebrew/unpack_strategy/zip.rb @@ -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 diff --git a/Library/Homebrew/unpack_strategy/zstd.rb b/Library/Homebrew/unpack_strategy/zstd.rb index 47fac8fe8c..4e77184977 100644 --- a/Library/Homebrew/unpack_strategy/zstd.rb +++ b/Library/Homebrew/unpack_strategy/zstd.rb @@ -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 diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index dfe5ceb7f2..9104ba1b37 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -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) diff --git a/Library/Homebrew/utils/git.rb b/Library/Homebrew/utils/git.rb index 8fbe90de34..c81435a0b1 100644 --- a/Library/Homebrew/utils/git.rb +++ b/Library/Homebrew/utils/git.rb @@ -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