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.
This commit is contained in:
James Smillie 2024-08-16 12:16:13 -06:00 committed by GitHub
parent 61b0f4f84d
commit cc7a29c55a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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)