Skip to content

Commit 9dc1112

Browse files
authored
Merge pull request #20358 from Homebrew/require-formula-ensure-formula-installed
Ensure we `require "formula"` before doing `ensure_formula_installed!`
2 parents 1fc4225 + 3d4df6f commit 9dc1112

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

Library/Homebrew/dev-cmd/bump.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ def run
135135

136136
if args.repology? && !Utils::Curl.curl_supports_tls13?
137137
begin
138-
ensure_formula_installed!("curl", reason: "Repology queries") unless HOMEBREW_BREWED_CURL_PATH.exist?
138+
unless HOMEBREW_BREWED_CURL_PATH.exist?
139+
require "formula"
140+
ensure_formula_installed!("curl", reason: "Repology queries")
141+
end
139142
rescue FormulaUnavailableError
140143
opoo "A newer `curl` is required for Repology queries."
141144
end

Library/Homebrew/extend/kernel.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,8 @@ def redirect_stdout(file)
441441

442442
# Ensure the given formula is installed
443443
# This is useful for installing a utility formula (e.g. `shellcheck` for `brew style`)
444+
# NOTE: One must `require "formula"` before using this method. Doing `require "formula"` inside the method
445+
# doesn't help, and therefore is useless to add.
444446
sig {
445447
params(formula_name: String, reason: String, latest: T::Boolean, output_to_stderr: T::Boolean,
446448
quiet: T::Boolean).returns(Formula)
@@ -460,8 +462,8 @@ def ensure_formula_installed!(formula_name, reason: "", latest: false,
460462
end
461463
end
462464

463-
require "formula"
464-
465+
# Do not `require "formula"` here. It will mask misuse of this method when
466+
# it is called without doing `require "formula"` first.
465467
formula = Formula[formula_name]
466468
reason = " for #{reason}" if reason.present?
467469

@@ -493,6 +495,7 @@ def ensure_executable!(name, formula_name = nil, reason: "", latest: false)
493495
].compact.first
494496
return executable if executable.exist?
495497

498+
require "formula"
496499
ensure_formula_installed!(formula_name, reason:, latest:).opt_bin/name
497500
end
498501

Library/Homebrew/style.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,23 +320,21 @@ def self.github_workflow_files
320320
HOMEBREW_REPOSITORY.glob(".github/workflows/*.yml")
321321
end
322322

323-
def self.rubocop
324-
ensure_formula_installed!("rubocop", latest: true,
325-
reason: "Ruby style checks").opt_bin/"rubocop"
326-
end
327-
328323
def self.shellcheck
324+
require "formula"
329325
ensure_formula_installed!("shellcheck", latest: true,
330326
reason: "shell style checks").opt_bin/"shellcheck"
331327
end
332328

333329
def self.shfmt
330+
require "formula"
334331
ensure_formula_installed!("shfmt", latest: true,
335332
reason: "formatting shell scripts")
336333
HOMEBREW_LIBRARY/"Homebrew/utils/shfmt.sh"
337334
end
338335

339336
def self.actionlint
337+
require "formula"
340338
ensure_formula_installed!("actionlint", latest: true,
341339
reason: "GitHub Actions checks").opt_bin/"actionlint"
342340
end

Library/Homebrew/utils/git.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def self.ensure_installed!
103103
# and will also likely fail due to `OS::Linux` and `OS::Mac` being undefined.
104104
raise "Refusing to install Git on a generic OS." if ENV["HOMEBREW_TEST_GENERIC_OS"]
105105

106+
require "formula"
106107
ensure_formula_installed!("git")
107108
clear_available_cache
108109
rescue

Library/Homebrew/utils/pypi.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ def basic_metadata
152152
@extras ||= T.let([], T.nilable(T::Array[String]))
153153
@version ||= T.let(match[2], T.nilable(String))
154154
elsif @is_url
155+
require "formula"
155156
ensure_formula_installed!(@python_name)
156157

157158
# The URL might be a source distribution hosted somewhere;
@@ -260,6 +261,7 @@ def self.update_python_resources!(formula, version: nil, package_name: nil, extr
260261
missing_msg = "formulae required to update \"#{formula.name}\" resources: #{missing_dependencies.join(", ")}"
261262
odie "Missing #{missing_msg}" unless install_dependencies
262263
ohai "Installing #{missing_msg}"
264+
require "formula"
263265
missing_dependencies.each(&:ensure_formula_installed!)
264266
end
265267

@@ -334,6 +336,7 @@ def self.update_python_resources!(formula, version: nil, package_name: nil, extr
334336
end
335337
end
336338

339+
require "formula"
337340
ensure_formula_installed!(python_name)
338341

339342
# Resolve the dependency tree of all input packages

0 commit comments

Comments
 (0)