Fix HEAD installations with HOMEBREW_FORBID_PACKAGES_FROM_PATHS

- Allow cache paths in FromPathLoader when HOMEBREW_FORBID_PACKAGES_FROM_PATHS is set
- Fixes issue where HEAD installations fail due to temporary source downloads
- Add test case to verify cache paths are allowed when path restrictions are enabled

The issue occurred because HEAD installations download formula sources to cache
directories, but HOMEBREW_FORBID_PACKAGES_FROM_PATHS only allowed paths from
HOMEBREW_CELLAR and HOMEBREW_LIBRARY/Taps, causing the installation to fail.

Closes: homebrew/brew#issue-number
This commit is contained in:
HuaDeity 2025-08-13 17:57:55 +08:00
parent ec207feca4
commit dd0e187eb5
No known key found for this signature in database
2 changed files with 17 additions and 1 deletions

View File

@ -617,7 +617,8 @@ module Formulary
return unless path.expand_path.exist?
return if Homebrew::EnvConfig.forbid_packages_from_paths? &&
!path.realpath.to_s.start_with?("#{HOMEBREW_CELLAR}/", "#{HOMEBREW_LIBRARY}/Taps/")
!path.realpath.to_s.start_with?("#{HOMEBREW_CELLAR}/", "#{HOMEBREW_LIBRARY}/Taps/",
"#{HOMEBREW_CACHE}/")
if (tap = Tap.from_path(path))
# Only treat symlinks in taps as aliases.

View File

@ -141,6 +141,21 @@ RSpec.describe Formulary do
end.to raise_error(FormulaUnavailableError)
end
it "allows cache paths even when paths are disabled" do
ENV["HOMEBREW_FORBID_PACKAGES_FROM_PATHS"] = "1"
cache_dir = HOMEBREW_CACHE/"test_formula_cache"
cache_dir.mkpath
cache_formula_path = cache_dir/formula_path.basename
FileUtils.cp formula_path, cache_formula_path
begin
formula = described_class.factory(cache_formula_path)
expect(formula).to be_a(Formula)
ensure
cache_formula_path.unlink if cache_formula_path.exist?
cache_dir.rmdir if cache_dir.exist?
end
end
context "when given a bottle" do
subject(:formula) { described_class.factory(bottle) }