https://github.com/mayanksinha17 created 
https://github.com/llvm/llvm-project/pull/224925

Fixes #215630

The Windows `git-clang-format.bat` launcher currently invokes `py -3`
directly, which fails on systems where Python 3 is available as
`python.exe` but the optional `py.exe` launcher is not installed.

This change:

- Uses `python` when it is available on PATH.
- Falls back to `py -3` when `python` is unavailable.
- Preserves the existing script path handling and argument forwarding.
- Adds a Windows regression test covering both launcher paths and
  non-zero exit-code propagation.

Testing:
- Targeted Windows launcher regression cases: PASS
- Python available / py unavailable: PASS
- Python unavailable / py available: PASS
- Python non-zero exit code propagation: PASS
- Paths/arguments containing spaces and quotes: PASS
- `git diff --check`: PASS

A full LLVM Lit run was not available locally because this checkout does
not currently have a configured CMake/Ninja build directory.

>From 76b8efcbb8b92652d704b7cd6975ef3d2fb40008 Mon Sep 17 00:00:00 2001
From: Mayank Kumar Sinha <[email protected]>
Date: Sun, 20 Sep 2026 18:09:49 +0530
Subject: [PATCH] Fix git-clang-format Python launcher on Windows

---
 .../test/Format/git-clang-format-windows.test | 19 +++++++++++++++++++
 clang/tools/clang-format/git-clang-format.bat |  8 +++++++-
 2 files changed, 26 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Format/git-clang-format-windows.test

diff --git a/clang/test/Format/git-clang-format-windows.test 
b/clang/test/Format/git-clang-format-windows.test
new file mode 100644
index 0000000000000..b20e803deee56
--- /dev/null
+++ b/clang/test/Format/git-clang-format-windows.test
@@ -0,0 +1,19 @@
+// REQUIRES: system-windows
+
+// RUN: rm -rf %t.dir
+// RUN: mkdir -p %t.dir/mock_bin
+
+// Case 1: python is in PATH, py and python3 are unavailable.
+// RUN: echo @echo MOCK_PYTHON %%* > %t.dir/mock_bin/python.bat
+// RUN: cmd /c "set PATH=%t.dir\mock_bin;%SystemRoot%\System32& 
\"%S/../../tools/clang-format/git-clang-format.bat\" --style=\"{BasedOnStyle: 
LLVM}\" \"test file.cpp\"" | FileCheck --check-prefix=CHECK-PYTHON %s
+// CHECK-PYTHON: MOCK_PYTHON "{{.*}}git-clang-format" --style="{BasedOnStyle: 
LLVM}" "test file.cpp"
+
+// Case 2: python is unavailable, fallback to py -3.
+// RUN: rm -f %t.dir/mock_bin/python.bat
+// RUN: echo @echo MOCK_PY %%* > %t.dir/mock_bin/py.bat
+// RUN: cmd /c "set PATH=%t.dir\mock_bin;%SystemRoot%\System32& 
\"%S/../../tools/clang-format/git-clang-format.bat\" --style=\"{BasedOnStyle: 
LLVM}\" \"test file.cpp\"" | FileCheck --check-prefix=CHECK-PY %s
+// CHECK-PY: MOCK_PY -3 "{{.*}}git-clang-format" --style="{BasedOnStyle: 
LLVM}" "test file.cpp"
+
+// Case 3: Error exit code preservation (ensuring fallback is not executed on 
failure).
+// RUN: echo @exit /b 42 > %t.dir/mock_bin/python.bat
+// RUN: not cmd /c "set PATH=%t.dir\mock_bin;%SystemRoot%\System32& 
\"%S/../../tools/clang-format/git-clang-format.bat\""
diff --git a/clang/tools/clang-format/git-clang-format.bat 
b/clang/tools/clang-format/git-clang-format.bat
index a40276e63c584..b6bd7c992561f 100644
--- a/clang/tools/clang-format/git-clang-format.bat
+++ b/clang/tools/clang-format/git-clang-format.bat
@@ -1 +1,7 @@
-py -3 "%~dpn0" %*
+@echo off
+where /q python
+if not errorlevel 1 (
+  python "%~dpn0" %*
+) else (
+  py -3 "%~dpn0" %*
+)

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to