Skip to content

fix msvc linker bug for huge command line arguments#406

Open
astrelsky wants to merge 3 commits intopypa:mainfrom
astrelsky:msvc_linker_long_cmdline
Open

fix msvc linker bug for huge command line arguments#406
astrelsky wants to merge 3 commits intopypa:mainfrom
astrelsky:msvc_linker_long_cmdline

Conversation

@astrelsky
Copy link
Copy Markdown

Fixes #226 and pypa/setuptools#4177

This is one of two issues that is blocking flash-attention using ROCM CK on Windows. The other issue is in CK itself and not a pypa problem.

This is just to make it easier for the flash-attention maintainers to find when looking through their comments.
Dao-AILab/flash-attention#2400

@0xDELUXA
Copy link
Copy Markdown

0xDELUXA commented Apr 3, 2026

It’s a bit concerning to see that the last commit was 2 months ago - is the project still actively maintained?

@astrelsky
Copy link
Copy Markdown
Author

@abravalheri @zooba may you get someone to review this please? I only opened this here instead of in setuptools since it was requested to report the issue here in distutils.

The problem is not file path length, it is command line length. So if you're linking thousands of files it doesn't matter how short your paths are, you're going to run out of space.

Comment thread distutils/compilers/C/msvc.py Outdated
# we must pass in the arguments through a file if it is longer
# https://learn.microsoft.com/en-us/cpp/build/reference/linking?view=msvc-170#linker-command-files
# https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa#parameters
if len(' '.join(ld_args)) > 30000:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is not correct if the arguments need to be escaped.

For eg:

>>> import subprocess
>>> ld_args=["1 2"]*5500                           
>>> len(' '.join(ld_args))                    
21999
>>> subprocess.run(["python", "-c", *ld_args])     
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\hostedtoolcache\windows\Python\3.12.10\x64\Lib\subprocess.py", line 548, in run
    with Popen(*popenargs, **kwargs) as process:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\hostedtoolcache\windows\Python\3.12.10\x64\Lib\subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\hostedtoolcache\windows\Python\3.12.10\x64\Lib\subprocess.py", line 1538, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 206] The filename or extension is too long

Copy link
Copy Markdown
Author

@astrelsky astrelsky Apr 3, 2026

Choose a reason for hiding this comment

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

I guestimated the size to allow compensation for things like this. At what point should it be moved to a file then?

Ignore my initial response if you saw it (now deleted), I misunderstood.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

something like

Suggested change
if len(' '.join(ld_args)) > 30000:
if len(shlex.join(ld_args)) > 30000:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Actually, I still fail to see your point. Handling this in a file is something specific to the msvc toolchain. The example you show just uses python in a subprocess?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

something like

Suggested change
if len(' '.join(ld_args)) > 30000:
if len(shlex.join(ld_args)) > 30000:

shlex is for unix shells only.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Actually, I still fail to see your point. Handling this in a file is something specific to the msvc toolchain. The example you show just uses python in a subprocess?

Ok. I understand now, sorry I'm viewing this on my phone and it's not helping. I will return in about 30 minutes when I get off work.

There is an internal function in subprocess I can use for proper escaping, will that be acceptable to use?

@astrelsky astrelsky requested a review from isuruf April 3, 2026 18:22
Comment thread distutils/compilers/C/msvc.py Outdated
Co-authored-by: Isuru Fernando <isuruf@gmail.com>
@0xDELUXA
Copy link
Copy Markdown

0xDELUXA commented Apr 3, 2026

@isuruf Thanks for the review!

Could someone with merge rights take a quick look as well?

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.

Distutils passes too long a command to link.exe, causing [WinError 206] The filename or extension is too long

3 participants