Skip to content

Commit 5094658

Browse files
JorgeMIngWauplingithub-actions[bot]
authored
Fix Incomplete File Not found on windows systems (#3247)
* Fix incomplete path Fixing mixing incomplete path. Paths longer than 256 characters in Windows require modification to be usable. The previous solutions didn't consider paths with \\?\\ already on the path and did not consider paths with fewer than 256 characters, but adding etag and incomplete made the path longer. * Fix incomplete path error Paths with already the prefix \\?\ should not incorporate it again * Apply suggestions from code review * Apply style fixes --------- Co-authored-by: Lucain <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 3267a90 commit 5094658

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/huggingface_hub/_local_folder.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,13 @@ class LocalDownloadFilePaths:
8686

8787
def incomplete_path(self, etag: str) -> Path:
8888
"""Return the path where a file will be temporarily downloaded before being moved to `file_path`."""
89-
return self.metadata_path.parent / f"{_short_hash(self.metadata_path.name)}.{etag}.incomplete"
89+
path = self.metadata_path.parent / f"{_short_hash(self.metadata_path.name)}.{etag}.incomplete"
90+
resolved_path = str(path.resolve())
91+
# Some Windows versions do not allow for paths longer than 255 characters.
92+
# In this case, we must specify it as an extended path by using the "\\?\" prefix.
93+
if len(resolved_path) > 255 and not resolved_path.startswith("\\\\?\\"):
94+
path = Path("\\\\?\\" + resolved_path)
95+
return path
9096

9197

9298
@dataclass(frozen=True)

src/huggingface_hub/file_download.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,10 +1136,18 @@ def _hf_hub_download_to_cache_dir(
11361136

11371137
# Some Windows versions do not allow for paths longer than 255 characters.
11381138
# In this case, we must specify it as an extended path by using the "\\?\" prefix.
1139-
if os.name == "nt" and len(os.path.abspath(lock_path)) > 255:
1139+
if (
1140+
os.name == "nt"
1141+
and len(os.path.abspath(lock_path)) > 255
1142+
and not os.path.abspath(lock_path).startswith("\\\\?\\")
1143+
):
11401144
lock_path = "\\\\?\\" + os.path.abspath(lock_path)
11411145

1142-
if os.name == "nt" and len(os.path.abspath(blob_path)) > 255:
1146+
if (
1147+
os.name == "nt"
1148+
and len(os.path.abspath(blob_path)) > 255
1149+
and not os.path.abspath(blob_path).startswith("\\\\?\\")
1150+
):
11431151
blob_path = "\\\\?\\" + os.path.abspath(blob_path)
11441152

11451153
Path(lock_path).parent.mkdir(parents=True, exist_ok=True)

0 commit comments

Comments
 (0)