Skip to content

Fix Windows crash when downloading into a deep local_dir - #4546

Merged
Wauplin merged 2 commits into
huggingface:mainfrom
sprayberry-code:fix/local-dir-long-path-windows
Jul 20, 2026
Merged

Wauplin merged 2 commits into
huggingface:mainfrom
sprayberry-code:fix/local-dir-long-path-windows

Conversation

@askalf

@askalf askalf commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

On Windows the default is LongPathsEnabled=0, downloading into a deep local directory crash while creating the. cache/huggingface directory. That guard prefixes paths above 255 chars, but directory creation already fails at ≥248
(MAX_PATH minus 12 for legacy 8.3 names), so the 248-255 window slips through and throws.

The repro is local_dir whose absolute path lands in that 248-255 window + an ordinary file name -> the crash. The fix
is to lower the guard threshold to >247 so \?\ extended-length prefix applies across the whole failing range.

Testing is a parametrized regression test at [252, 300] chars, guarded to windows, then executing in your windows-latest CI matrix. AI assisted me with this PR, I've reviewed the change and understand every line.


Note

Low Risk
Scoped to Windows filesystem path handling in local-dir cache setup; behavior elsewhere is unchanged and covered by a targeted regression test.

Overview
Fixes WinError 206 when downloading into a very deep local_dir on Windows without long-path support enabled.

_huggingface_dir now applies the \\?\ extended-length prefix for mkdir, CACHEDIR.TAG, and .gitignore when the absolute .cache/huggingface path exceeds 247 characters (directory limit before the existing 255-char guard on download/upload paths). It still returns the normal unprefixed path so downstream path logic is unchanged.

Adds a Windows-only parametrized test (252 and 300 char cache paths) that exercises get_local_download_paths with a long local_dir, not just a long filename.

Reviewed by Cursor Bugbot for commit 02efc97. Bugbot is set up for automated code reviews on this repo. Configure here.

askalf added 2 commits July 18, 2026 10:01
get_local_download_paths / get_local_upload_paths apply the extended-length "\?\" prefix to the download/upload paths, but _huggingface_dir() created <local_dir>/.cache/huggingface with a plain mkdir first. For a deep local_dir that folder exceeds the Windows ~255 char limit, so the mkdir raised FileNotFoundError (WinError 206) before the prefix workaround ran.

Apply the same prefix inside _huggingface_dir for its directory creation and bookkeeping files (CACHEDIR.TAG, .gitignore); the un-prefixed path is still returned so callers keep prefixing as before. Adds a Windows regression test covering a long local_dir (the existing test only covers a long filename).
Plain CreateDirectory fails once the directory path exceeds 247 characters (MAX_PATH minus room for an 8.3 file name), not 255: verified empirically that mkdir fails with WinError 206 from 248 onwards while file creation only fails at 260. Guard _huggingface_dir at >247 accordingly and cover both boundary windows (252 and 300) in the regression test.
@askalf
askalf marked this pull request as ready for review July 19, 2026 15:40
@bot-ci-comment

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@Wauplin Wauplin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks for reporting and fixing it :)

@Wauplin
Wauplin merged commit 0aacc25 into huggingface:main Jul 20, 2026
13 of 22 checks passed
@Wauplin

Wauplin commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

This PR has been shipped as part of the v1.25.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants