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 <msta@google.com>
Reviewed-by: Gavin Mak <gavinmak@google.com>
This commit is contained in:
Matt Stark
2025-08-25 16:48:48 -07:00
committed by LUCI CQ
parent 05cb270161
commit bf3673451d
2 changed files with 19 additions and 6 deletions

View File

@@ -84,11 +84,27 @@ 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:
tool = FindClangFormatInPath()
if tool is None:
sys.stderr.write("%s\n" % str(e))
return 1

5
gn.py
View File

@@ -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