From dca14bc463857bd2a0fee59c86ffa289b535d5d3 Mon Sep 17 00:00:00 2001 From: Bruce Dawson Date: Thu, 15 Sep 2022 20:59:38 +0000 Subject: [PATCH] Show PresubmitResult call stacks in verbose mode When a presubmit message, warning, or error strikes it is sometimes due to a bug or weakness in the presubmit. Examining the presubmit or fixing it can be important. However it can be hard to find the relevant code (hint: many presubmits are in depot_tools/presubmit_canned_checks.py). With this change you can just run the presubmits with -v -v (double verbose) and a call stack will be recorded when each presubmit result object is created. For instance: >git cl presubmit --force --files ash/public/cpp/app_list/vector_icons/google_black.icon -v -v ** Presubmit Messages: 1 ** Trademarked images should not be added to the public repo. See crbug.com/944754 ash/public/cpp/app_list/vector_icons/google_black.icon *************** Presubmit result call stack is: File "depot_tools/presubmit_support.py", line 2098, in sys.exit(main()) File "depot_tools/presubmit_support.py", line 2074, in main return DoPresubmitChecks( File "depot_tools/presubmit_support.py", line 1771, in DoPresubmitChecks results += executer.ExecPresubmitScript(presubmit_script, filename) File "depot_tools/presubmit_support.py", line 1612, in ExecPresubmitScript self._run_check_function(function_name, context, sink, File "depot_tools/presubmit_support.py", line 1653, in _run_check_function result = eval(function_name + '(*__args)', context) File "", line 1, in File "chromium/src/PRESUBMIT.py", line 2225, in CheckNoProductIconsAddedToPublicRepo message_type( File "depot_tools/presubmit_support.py", line 352, in __init__ self._long_text += ' '.join(traceback.format_stack(None, 8)) This changes tracking down presubmits from a dark art to a trivial operation. Bug: 1309977 Change-Id: Ia0a6adfbbab04041f97c56cd2064a1627e252561 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3896076 Reviewed-by: Dmitrii Kuragin Commit-Queue: Dmitrii Kuragin --- presubmit_support.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/presubmit_support.py b/presubmit_support.py index cf15c9de4..8d5338533 100755 --- a/presubmit_support.py +++ b/presubmit_support.py @@ -65,6 +65,11 @@ else: # Ask for feedback only once in program lifetime. _ASKED_FOR_FEEDBACK = False +# Set if super-verbose mode is requested, for tracking where presubmit messages +# are coming from. +_SHOW_CALLSTACKS = False + + def time_time(): # Use this so that it can be mocked in tests without interfering with python # system machinery. @@ -342,6 +347,9 @@ class _PresubmitResult(object): self._message = _PresubmitResult._ensure_str(message) self._items = items or [] self._long_text = _PresubmitResult._ensure_str(long_text.rstrip()) + if _SHOW_CALLSTACKS: + self._long_text += 'Presubmit result call stack is:\n' + self._long_text += ''.join(traceback.format_stack(None, 8)) @staticmethod def _ensure_str(val): @@ -2047,6 +2055,12 @@ def main(argv=None): '%(filename)s] %(message)s') logging.basicConfig(format=log_format, level=log_level) + # Print call stacks when _PresubmitResult objects are created with -v -v is + # specified. This helps track down where presubmit messages are coming from. + if options.verbose >= 2: + global _SHOW_CALLSTACKS + _SHOW_CALLSTACKS = True + if options.description_file: options.description = gclient_utils.FileRead(options.description_file) gerrit_obj = _parse_gerrit_options(parser, options)