From d555ec9218e80c6192552ef9089ccef356c0abd7 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Mon, 27 Jan 2025 08:42:49 +0000 Subject: [PATCH] Fix handling of case-mistyped commands - hide warnings when requiring files repeatedly on a case-insensitive filesystem and add reference to Ruby bugs - add another case to check for command require failures - also handle commands with `-` in them Fixes #19125 --- Library/Homebrew/brew.rb | 7 +++++-- Library/Homebrew/extend/kernel.rb | 10 +++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/brew.rb b/Library/Homebrew/brew.rb index 03b1174ddb..ced88c58be 100644 --- a/Library/Homebrew/brew.rb +++ b/Library/Homebrew/brew.rb @@ -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 diff --git a/Library/Homebrew/extend/kernel.rb b/Library/Homebrew/extend/kernel.rb index 89e3723b90..94bcf520c0 100644 --- a/Library/Homebrew/extend/kernel.rb +++ b/Library/Homebrew/extend/kernel.rb @@ -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.