From bf3673451d5781e321849f330ecf24981dcd1c58 Mon Sep 17 00:00:00 2001 From: Matt Stark Date: Mon, 25 Aug 2025 16:48:48 -0700 Subject: [PATCH] Fix clang-format to work when you're not in a chromium checkout. Bug: 40861992 Change-Id: Ic9dc3102815febc1e05795b88ed434bed66d123f Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6872983 Commit-Queue: Matt Stark Reviewed-by: Gavin Mak --- clang_format.py | 20 ++++++++++++++++++-- gn.py | 5 +---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/clang_format.py b/clang_format.py index c62baf87b7..e02cdfaaf2 100755 --- a/clang_format.py +++ b/clang_format.py @@ -84,13 +84,29 @@ def FindClangFormatScriptInChromiumTree(script_name): raise NotFoundError('File does not exist: %s' % script_path) return script_path +def FindClangFormatInPath(): + env_path = os.environ.get('PATH') + if not env_path: + return + for bin_dir in env_path.split(os.pathsep): + if bin_dir.rstrip(os.sep).endswith('depot_tools'): + # skip depot_tools to avoid calling gn.py infinitely. + continue + tool = os.path.join( + bin_dir, + 'clang-format' + gclient_paths.GetExeSuffix(), + ) + if os.path.isfile(tool): + return tool def main(args): try: tool = FindClangFormatToolInChromiumTree() except NotFoundError as e: - sys.stderr.write("%s\n" % str(e)) - return 1 + tool = FindClangFormatInPath() + if tool is None: + sys.stderr.write("%s\n" % str(e)) + return 1 # Add some visibility to --help showing where the tool lives, since this # redirection can be a little opaque. diff --git a/gn.py b/gn.py index 8a4319389a..bae7e74403 100755 --- a/gn.py +++ b/gn.py @@ -37,14 +37,11 @@ def findGnInPath(): env_path = os.getenv('PATH') if not env_path: return - exe = 'gn' - if sys.platform in ('win32', 'cygwin'): - exe += '.exe' for bin_dir in env_path.split(os.pathsep): if bin_dir.rstrip(os.sep).endswith('depot_tools'): # skip depot_tools to avoid calling gn.py infinitely. continue - gn_path = os.path.join(bin_dir, exe) + gn_path = os.path.join(bin_dir, 'gn' + gclient_paths.GetExeSuffix()) if os.path.isfile(gn_path): return gn_path