diff --git a/Library/Homebrew/formula_cellar_checks.rb b/Library/Homebrew/formula_cellar_checks.rb index f896caaf47..cd568a8466 100644 --- a/Library/Homebrew/formula_cellar_checks.rb +++ b/Library/Homebrew/formula_cellar_checks.rb @@ -7,6 +7,10 @@ require "utils/shell" # # @api private module FormulaCellarChecks + # If the location of HOMEBREW_LIBRARY changes + # keg_relocate.rb, test/global_spec.rb, and this constant need to change. + REPOSITORY_AND_NOT_LIBRARY_REGEX = %r{#{HOMEBREW_REPOSITORY}(?!/Library/)}.freeze + def check_env_path(bin) # warn the user if stuff was installed outside of their PATH return unless bin.directory? @@ -207,6 +211,26 @@ module FormulaCellarChecks EOS end + def check_repository_references(prefix) + return if HOMEBREW_PREFIX != HOMEBREW_REPOSITORY + return unless prefix.directory? + + keg = Keg.new(prefix) + + matches = [] + keg.each_unique_file_matching(HOMEBREW_REPOSITORY) do |f| + matches << f.relative_path_from(keg.to_path) if f.read.match? REPOSITORY_AND_NOT_LIBRARY_REGEX + end + + return if matches.empty? + + <<~EOS + Files were found with references to the Homebrew repository directory + that are outside of the Library directory. The offending files are: + #{matches * "\n "} + EOS + end + def check_shim_references(prefix) return unless prefix.directory? @@ -292,6 +316,7 @@ module FormulaCellarChecks problem_if_output(check_elisp_dirname(formula.share, formula.name)) problem_if_output(check_elisp_root(formula.share, formula.name)) problem_if_output(check_python_packages(formula.lib, formula.deps)) + problem_if_output(check_repository_references(formula.prefix)) problem_if_output(check_shim_references(formula.prefix)) problem_if_output(check_plist(formula.prefix, formula.plist)) problem_if_output(check_python_symlinks(formula.name, formula.keg_only?)) diff --git a/Library/Homebrew/keg_relocate.rb b/Library/Homebrew/keg_relocate.rb index 0ea9544b33..a10ebe27b2 100644 --- a/Library/Homebrew/keg_relocate.rb +++ b/Library/Homebrew/keg_relocate.rb @@ -6,8 +6,12 @@ class Keg CELLAR_PLACEHOLDER = "@@HOMEBREW_CELLAR@@" REPOSITORY_PLACEHOLDER = "@@HOMEBREW_REPOSITORY@@" - Relocation = Struct.new(:old_prefix, :old_cellar, :old_repository, - :new_prefix, :new_cellar, :new_repository) do + # If the location of HOMEBREW_LIBRARY changes + # formula_cellar_checks.rb, test/global_spec.rb, and this constant need to change. + LIBRARY_PLACEHOLDER = "@@HOMEBREW_REPOSITORY@@/Library" + + Relocation = Struct.new(:old_prefix, :old_cellar, :old_repository, :old_library, + :new_prefix, :new_cellar, :new_repository, :new_library) do # Use keyword args instead of positional args for initialization. def initialize(**kwargs) super(*members.map { |k| kwargs[k] }) @@ -40,9 +44,11 @@ class Keg old_prefix: HOMEBREW_PREFIX.to_s, old_cellar: HOMEBREW_CELLAR.to_s, old_repository: HOMEBREW_REPOSITORY.to_s, + old_library: HOMEBREW_LIBRARY.to_s, new_prefix: PREFIX_PLACEHOLDER, new_cellar: CELLAR_PLACEHOLDER, new_repository: REPOSITORY_PLACEHOLDER, + new_library: LIBRARY_PLACEHOLDER, ) relocate_dynamic_linkage(relocation) replace_text_in_files(relocation) @@ -53,9 +59,11 @@ class Keg old_prefix: PREFIX_PLACEHOLDER, old_cellar: CELLAR_PLACEHOLDER, old_repository: REPOSITORY_PLACEHOLDER, + old_library: LIBRARY_PLACEHOLDER, new_prefix: HOMEBREW_PREFIX.to_s, new_cellar: HOMEBREW_CELLAR.to_s, new_repository: HOMEBREW_REPOSITORY.to_s, + new_library: HOMEBREW_LIBRARY.to_s, ) relocate_dynamic_linkage(relocation) unless skip_linkage replace_text_in_files(relocation, files: files) @@ -74,7 +82,11 @@ class Keg } # when HOMEBREW_PREFIX == HOMEBREW_REPOSITORY we should use HOMEBREW_PREFIX for all relocations to avoid # being unable to differentiate between them. - replacements[relocation.old_repository] = relocation.new_repository if HOMEBREW_PREFIX != HOMEBREW_REPOSITORY + if HOMEBREW_PREFIX == HOMEBREW_REPOSITORY + replacements[relocation.old_library] = relocation.new_library + else + replacements[relocation.old_repository] = relocation.new_repository + end changed = s.gsub!(Regexp.union(replacements.keys.sort_by(&:length).reverse), replacements) next unless changed diff --git a/Library/Homebrew/test/global_spec.rb b/Library/Homebrew/test/global_spec.rb index 6c34ca8838..644c32bd16 100644 --- a/Library/Homebrew/test/global_spec.rb +++ b/Library/Homebrew/test/global_spec.rb @@ -8,4 +8,10 @@ describe "brew", :integration_test do .and not_to_output.to_stderr .and be_a_success end + + # If the location of HOMEBREW_LIBRARY changes + # keg_relocate.rb, formula_cellar_checks.rb, and this test need to change. + it "ensures that HOMEBREW_LIBRARY=HOMEBREW_REPOSITORY/Library" do + expect(HOMEBREW_LIBRARY.to_s).to eq("#{HOMEBREW_REPOSITORY}/Library") + end end diff --git a/bin/brew b/bin/brew index d247d13264..d93497ef3e 100755 --- a/bin/brew +++ b/bin/brew @@ -58,6 +58,8 @@ then fi fi +# If the location of HOMEBREW_LIBRARY changes +# keg_relocate.rb, formula_cellar_checks.rb, and test/global_spec.rb need to change. HOMEBREW_LIBRARY="$HOMEBREW_REPOSITORY/Library" # Copy and export all HOMEBREW_* variables previously mentioned in