From 0a33cc591d258e07f2279bc0440d62e38983fd67 Mon Sep 17 00:00:00 2001 From: Martin Afanasjew Date: Sat, 30 Jul 2016 14:51:14 +0200 Subject: [PATCH] utils: provide a better location in 'odeprecated' Try to find a formula in the backtrace to make the warning message more helpful in identifying the culprit, as the formula is not always the immediate caller of a deprecated method. Provide some sane fallbacks if there's not formula in the call stack. --- Library/Homebrew/utils.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 928f25fc91..aaf9d8ba52 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -125,9 +125,18 @@ def odeprecated(method, replacement = nil, options = {}) "There is no replacement." end - # Show the first location that's not in compat. - backtrace = options[:caller] || caller - caller_message = backtrace[1] + # Try to show the most relevant location in message, i.e. (if applicable): + # - Location in a formula. + # - Location outside of 'compat/'. + # - Location of caller of deprecated method (if all else fails). + backtrace = options.fetch(:caller, caller) + caller_message = backtrace.detect do |line| + line.start_with?("#{HOMEBREW_LIBRARY}/Taps/") + end + caller_message ||= backtrace.detect do |line| + !line.start_with?("#{HOMEBREW_LIBRARY_PATH}/compat/") + end + caller_message ||= backtrace[1] message = <<-EOS.undent Calling #{method} is #{verb}! @@ -136,7 +145,7 @@ def odeprecated(method, replacement = nil, options = {}) EOS if ARGV.homebrew_developer? || options[:die] - raise FormulaMethodDeprecatedError.new message + raise FormulaMethodDeprecatedError, message else opoo "#{message}\n" end