Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of case-mistyped commands #19151

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Library/Homebrew/brew.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
ENV["PATH"] = path.to_s

require "commands"
require "warnings"

internal_cmd = Commands.valid_internal_cmd?(cmd) || Commands.valid_internal_dev_cmd?(cmd) if cmd

Expand Down Expand Up @@ -96,8 +97,10 @@
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"

Check warning on line 102 in Library/Homebrew/brew.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/brew.rb#L100-L102

Added lines #L100 - L102 were not covered by tests
odie "Unknown command: brew #{cmd}" if [case_error, private_method_error].include?(e.message)

raise
end
Expand Down
10 changes: 9 additions & 1 deletion Library/Homebrew/extend/kernel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading