dotnet / dotnet/msbuild

Exec and (very) long commands on Windows

Open
#2,530 5 comments 9 reactions 0 assignees View on GitHub
triaged
Dominant language
C#
Stars
5.5k
Forks
1.5k
Avg merge
1d 8h
Merged PRs (30d)
141

Description

I've observed some problematic behavior when using the `Exec` task on Windows with a very long (~17000 characters) command string. There are a few points I've discovered:

* `Exec` is documented as running through `cmd.exe` -- the command is not launched directly in a new process.
* Additionally, the command itself is actually run through a batch script containing the command line and some extra commands. The script is launched via `cmd.exe`.

Error behavior:
* `cmd.exe` has documented limits on command-line length. On relevant systems, the limit is [8191 characters](https://support.microsoft.com/en-us/help/830473/command-prompt-cmd--exe-command-line-string-limitation).
* When an extremely long command is run directly with `cmd /c `, you get an error message: `The command line is too long.`
* **When an extremely long command is embedded in a script and run indirectly through `cmd /c `, you get very misleading and dangerous behavior.**

It appears that cmd.exe will attempt to run a command in a script file which is over the length limit, but will silently remove individual characters at the command length boundary. In my case, characters 8192 and 16383 are being silently deleted from the command, but the rest of the command line is unchanged. I spent several hours debugging this behavior, because I thought something was wrong in my app code.

The current state (silent and confusing errors) seems undesirable. Can we do one or both of the following?

* Add an alternative task which launches processes directly: `DirectExec` or something like that. This could be written by anyone, but it might be generally useful enough to include in MSBuild.
* Is it feasible for this to just be an option on `Exec` itself?
* Error out when a command is too long on Windows. We should be able to detect if a string is over the 8191 limit. If we do, we should not try to execute it given the above behavior.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.