From cc7a29c55a9fcd9fb15aa6ec37e045c03e6e77d9 Mon Sep 17 00:00:00 2001 From: James Smillie <83249606+jamessmillie@users.noreply.github.com> Date: Fri, 16 Aug 2024 12:16:13 -0600 Subject: Windows: fix stage cleaning for long paths (#45786) Paths over 260 characters in length are not handled by `shutil.rmtree` unless they use the extended-length path syntax (using a prefix of "\\?\"). This fixes an issue where stage cleaning fails when paths in a stage exceed the normal 260-character limit. This indicates that other parts of the codebase should be examined/ refactored to handle long paths. --- lib/spack/llnl/util/filesystem.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/spack/llnl/util/filesystem.py b/lib/spack/llnl/util/filesystem.py index 6b2ba50c0e..308c6154e1 100644 --- a/lib/spack/llnl/util/filesystem.py +++ b/lib/spack/llnl/util/filesystem.py @@ -1624,6 +1624,12 @@ def remove_linked_tree(path): shutil.rmtree(os.path.realpath(path), **kwargs) os.unlink(path) else: + if sys.platform == "win32": + # Adding this prefix allows shutil to remove long paths on windows + # https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry + long_path_pfx = "\\\\?\\" + if not path.startswith(long_path_pfx): + path = long_path_pfx + path shutil.rmtree(path, **kwargs) -- cgit v1.2.3-70-g09d2