Skip to content

fix(windows): use extended paths in Python bootstraps - #4071

Merged
aignas merged 2 commits into
bazel-contrib:mainfrom
belitskiy:fix-long-windows-paths
Aug 22, 2026
Merged

fix(windows): use extended paths in Python bootstraps#4071
aignas merged 2 commits into
bazel-contrib:mainfrom
belitskiy:fix-long-windows-paths

Conversation

@belitskiy

Copy link
Copy Markdown
Contributor

Implicit long-path support is not universal across the Win32 API. The documented set of APIs covered by the long-path opt-in does not include DLL loading functions, e.g., LoadLibraryExW:

https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation#functions-without-max_path-restrictions

Always use extended-length paths in Windows bootstrap code and correctly convert UNC paths to the \?\UNC\ form.

Experienced this breakage in JAX after the project being switched over to Bzlmod, which made some paths too long:
https://github.com/jax-ml/jax/actions/runs/31674295160/job/94365413380

    File "c:\botcode\w\bazel-out\x64_windows-opt\bin\jax\experimental\jax2tf\tests\multiprocess\jax2tf_multiprocess_test_cpu.exe.runfiles\rules_python++pip+jax_pypi_312_ml_dtypes_cp312_cp312_win_amd64_c1a95399\site-packages\ml_dtypes\_finfo.py", line 17, in <module>
      from ml_dtypes._ml_dtypes_ext import bfloat16
  ImportError: DLL load failed while importing _ml_dtypes_ext: The filename or extension is too long.

Already used as a patch in jax-ml/jax#39961

@rickeylev

Copy link
Copy Markdown
Collaborator

CI flagged a variety of edge cases: https://buildkite.com/bazel/rules-python-python/builds/17072/list

Would you be able to create a repro for this case? i.e. one that triggers LoadLibraryExW with a long path. There's now a py_extension rule that can create windows DLLs, so it should possible to create a small no-op python c module at a long path location to verify it can be loaded

I'm gonna throw my ai at this and see if it can hash out the failures, too.

@belitskiy

belitskiy commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

Sorry, I expected to get emails about replies here, but I didn't for some reason (or they were filtered out, etc.), so I missed this.
It seems like you have already done what you asked me to do - please let me know if something else is needed of me.

(fixed notifications, they were marked Spam for some reason...)

@rickeylev

Copy link
Copy Markdown
Collaborator

If you could cross check the current state, that'd be helpful. I threw my agent at it, but haven't vetted what it's done.

@belitskiy
belitskiy force-pushed the fix-long-windows-paths branch from 7a8ef71 to 47be690 Compare August 21, 2026 18:31
Implicit long-path support is not universal across Win32. The
documented APIs covered by the long-path opt-in do not include DLL
loading functions such as LoadLibraryExW:

https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation#functions-without-max_path-restrictions

Handle extension paths during import resolution by replacing the
standard PathFinder entry in sys.meta_path with a subclass. It only
modifies a resolved spec when its loader is ExtensionFileLoader and the
extension path reaches MAX_PATH.

Add the extended-length prefix only to the concrete extension path,
leaving sys.path and sys.prefix unchanged.
Convert UNC paths to the \\?\UNC\ form.

Add a Windows regression test that loads a real extension module from a
path exceeding MAX_PATH without changing other Python paths.

Prefixing only sys.path entries that reach MAX_PATH doesn't work:
a search root can be shorter than MAX_PATH while the full
path of an extension under it may exceed the limit.
It would also expose unrelated consumers, such as importlib.metadata
and script handling, to extended-path semantics, where relative
components such as .. are not normalized.
@belitskiy
belitskiy force-pushed the fix-long-windows-paths branch from 47be690 to 1ece5f7 Compare August 21, 2026 18:37
@belitskiy

belitskiy commented Aug 21, 2026

Copy link
Copy Markdown
Contributor Author

I threw an agent at it as well, and it came up with a more filtered solution (explanation in the commit). PTAL

Edit: nvm, buildkite doesn't need approval - will check results myself and then let you know

@belitskiy

Copy link
Copy Markdown
Contributor Author

Buildkite is also passing, except one unrelated, non-Windows, at HEAD failure.

Comment thread news/4071.fixed.md Outdated

@aignas aignas 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.

Thank you

@aignas
aignas enabled auto-merge August 22, 2026 04:57
@aignas
aignas added this pull request to the merge queue Aug 22, 2026
Merged via the queue into bazel-contrib:main with commit 581e6c5 Aug 22, 2026
5 checks passed
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.

3 participants