[llvm] lit: improve long path support on Windows (PR #207250)
Saleem Abdulrasool via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 2 11:48:52 PDT 2026
https://github.com/compnerd created https://github.com/llvm/llvm-project/pull/207250
This pull request improves Windows path handling in the `llvm/utils/lit/lit` utilities by introducing and applying an `extended` function to correctly format file paths for Windows APIs, especially for long paths and UNC paths. The changes ensure that file operations such as removal and redirection work reliably on Windows systems.
**Windows path handling improvements:**
* Added an `extended` function in both `InprocBuiltins.py` and `ShellEnvironment.py` to convert paths to the extended-length format required by Windows, handling both regular and UNC paths. [[1]](diffhunk://#diff-7b75d403cff61cebbd12ef3915054dee6a887deaa2300fbc73a33f64ce2d1255R179-R186) [[2]](diffhunk://#diff-31c539a1c64eb53261e543eeda1966733230d2b7613f5d500deed3f2f1ce2baeR121-R128)
* Applied the `extended` function to file removal operations in `InprocBuiltins.py`, ensuring paths are properly formatted before deletion, which helps avoid issues with long or special Windows paths.
* Used the `extended` function for redirected file paths in `ShellEnvironment.py`, ensuring that redirections to files handle Windows path limitations correctly.
>From 78a8a2f1bbb0ad9bff754e7e43eb5e65614eeea1 Mon Sep 17 00:00:00 2001
From: Saleem Abdulrasool <compnerd at compnerd.org>
Date: Thu, 2 Jul 2026 11:40:21 -0700
Subject: [PATCH 1/2] lit: use extended paths on Windows for redirection
If you exceed the Win32 path limit, we will silently fail on Windows.
Use extended (NT) path spellings to allow us to use deeper paths.
---
llvm/utils/lit/lit/ShellEnvironment.py | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/llvm/utils/lit/lit/ShellEnvironment.py b/llvm/utils/lit/lit/ShellEnvironment.py
index ffd58617b42f2..7a8887b021b54 100644
--- a/llvm/utils/lit/lit/ShellEnvironment.py
+++ b/llvm/utils/lit/lit/ShellEnvironment.py
@@ -118,6 +118,14 @@ def processRedirects(cmd, stdin_source, cmd_shenv, opened_files):
subprocess module.
"""
+ def extended(path):
+ if not kIsWindows:
+ return path
+ path = os.path.abspath(path)
+ if path.startswith('\\\\'):
+ return "\\\\?\\UNC\\{0}".format(path[2:])
+ return "\\\\?\\{0}".format(path)
+
# Apply the redirections, we use (N,) as a sentinel to indicate stdin,
# stdout, stderr for N equal to 0, 1, or 2 respectively. Redirects to or
# from a file are represented with a list [file, mode, file-object]
@@ -190,6 +198,7 @@ def processRedirects(cmd, stdin_source, cmd_shenv, opened_files):
else:
# Make sure relative paths are relative to the cwd.
redir_filename = os.path.join(cmd_shenv.cwd, name)
+ redir_filename = extended(redir_filename)
fd = open(redir_filename, mode, encoding="utf-8")
# Workaround a Win32 and/or subprocess bug when appending.
#
>From 75b0b50d8b2c31d36aee349d19bd6acb36548685 Mon Sep 17 00:00:00 2001
From: Saleem Abdulrasool <compnerd at compnerd.org>
Date: Thu, 2 Jul 2026 11:41:14 -0700
Subject: [PATCH 2/2] lit: handle `rm` on Windows for files with extended paths
Previous changes enabled long directories to be cleaned up by using
ShellWapi. This addresses the _file_ operation now. Use NT extended
paths to ensure that long paths do not silently fail.
---
llvm/utils/lit/lit/InprocBuiltins.py | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/llvm/utils/lit/lit/InprocBuiltins.py b/llvm/utils/lit/lit/InprocBuiltins.py
index b20aeb6993726..458aae46386c2 100644
--- a/llvm/utils/lit/lit/InprocBuiltins.py
+++ b/llvm/utils/lit/lit/InprocBuiltins.py
@@ -176,6 +176,14 @@ def on_rm_error(func, path, exc_info):
os.chmod(path, stat.S_IMODE(os.stat(path).st_mode) | stat.S_IWRITE)
os.remove(path)
+ def extended(path):
+ if not kIsWindows:
+ return path
+ path = os.path.abspath(path)
+ if path.startswith('\\\\'):
+ return "\\\\?\\UNC\\{0}".format(path[2:])
+ return "\\\\?\\{0}".format(path)
+
stderr = StringIO()
exitCode = 0
for path in args:
@@ -251,6 +259,7 @@ class SHFILEOPSTRUCTW(Structure):
else:
shutil.rmtree(path, onerror=on_rm_error if force else None)
else:
+ path = extended(path)
if force and not os.access(path, os.W_OK):
os.chmod(path, stat.S_IMODE(os.stat(path).st_mode) | stat.S_IWRITE)
os.remove(path)
More information about the llvm-commits
mailing list