[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:52:35 PDT 2026


https://github.com/compnerd updated https://github.com/llvm/llvm-project/pull/207250

>From 0673fe35f7d6c28e0ca27b7b1f37cdff8a856e52 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..ac5ce27380d53 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 7c0c007c6a0ce5550c3329719ad905639249bc83 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..6cb0196ae2e0a 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