Skip to content

Windows: Long filename detection #13346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
AA-Turner opened this issue Apr 23, 2025 · 4 comments
Open

Windows: Long filename detection #13346

AA-Turner opened this issue Apr 23, 2025 · 4 comments
Labels
C: error messages Improving error messages good first issue A good item for first time contributors to work on OS: windows Windows specific type: bug A confirmed bug or unintended behavior

Comments

@AA-Turner
Copy link
Contributor

AA-Turner commented Apr 23, 2025

Re: https://discuss.python.org/t/pep-773-a-python-installation-manager-for-windows/77900/160

# Suggest the user to enable Long Paths if path length is
# more than 260
if (
WINDOWS
and error.errno == errno.ENOENT
and error.filename
and len(error.filename) > 260
):
parts.append(
"HINT: This error might have occurred since "
"this system does not have Windows Long Path "
"support enabled. You can find information on "
"how to enable this at "
"https://pip.pypa.io/warnings/enable-long-paths\n"
)

try:
    Path('a' * 300 + '.txt').touch()
except OSError as e:
    exc = e

This gives exc as OSError(22, 'Invalid argument'), where exc.errno == errno.EINVAL, rather than ENOENT.

Python 3.13.2; Windows 11

A

@AA-Turner AA-Turner added S: needs triage Issues/PRs that need to be triaged type: bug A confirmed bug or unintended behavior labels Apr 23, 2025
@AA-Turner AA-Turner changed the title Long path detection Windows: Long path detection Apr 23, 2025
@bwoodsend
Copy link
Contributor

What about a path with a few slashes in it? I vaguely remember that long paths and long names are treated differently. (Sorry, I'm a long way from the nearest Windows machine or I'd just check myself.)

@AA-Turner
Copy link
Contributor Author

AA-Turner commented Apr 23, 2025

It turns out I do have long paths enabled (via the registry rather than group policy), sorry for the false report. The problem seen here is when the filename itself is 255 characters or longer, which does still occur on PyPI.

>>> Path('a/'*300).mkdir(parents=True, exist_ok=True)
>>> Path('a/'*300 + 'a'*251 + '.txt').touch()
>>> Path('a/'*300 + 'a'*252 + '.txt').touch()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "...\Lib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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt'

@AA-Turner AA-Turner changed the title Windows: Long path detection Windows: Long filename detection Apr 23, 2025
@zooba
Copy link
Contributor

zooba commented Apr 23, 2025

Yeah, there are a few different errors that can occur due to long paths. But that particular one is due to a long segment, which is still limited even with the OS setting changed.

You might want to look at the .winerror attribute (if it's there). Windows has much more fine grained error codes than POSIX, and we map a lot of different errors to ENOENT and EINVAL. Still pretty sure you can't tell specifically that the path was too long, though. Catching the error and checking the path argument that was passed in is the "best" way (it's not great).

@ichard26 ichard26 added OS: windows Windows specific good first issue A good item for first time contributors to work on C: error messages Improving error messages and removed S: needs triage Issues/PRs that need to be triaged labels Apr 24, 2025
@ichard26
Copy link
Member

PRs are welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: error messages Improving error messages good first issue A good item for first time contributors to work on OS: windows Windows specific type: bug A confirmed bug or unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants