Documentation
¶
Overview ¶
Package winexec builds safe argv for spawning Windows PowerShell (powershell.exe, i.e. Windows PowerShell 5.1, as distinct from pwsh/PowerShell 7) so text carried through it — script content in, captured stdout out — survives non-ASCII characters correctly.
F-41 (2026-09-24, reproduced live on a plain Windows box, isolating each hop in turn): the observed defect is NOT in argv decoding — a non-ASCII "-Command <script>" argument reaches PowerShell's parser correctly (Go's os/exec always calls the WIDE CreateProcessW on Windows, and confirmed here by having the script write the text it received straight to a file: byte-perfect, with or without -EncodedCommand). It is in OUTPUT capture: Windows PowerShell 5.1's default Console.OutputEncoding, when stdout is redirected to a pipe (which os/exec.Cmd.Output/CombinedOutput always is), follows the host's legacy OEM/ANSI code page rather than UTF-8. Measured round trip for "¿VOLVERÁ EL ROTATIVO? ñ ü é" via `Write-Output` on a pipe:
without the fix: \xa8VOLVERA EL ROTATIVO? \xa4 \x81 \x82 (Á silently dropped to
bare A; ¿ ñ ü é mapped
to unrelated single-
byte codepage values)
with the fix : ¿VOLVERÁ EL ROTATIVO? ñ ü é (byte-perfect)
Setting Console.OutputEncoding to UTF-8 before the script produces any output fixes this completely, with or without -EncodedCommand — see TestSafeScriptArgs_SurvivesPowerShell5OutputMangling for the live proof (and its A/B/C/D breakdown of which combination fixes what, kept in the test's comment since it is exactly how this was isolated).
Index ¶
- func Command(name string, arg ...string) *exec.Cmd
- func CommandContext(ctx context.Context, name string, arg ...string) *exec.Cmd
- func CommandDetached(name string, arg ...string) *exec.Cmd
- func CommandIfNoConsole(name string, arg ...string) *exec.Cmd
- func EncodedCommandArgs(script string) []string
- func HideWindow(cmd *exec.Cmd) *exec.Cmd
- func HideWindowDetached(cmd *exec.Cmd) *exec.Cmd
- func HideWindowIfNoConsole(cmd *exec.Cmd) *exec.Cmd
- func KillTree(p *os.Process) error
- func KillTreeOnCancel(cmd *exec.Cmd) *exec.Cmd
- func SafeScriptArgs(script string) []string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Command ¶
Command is exec.Command whose child opens no window on Windows (see HideWindow). On every other platform it is exec.Command.
This is the constructor for every child the harness starts and waits for, so one rule holds in one place: a console program started by a process that has no console of its own (a detached holder, a service, a launcher started hidden) gets a NEW visible console window on Windows unless it is started hidden, and with Windows Terminal as the default terminal that window is a Windows Terminal window on the operator's desktop. One test run put several there, one titled taskkill.exe. A child that must outlive this process is CommandDetached, and a program the operator named is CommandIfNoConsole. A new direct os/exec constructor, a process created through the Win32 or os.StartProcess calls, or an exec.Cmd literal in a Go file of this module fails TestEveryChildProcessStartIsHidden until it uses these or is listed there with the reason it must stay as it is.
func CommandContext ¶
CommandContext is exec.CommandContext whose child opens no window on Windows (see Command).
func CommandDetached ¶
CommandDetached is exec.Command whose child always owns a hidden console of its own on Windows (see HideWindowDetached), for a child that outlives this process or must not share its console.
func CommandIfNoConsole ¶
CommandIfNoConsole is exec.Command whose child is hidden only when this process has no console (see HideWindowIfNoConsole), for a program the operator names and may run at their terminal.
func EncodedCommandArgs ¶
EncodedCommandArgs returns the powershell.exe argv for running `script` via -EncodedCommand (a base64 UTF-16LE blob) instead of a raw "-Command <script>" argument. This does not by itself fix F-41 (see the package doc — that is an output- capture defect, not an argv one), but it is still the right way to embed arbitrary caller-supplied text into a script: -Command re-tokenizes its argument as PowerShell source, so a caller's text containing a quote, backtick, or `$` can break out of whatever quoting the caller built by hand. -EncodedCommand sidesteps that entirely. Safe for any Unicode script text, including the empty string.
func HideWindow ¶
HideWindow is a no-op outside Windows: there is no console window to hide. It returns cmd.
func HideWindowDetached ¶
HideWindowDetached is a no-op outside Windows. It returns cmd.
func HideWindowIfNoConsole ¶
HideWindowIfNoConsole is a no-op outside Windows. It returns cmd.
func KillTree ¶
KillTree sends SIGKILL to the process group p leads, which is every process it started that has not left the group. When p leads no group (it was not started with Setpgid) p alone is killed.
func KillTreeOnCancel ¶
KillTreeOnCancel makes the cancellation of cmd's context (cmd comes from CommandContext) end the whole process group the program started, not only the program: a wrapper script leaves what it backgrounded running, with the output pipe it inherited held, after the script alone is killed. The program is started as the leader of a group of its own (Setpgid) so the group can be signalled without touching this process's. It sets cmd.Cancel and returns cmd. Call it before the command starts.
func SafeScriptArgs ¶
SafeScriptArgs returns the powershell.exe argv for running `script` such that (1) its own stdout, when captured through a redirected pipe, comes back as UTF-8 instead of mangled through WinPS 5.1's legacy default (F-41 — the package doc has the measured before/after) and (2) the script text itself is carried via -EncodedCommand, so caller-supplied text embedded in it cannot break PowerShell's own argument tokenizing (see EncodedCommandArgs). This is the helper to reach for whenever the caller will parse the script's captured stdout and it might contain non-ASCII text; Go's string(out) already assumes UTF-8, so no decoding step is needed on the caller's side.
Types ¶
This section is empty.