Stop wrapping commands in cmd.exe on Windows - #898
Open
bwilde-castle wants to merge 3 commits into
Open
Conversation
`Subprocess.spawn` wrapped every command in `cmd.exe /c` and joined the arguments into a single string before handing them to childprocess. That collapsed the whole command into one argv element, and childprocess 5.x on Windows is a thin shim over `Process.spawn`, which sees the embedded spaces, wraps the element in quotes and backslash-escapes the quotes already inside it. cmd.exe strips the outer quotes and the child's C runtime then reads the remaining `\"` as a literal quote rather than a delimiter, so the argument gets re-split on whitespace. For the pre-commit stash this meant `git stash save` received the trailing UTC offset of the stash message as a switch of its own and aborted with `error: unknown switch '0'`, leaving hooks unable to run at all. No cmd.exe escaping scheme fixes this properly either: quoting makes `& | < > ( )` literal but does not prevent `%VAR%` expansion, and carets inside quotes survive as literal carets. The wrapper is unnecessary. `Process.spawn` performs the same command lookup cmd.exe was being used for, so `.bat`, `.cmd`, extensionless RubyGems shims and bare names resolved via PATHEXT all launch correctly when passed straight to `ChildProcess.build`, and arguments stay atomic because no shell ever re-parses them. Drop the wrapper and pass the argument vector through untouched on every platform. This requires the `Process.spawn`-based Windows backend that childprocess introduced in 5.0.0, so raise the dependency floor accordingly. Fixes sds#847
`mklink` and `dir` are cmd.exe built-ins with no standalone executable, so removing the cmd.exe wrapper for all Subprocess calls broke FileUtils.symlink (and left FileUtils.symlink?/readlink working only by accident, since `dir` happens to be on MRI's hardcoded list of legacy cmd.exe built-ins that Process.spawn falls back to). Unlike the removed general-purpose wrapper, this passes cmd.exe, /c, and the command as separate argv elements rather than joining them into one pre-escaped string, so it doesn't reintroduce the argument corruption from sds#847.
Subprocess no longer branches on Overcommit::OS.windows?, so stubbing it to both true and false ran the same assertions twice and the comment claiming it exercised "the Windows code path" was no longer accurate. Collapse to one context, drop the example that duplicated an existing assertion, and match the surrounding suite's should_receive/stub syntax.
bwilde-castle
marked this pull request as ready for review
September 11, 2026 21:46
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An attempt at Issue #847. This isn't a corner case:
pre-commitandcommit-msghooks both stash unstaged changes before running hooks, by default, so any Windows user doing a partial commit (staging some but not all changes) hits this — hooks fail outright witherror: unknown switch '0'instead of running at all.I commented on the issue a couple of months ago; I've since tested this fix end to end on Windows 11 / Ruby 3.3, and while the suggestion still looks right, my description of the cause back then was off. Here's what I think is actually going on.
The problem
win32_prepare_argsescapes metacharacters per argument, then joins everything into one string behindcmd.exe /c:The escaping doesn't seem to be what breaks; the join is. It collapses the command into a single argv element, and childprocess 5.x on Windows is a thin shim over
Process.spawn, which sees the embedded spaces, wraps the element in quotes, and backslash-escapes the quotes already inside it. cmd.exe strips the outer pair, and the child's C runtime then reads the surviving\"as a literal quote rather than a delimiter — so the argument re-splits on whitespace.With the pre-commit stash message (
Overcommit: Stash of repo state before hook run at 2024-04-10 12:34:56 -0700),git stash savereceives the UTC offset as a switch of its own and aborts witherror: unknown switch '0'. On a throwaway repo I get exit 129 with that message onmain, and exit 0 with the message intact on this branch.I couldn't find an escaping scheme that fixes this while keeping the wrapper: inside double quotes
& | < > ( )are already literal,%VAR%still expands and can't be caret-escaped, and carets inside quotes reach the child as literal carets.The change
Drop the wrapper and hand the argument vector to
ChildProcess.buildunmodified.Process.spawnappears to do the command lookup the wrapper was standing in for —.bat,.cmd, extensionless RubyGems shims andPATHEXTnames all launched fine in my testing, including the realbundle.bat— and arguments stay atomic because no shell re-parses them.Both call sites were already gated on
OS.windows?, so this is Windows-only in effect; macOS and Linux behaviour is unchanged.That
Process.spawnbackend arrived in childprocess 5.0.0, which replaced the Windows FFI layer. Before that, Windows used rawCreateProcess, which does noPATHEXTresolution, so I've raised the gemspec floor from>= 0.6.3to>= 5.0.0. I realise that's the debatable part and entirely your call — for context, the< 6ceiling was already there, 5.0.0 is from 2022, this repo'sGemfile.lockresolves to 5.x, and 4.1.0/5.0.0/5.1.0 all declarerequired_ruby_version >= 2.4.0.One wrinkle:
mklinkanddir(used byFileUtilsfor symlinks) are cmd.exe built-ins with no standalone executable, so dropping the wrapper brokeFileUtils.symlink. Those two call sites now passcmd.exe,/c, and the command as separate argv elements, which avoids reintroducing the join-based bug above.Tests
Adds
spec/overcommit/subprocess_spec.rb— there wasn't coverage forSubprocessbefore. It usesGem.rubyand a NUL-joinedARGVdump so it should be platform-agnostic and pass on the current Linux CI. It covers the #847 case directly, plus results and exit status,options[:input], metacharacter and empty-string arguments, and thatChildProcess.buildreceives the argv verbatim from both.spawnand.spawn_detached.On Windows locally,
bundle exec overcommit --runpasses and rubocop is clean.bundle exec rspecgives 1643 examples / 2 failures, the same as onmain(git_repo_spec.rb:341, andutils_spec.rb:142, which needswmic);git_repo_spec.rb:218fails onmainand passes here.Notes
require 'overcommit/os'; happy to put it back if you'd rather keep the diff minimal.mklink/dirfix —FileUtilshas no existing spec file and Windows CI is disabled (Various specs fail on Windows for all Ruby versions #836).Process.spawn, so it looks unrelated.commandwould lose it. The API has always been argv-based andexecute_in_backgroundalready raises on|, so I don't think it'll be an issue, but the reviewer will probably know better.