winexec

package
v0.206.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Command

func Command(name string, arg ...string) *exec.Cmd

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

func CommandContext(ctx context.Context, name string, arg ...string) *exec.Cmd

CommandContext is exec.CommandContext whose child opens no window on Windows (see Command).

func CommandDetached

func CommandDetached(name string, arg ...string) *exec.Cmd

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

func CommandIfNoConsole(name string, arg ...string) *exec.Cmd

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

func EncodedCommandArgs(script string) []string

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

func HideWindow(cmd *exec.Cmd) *exec.Cmd

HideWindow is a no-op outside Windows: there is no console window to hide. It returns cmd.

func HideWindowDetached

func HideWindowDetached(cmd *exec.Cmd) *exec.Cmd

HideWindowDetached is a no-op outside Windows. It returns cmd.

func HideWindowIfNoConsole

func HideWindowIfNoConsole(cmd *exec.Cmd) *exec.Cmd

HideWindowIfNoConsole is a no-op outside Windows. It returns cmd.

func KillTree

func KillTree(p *os.Process) error

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

func KillTreeOnCancel(cmd *exec.Cmd) *exec.Cmd

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

func SafeScriptArgs(script string) []string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL