From 33744dc6bc2928714f5988a635202af6974416a8 Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Thu, 30 Oct 2025 12:05:33 -0700 Subject: [PATCH] Make google-java-format easier to use for non-chromium checkouts like R8 1. Use "codereview.settings" in addition to "buildtools" as the marker that identifies a project root. 2. Allow the presence of any .jar file to indicate the availability of google-java-format. 3. Do not pass --aosp when running google-java-format Bug: 456461246 Change-Id: Id27b3c03f592a0ed73fb7a6b4dd662707a649166 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/7102758 Reviewed-by: Scott Lee Commit-Queue: Andrew Grieve --- gclient_paths.py | 6 ++++-- git_cl.py | 9 ++++++++- google_java_format.py | 20 +++++++++----------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/gclient_paths.py b/gclient_paths.py index 7d05599533..1bcb091da2 100644 --- a/gclient_paths.py +++ b/gclient_paths.py @@ -80,7 +80,8 @@ def _GetPrimarySolutionPathInternal(cwd): return os.path.join(gclient_root, source_dir_name) # Some projects might not use .gclient. Try to see whether we're in a git - # checkout that contains a 'buildtools' subdir. + # checkout that contains a "buildtools" directory or "codereview.settings" + # file. top_dir = cwd try: top_dir = subprocess2.check_output( @@ -90,7 +91,8 @@ def _GetPrimarySolutionPathInternal(cwd): except subprocess2.CalledProcessError: pass - if os.path.exists(os.path.join(top_dir, 'buildtools')): + if (os.path.exists(os.path.join(top_dir, 'codereview.settings')) + or os.path.exists(os.path.join(top_dir, 'buildtools'))): return top_dir return None diff --git a/git_cl.py b/git_cl.py index ed4e0b8be2..a4d891c278 100755 --- a/git_cl.py +++ b/git_cl.py @@ -6839,7 +6839,14 @@ def _RunGoogleJavaFormat(opts, paths, top_dir, diffs): print('google-java-format not found, skipping java formatting.') return 0 - base_cmd = [tool, '--aosp'] + base_cmd = [tool] + # The script now adds --aosp, but this shim is needed for the window where + # devs are using depot_tools that is newer than their chromium checkout. + # This can be remove after Dec 2025. + if os.path.exists( + os.path.join(os.path.dirname(tool), 'chromium-overrides.jar')): + base_cmd += ['--aosp'] + if not opts.diff: if opts.dry_run: base_cmd += ['--dry-run'] diff --git a/google_java_format.py b/google_java_format.py index 9e54bed68b..dbc41a8de9 100755 --- a/google_java_format.py +++ b/google_java_format.py @@ -9,6 +9,7 @@ you sync Chrome. This script finds and runs the executable. """ import gclient_paths +import glob import os import subprocess import sys @@ -24,19 +25,16 @@ def FindGoogleJavaFormat(): # Make relative to solution root if not an absolute path. return os.path.join(primary_solution_path, override) - bin_path = os.path.join(primary_solution_path, 'third_party', - 'google-java-format', 'google-java-format') - cipd_path = os.path.join(primary_solution_path, 'third_party', - 'google-java-format', 'cipd', - 'google-java-format.jar') + bin_dir = os.path.join(primary_solution_path, 'third_party', + 'google-java-format') + bin_path = os.path.join(bin_dir, 'google-java-format') + # Check that the .jar exists, since it is conditionally downloaded via # DEPS conditions. - # TODO(b/345761161): Remove old os.path.exists(path + '.jar') check, - # when third_party/google-java-format - # -> third_party/google-java-format/cipd is fully rolled out. - if os.path.exists(bin_path) and (os.path.exists(bin_path + '.jar') - or os.path.exists(cipd_path)): - return bin_path + if os.path.exists(bin_path): + if glob.glob(os.path.join(bin_dir, '*.jar')) or glob.glob( + os.path.join(bin_dir, 'cipd', '*.jar')): + return bin_path return None