Skip to content
Myr

windows.md

This file

Path

docs/windows.md

102 lines · 5.9 KiB · Markdown

Last changed by

Fix arguments of Windows batch files that read them with `%~1`Sindre Sorhusbefore adoption93bddc4

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 1–26why, one step back
1<picture>
2 <source media="(prefers-color-scheme: dark)" srcset="../media/logo_dark.svg">3 <img alt="execa logo" src="../media/logo.svg" width="400">4</picture>5<br>67# 📎 Windows89Although each OS implements subprocesses very differently, Execa makes them cross-platform, except in a few instances.1011## Shebang1213On Unix, executable files can use [shebangs](https://en.wikipedia.org/wiki/Shebang_(Unix)).1415```js16import {execa} from 'execa';1718// If script.js starts with #!/usr/bin/env node19await execa`./script.js`;2021// Then, the above is a shortcut for:22await execa`node ./script.js`;23```2425Although Windows does not natively support shebangs, Execa adds support for them.26

Document and test Windows fixes (#1248)Sindre Sorhusbefore adoption4b0edb3

lines 27–37why, one step back
27## File extensions
2829On Windows, executables have a file extension, such as `.exe`, `.cmd`, `.bat` or `.com`. When the file is passed without any extension, Execa resolves it using the [`PATHEXT`](https://ss64.com/nt/path.html#pathext) environment variable, just like the OS does when looking up commands.3031```js32import {execa} from 'execa';3334// Runs `npm.cmd`35await execa`npm run build`;36```37

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 38why, one step back
38This means `.cmd` and `.bat` files can be run directly. Unlike Node.js, Execa does not require a [shell](shell.md) (nor a `cmd.exe /c` prefix) for this. Relative paths (such as `./folder/executable`) and files whose path contains spaces also work without a shell, just like on Unix. Execa handles this automatically.

Document and test Windows fixes (#1248)Sindre Sorhusbefore adoption4b0edb3

line 39why, one step back
39

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 40–41why, one step back
40## Signals
41

Document `SIGQUIT` (#1022)ehmickybefore adoptionc5bd9d8

line 42why, one step back
42Only few [signals](termination.md#other-signals) work on Windows with Node.js: [`SIGTERM`](termination.md#sigterm), [`SIGKILL`](termination.md#sigkill), [`SIGINT`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGINT) and [`SIGQUIT`](termination.md#sigquit). Also, sending signals from other processes is [not supported](termination.md#signal-name-and-description). Finally, the [`forceKillAfterDelay`](api.md#optionsforcekillafterdelay) option [is a noop](termination.md#forceful-termination) on Windows.

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 43–49why, one step back
43
44## Asynchronous I/O4546The default value for the [`stdin`](api.md#optionsstdin), [`stdout`](api.md#optionsstdout) and [`stderr`](api.md#optionsstderr) options is [`'pipe'`](output.md#stdout-and-stderr). This returns the output as [`result.stdout`](api.md#resultstdout) and [`result.stderr`](api.md#resultstderr) and allows for [manual streaming](streams.md#manual-streaming).4748Instead of `'pipe'`, `'overlapped'` can be used instead to use [asynchronous I/O](https://learn.microsoft.com/en-us/windows/win32/fileio/synchronous-and-asynchronous-i-o) under-the-hood on Windows, instead of the default behavior which is synchronous. On other platforms, asynchronous I/O is always used, so `'overlapped'` behaves the same way as `'pipe'`.49

Improve documentation for `windowsVerbatimArguments` (#1149)ehmickybefore adoptionc4cb62a

line 50why, one step back
50## Escaping

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 51why, one step back
51

Improve documentation for `windowsVerbatimArguments` (#1149)ehmickybefore adoptionc4cb62a

line 52why, one step back
52Windows requires files and arguments to be quoted when they contain spaces, tabs, backslashes or double quotes. Unlike Unix, this is needed even when no [shell](shell.md) is used.

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 53why, one step back
53

Improve documentation for `windowsVerbatimArguments` (#1149)ehmickybefore adoptionc4cb62a

lines 54–59why, one step back
54When not using any shell, Execa performs that quoting automatically. This ensures files and arguments are split correctly.
5556```js57await execa`npm run ${'task with space'}`;58```59

Fix arguments of Windows batch files that read them with `%~1`Sindre Sorhusbefore adoption93bddc4

lines 60–69why, one step back
60`.cmd` and `.bat` files are run through `cmd.exe`, so their arguments are also escaped to prevent `cmd.exe` from interpreting them. This uses the same escaping as [Rust](https://github.com/rust-lang/rust/blob/main/library/std/src/sys/args/windows.rs), which is safe whether the batch file reads its arguments (`%1`, `"%~1"`) or forwards them (`%*`), like the shims in `node_modules/.bin` do.
6162> [!WARNING]63> Batch files can still misuse their arguments, which no escaping can prevent:64> - `%~1` receives the escaped argument: each `"` is doubled, and so are trailing backslashes.65> - Using `%~1` outside double quotes, or `%1` inside double quotes (like `if "%1" == ""`), lets `cmd.exe` interpret the argument's special characters, which allows command injection.66> - Forwarding arguments with `call`, or enabling delayed expansion, makes `cmd.exe` expand `%` or `!` in them again.67>68> Also, arguments cannot contain line breaks, since `cmd.exe` cannot escape them. This applies to any file run through `cmd.exe`, i.e. any file other than `.exe` and `.com`.69

Improve documentation for `windowsVerbatimArguments` (#1149)ehmickybefore adoptionc4cb62a

lines 70–86why, one step back
70When using a [shell](shell.md), the user must manually perform shell-specific quoting, on both Unix and Windows. When the [`shell`](api.md#optionsshell) option is `true`, [`cmd.exe`](https://en.wikipedia.org/wiki/Cmd.exe) is used on Windows and `sh` on Unix. Unfortunately, both shells use different quoting rules. With `cmd.exe`, this mostly involves double quoting arguments and prepending double quotes with a backslash.
7172```js73if (isWindows) {74 await execa({shell: true})`npm run ${'"task with space"'}`;75} else {76 await execa({shell: true})`npm run ${'\'task with space\''}`;77}78```7980When using other Windows shells (such as PowerShell or WSL), Execa performs `cmd.exe`-specific automatic quoting by default. This is a problem since Powershell uses different quoting rules. This can be disabled using the [`windowsVerbatimArguments: true`](api.md#optionswindowsverbatimarguments) option.8182```js83if (isWindows) {84 await execa({windowsVerbatimArguments: true})`wsl ...`;85}86```

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 87–89why, one step back
87
88## Console window89

Document Windows subprocess tree cleanup workaround (#1250)Sindre Sorhusbefore adoption97dc613

line 90why, one step back
90If the [`windowsHide`](api.md#optionswindowshide) option is `false`, the subprocess is run in a new console window. This is necessary to make [`SIGINT` work](https://github.com/nodejs/node/issues/29837) on Windows, and to [clean up](termination.md#current-process-exit) subprocesses which spawn their own subprocesses, such as when [running an npm script through a shell](https://github.com/sindresorhus/execa/issues/433).

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 91–102why, one step back
91
92## UID and GID9394By default, subprocesses are run using the current [user](https://en.wikipedia.org/wiki/User_identifier) and [group](https://en.wikipedia.org/wiki/Group_identifier). The [`uid`](api.md#optionsuid) and [`gid`](api.md#optionsgid) options can be used to set a different user or group.9596However, since Windows uses a different permission model, those options throw.9798<hr>99100[**Next**: 🔍 Differences with Bash and zx](bash.md)\101[**Previous**: 🐛 Debugging](debugging.md)\102[**Top**: Table of contents](../readme.md#documentation)