Merge pull request #19151 from Homebrew/fix_case_commands

Fix handling of case-mistyped commands
This commit is contained in:
Mike McQuaid 2025-01-27 09:13:53 +00:00 committed by GitHub
commit 9fd2b27a35
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 3 deletions

View File

@ -61,6 +61,7 @@ begin
ENV["PATH"] = path.to_s
require "commands"
require "warnings"
internal_cmd = Commands.valid_internal_cmd?(cmd) || Commands.valid_internal_dev_cmd?(cmd) if cmd
@ -96,8 +97,10 @@ begin
begin
Homebrew.public_send Commands.method_name(cmd)
rescue NoMethodError => e
case_error = "undefined method `#{cmd.downcase}' for module Homebrew"
odie "Unknown command: brew #{cmd}" if e.message == case_error
converted_cmd = cmd.downcase.tr("-", "_")
case_error = "undefined method `#{converted_cmd}' for module Homebrew"
private_method_error = "private method `#{converted_cmd}' called for module Homebrew"
odie "Unknown command: brew #{cmd}" if [case_error, private_method_error].include?(e.message)
raise
end

View File

@ -8,7 +8,15 @@ module Kernel
def require?(path)
return false if path.nil?
require path
if defined?(Warnings)
# Work around require warning when done repeatedly:
# https://bugs.ruby-lang.org/issues/21091
Warnings.ignore(/already initialized constant/, /previous definition of/) do
require path
end
else
require path
end
true
rescue LoadError => e
# we should raise on syntax errors but not if the file doesn't exist.