Repository navigation
fix(shellwrap): hand cmd.exe its command line so paths with spaces start - #1980
marcus-beauregard wants to merge 1 commit into
Conversation
WrapWithUserShell quotes a stdio command path for cmd.exe, and os/exec then escaped every embedded quote as \" while building the Windows command line. cmd.exe has no backslash escape, so a server installed under "Program Files" was looked up under a name that began with a quote and never started (smart-mcp-proxy#1960). ApplyCommandLine sets SysProcAttr.CmdLine for cmd.exe /c invocations at both spawn sites - the stdio process-group command func and the launcher - using /s /c so cmd.exe strips exactly the outer pair of quotes and parses the string WrapWithUserShell built. Unix shells and Git Bash keep the argv path. The command-string builder and shell detection live in shellwrap so they are testable on any host; the end-to-end test is Windows-only.
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
@marcus-beauregard have in mind I also proposed a fix for this issue at #1963. |
|
Thanks @marcus-beauregard for picking up #1960 and for the tests here — the diagnosis is right. Two PRs fix the same bug, and we are going with #1963: it was opened first and also covers the Docker fallback path in Your tests are the part #1963 does not have: its tests only run on Windows, while yours run on Linux and macOS too. If you'd like to send them as a tests-only PR on top of |
Fixes #1960.
What was wrong
WrapWithUserShellquotes a stdio command path for cmd.exe and returns the whole thing as one argv element (["/c", commandString]). When os/exec builds the Windows command line it escapes each embedded quote as ", and cmd.exe has no backslash escape, so a server installed under a path with a space was looked up under a name that began with a backslash-quote:The fix
shellwrap.ApplyCommandLinesetsSysProcAttr.CmdLinewhen the command being started iscmd.exe /c <commandString>, so CreateProcess uses the line verbatim instead of letting argv escaping mangle it.internal/shellwrap/shellwrap.go:isCmdExeShellandcmdExeCommandLine, which emits<shell> /s /c "<commandString>". The extra outer quote pair is the one cmd.exe's/Srule strips, so the shell-quoted command inside survives unchanged — whether or not it starts with its own quote. The shell path is omitted when it contains a space (it cannot be quoted inside the line without breaking/S, and os/exec already passes the executable aslpApplicationName).internal/shellwrap/cmdline_windows.go/cmdline_other.go: the Windows implementation and a no-op stub.internal/upstream/core/process_windows.go: applied afterwinjob.MarkSuspended(that helper preserves otherSysProcAttrfields, soCREATE_SUSPENDEDandCmdLinecoexist).internal/upstream/core/connection_launcher.go: applied to the launcher command as well; it is a no-op for the docker/direct-exec branch, which has no/c.Unix shells and Git Bash on Windows keep the argv path — their command string uses POSIX quoting, which argv escaping does not damage.
Tests
cmdline_test.go(any host): shell detection; the/Scontract — stripping the first and last quote after/s /cmust give back exactly the command string, checked for a spaced program path, a bare command and a shell path that itself has a space; a shape test thatWrapWithUserShellstill returns[/c, commandString]so the rewrite precondition cannot drift silently; and the no-op cases.cmdline_windows_test.go(Windows): builds a.cmdunder<temp>/Program Files/HTTP Toolkit/, wraps it with an argument, applies the command line and runs it, expecting the script's own output.All pass.
unit-tests.ymlruns the Windows matrix only on pushes to main, so the Windows-only test compiles here (GOOS=windows go vet) and first executes there; the string-level contract it depends on is covered on Linux.