[clang] [clang-format][Windows] Support Python launcher fallbackFix git-clang-format Python launcher on Windows (PR #224925)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Sep 20 05:44:08 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Kumar Mayank (mayanksinha17)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/224925.diff
2 Files Affected:
- (added) clang/test/Format/git-clang-format-windows.test (+19)
- (modified) clang/tools/clang-format/git-clang-format.bat (+7-1)
``````````diff
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 00000000000000..b20e803deee564
--- /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 a40276e63c5848..b6bd7c992561fc 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" %*
+ at echo off
+where /q python
+if not errorlevel 1 (
+ python "%~dpn0" %*
+) else (
+ py -3 "%~dpn0" %*
+)
``````````
</details>
https://github.com/llvm/llvm-project/pull/224925
More information about the cfe-commits
mailing list