Skip to content

fix: escape all cmd/bat file args - #172

Merged
43081j merged 1 commit into
mainfrom
jg/windows-nonsense
Sep 8, 2026
Merged

43081j merged 1 commit into
mainfrom
jg/windows-nonsense

Conversation

@43081j

@43081j 43081j commented Sep 3, 2026

Copy link
Copy Markdown
Member

No description provided.

@43081j
43081j force-pushed the jg/windows-nonsense branch from 032952d to fc01bcb Compare September 8, 2026 07:54
@43081j
43081j merged commit a9d7702 into main Sep 8, 2026
9 checks passed
@43081j
43081j deleted the jg/windows-nonsense branch September 8, 2026 07:55
JoshuaKGoldberg added a commit to JoshuaKGoldberg/formatly that referenced this pull request Nov 19, 2026
#751)

## PR Checklist

- [x] Addresses an existing open issue: fixes #675
- [ ] That issue was marked as [`status: accepting
prs`](https://github.com/JoshuaKGoldberg/formatly/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22)
- [x] Steps in
[CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/formatly/blob/main/.github/CONTRIBUTING.md)
were taken

## Overview

On Windows, `resolvePackageCommand` falls back to `npx` / `pnpm` /
`yarn`, which are `.cmd` shims. Node's `spawn` can't run those without a
shell, so oxfmt, Biome, dprint, and out-of-cwd Prettier failed with
`spawn npx ENOENT`.

A new `spawnCommand` helper first runs each command through
[tinyexec](https://github.com/tinylibs/tinyexec)'s
`normalizeSpawnCommand`, then calls `node:child_process`'s `spawn`. On
Windows, `normalizeSpawnCommand` wraps shims in an escaped `cmd.exe /d
/s /c` call. On other platforms it returns its inputs unchanged. Every
formatter spawn goes through the helper. dprint's `editor-info` probe
moved from `execFile` to the shared `spawnOutput`, and it still falls
back to `dprint fmt --stdin` on any failure.

tinyexec was chosen over cross-spawn because it has no dependencies, and
e18e's
[module-replacements](https://github.com/es-tooling/module-replacements/blob/main/docs/modules/cross-spawn.md)
suggests it as cross-spawn's replacement. It's also already in most
modern toolchains through vitest, tsup, and lint-staged. Using only its
normalization, rather than its `x()` runner, keeps the raw
`ChildProcess` that the long-lived dprint editor service needs.

Known limitations on Windows:

- cmd.exe still expands `%VAR%` in arguments passed through a `.cmd`
shim.
- In tinyexec 1.3.1, arguments get double meta-char escaping only for
`.cmd` files in `node_modules/.bin`, not for `npx.cmd`. This only
matters for arguments containing `"`, which file paths can't contain.
tinyexec has fixed it upstream
([tinylibs/tinyexec#172](tinylibs/tinyexec#172)),
but the fix isn't released yet.
- A missing command surfaces as a cmd.exe exit code 1 with a "not
recognized" message on stderr, not as an `ENOENT` error.
- The dprint editor-service child is the cmd.exe wrapper. dprint still
exits with its parent because of `--parent-pid`.

⚠️ Not yet tested on Windows. #753 adds Windows to CI.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

1 participant