Skip to content

Start a program or working folder past Windows' path limit by its 8.3 short name - #125

Merged
HalRiveria merged 2 commits into
mainfrom
fix/long-path-short-name
Dec 3, 2026
Merged

HalRiveria merged 2 commits into
mainfrom
fix/long-path-short-name

Conversation

@HalRiveria-Bot

Copy link
Copy Markdown
Collaborator

What was wrong: Windows won't start a program by a path of 260 characters or more, or in a working folder of 259 or more (it adds a backslash). That holds even with long paths enabled and in the \\?\ form, on both start paths (measured on Windows PowerShell 5.1 and PowerShell 7). So these starts failed with "The filename or extension is too long", "The directory name is invalid", or, for a plain program path on PowerShell 7, "The system cannot find the file specified". Windows PowerShell 5.1's own & $exe seems to start such a program, but only through its fallback for files it can't start: shell32's FindExecutable returns the program by its 8.3 short name, and PowerShell runs that with the long path added in front of the arguments, so the program gets a wrong argument list.

What changed:

  • When a start fails and the program's path or the working folder is over Windows' limit, Start-PkProcessCore tries once more with their 8.3 short names, from GetShortPathNameW with the \\?\ prefix. That covers the batch, the dispatcher, Start-PkProcess and Invoke-PkProcessWithTimeout. The program's arguments are passed as given.
  • Where the volume makes no short names, or the second try fails too, the start fails with the first error, for the paths given. Without a short name, the error also says the path's length is why and to use a shorter path.
  • New C#: PkChildProcessJob.GetShortPath. The README's Limits line is updated.

How it was tested: new harness section BO, run on both start paths (CreatedInJob and Process.Start), in a 358-character folder under %TEMP% with a copy of cmd.exe:

  • A program past the limit runs by its short name, with a clean command line: no long path in front of the arguments.
  • A working folder past the limit is used by its short name. So are both at once, and through Invoke-PkProcessWithTimeout.
  • A missing program, a missing working folder, and a file that isn't a program fail with the error for the path given. The first two also say the length is why.

The cases that need short names skip on a volume without them.

Before the fix (main's kit, this branch's harness, -Section BO): 11 failed, 6 passed, on both 5.1 and 7. The six that pass are setup and the error cases that need no new message. After: all 17 of A and BO pass on both. Full harness on this branch: all 793 assertions pass on Windows PowerShell 5.1 and on PowerShell 7.

…t name

Windows refuses a program path of 260 characters or more and a working
folder of 259 or more, on both start paths and with long paths enabled.
When a start fails with one of those, the kit tries once more with their
short names; without short names, or if that fails too, the first error
is reported as before. Harness section BO covers both start paths.
…if the short-name lookup throws

Without an 8.3 short name to try, a start that failed for a program
path of 260 characters or more, or a working folder of 259 or more,
now adds a sentence saying the length is why. GetShortPath returns null
instead of throwing on characters .NET Framework refuses. Section BO's
error checks look for the retry's short names, not any '~', since
TEMP is itself a short path on some machines.
@HalRiveria
HalRiveria merged commit 9acfc0b into main Dec 3, 2026
3 checks passed
@HalRiveria
HalRiveria deleted the fix/long-path-short-name branch December 3, 2026 06:06
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