From 69e597f8e5818ed093e01dfd6f3acc2a3142a0de Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Thu, 12 Oct 2023 02:16:43 +0000 Subject: [PATCH] Do not fail formatting when google-java-formatter is not found A missing google-java-format should not break older chromium branches and non-chromium projects. This causes the formatting step to skip .java files when google-java-format is not available, and introduces an environment variable GOOGLE_JAVA_FORMAT_PATH to set a custom path. Bug: 1491627 Change-Id: I5e3155553630ddc59c1a8d197e96079f6a372454 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4930437 Commit-Queue: Andrew Grieve Reviewed-by: Josip Sokcevic --- git_cl.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/git_cl.py b/git_cl.py index 0a69eae340..7d11600517 100755 --- a/git_cl.py +++ b/git_cl.py @@ -6127,21 +6127,29 @@ def _RunClangFormatDiff(opts, clang_diff_files, top_dir, upstream_commit): def _FindGoogleJavaFormat(): + # Allow non-chromium projects to use a custom location. primary_solution_path = gclient_paths.GetPrimarySolutionPath() if primary_solution_path: + override = os.environ.get('GOOGLE_JAVA_FORMAT_PATH') + if override: + # Make relative to solution root if not an absolute path. + return os.path.join(primary_solution_path, override) + path = os.path.join(primary_solution_path, 'third_party', 'google-java-format', 'google-java-format') if os.path.exists(path): return path - - return shutil.which('google-java-format') + return None def _RunGoogleJavaFormat(opts, paths, top_dir, upstream_commit): """Runs google-java-format and sets a return value if necessary.""" google_java_format = _FindGoogleJavaFormat() if google_java_format is None: - DieWithError('Could not find google-java-format.') + # Fail silently. It could be we are on an old chromium revision, or that + # it is a non-chromium project. https://crbug.com/1491627 + print('google-java-format not found, skipping java formatting.') + return 0 base_cmd = [google_java_format, '--aosp'] if opts.dry_run or opts.diff: