subprocess

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 11 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 subprocess runs one external command with the conventions every Dockhand tool runner shares: a bounded wait after cancellation, captured output, the context error joined into the cause, and one error shape that names the tool, its command, and what it wrote to stderr. Each caller keeps its own environment policy; this package never adds to or filters it.

A command run to completion, for its output or its exit, goes through Run. os/exec is used directly only by one that holds the terminal, as an editor or xcodes does; runs as long as its caller, as the Tcl interpreter, a guest, and a person's build do; streams what it reads and writes, as a decompressor does; or outlives the process, as the background cleanup does.

Index

Constants

View Source
const DefaultWaitDelay = 15 * time.Second

DefaultWaitDelay is how long a command's output may take to arrive once it has exited, and a command may outlive a canceled context before it's killed. The timer starts as the process exits, so it bounds copying what the process already wrote: a second's delay failed git ls-tree under a load average of 190, "exec: WaitDelay expired before I/O complete". A canceled command is killed at once, so the delay matters only to a command whose children keep its pipes open, which is a hang this ends.

Variables

This section is empty.

Functions

func LookPath added in v0.5.0

func LookPath(name string, env []string) (string, error)

LookPath is where name runs from in env: name itself where it holds a slash, else the first executable file of that name in env's PATH, as exec.LookPath finds one in the process's.

Types

type Error

type Error struct {
	Tool    string
	Command string
	Stderr  string
	Cause   error
}

Error reports a failed command with what it wrote to standard error, or to the combined buffer when the streams were merged.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Result

type Result struct {
	Output []byte
	Stderr []byte
}

Result is what the command wrote. Stderr is empty when Combined.

func Run

func Run(ctx context.Context, spec Spec) (Result, error)

Run executes the command and returns what it wrote, even when it failed.

type Spec

type Spec struct {
	// Tool names the program in errors, such as "git" or "tart".
	Tool string
	// Command names the invocation in errors; empty selects the first argument.
	Command string
	Path    string
	Args    []string
	Dir     string
	// Env is the complete environment; nil inherits the process environment.
	Env   []string
	Stdin io.Reader
	// Stdout receives standard output in addition to the captured copy.
	Stdout io.Writer
	// StdoutOnly sends standard output to Stdout alone, uncaptured, for a
	// stream too large to keep, such as a file being transferred; with
	// Combined, standard error too. Where Stdout is a file, the program
	// writes it itself, and goes on writing if this process ends.
	StdoutOnly bool
	// Stderr receives standard error in addition to the captured copy,
	// as Stdout does standard output; Combined sends it to Stdout instead.
	Stderr io.Writer
	// Combined captures standard error into the same buffer as standard output.
	Combined   bool
	ExtraFiles []*os.File
	// WaitDelay bounds how long the process may outlive a canceled context,
	// and how long its output may take to arrive once it has exited; zero
	// selects DefaultWaitDelay.
	WaitDelay time.Duration
	// Group starts the program in a process group of its own, which a
	// terminal's interrupt doesn't reach, and a canceled context asks it
	// to stop with SIGTERM, its whole group where it may, before
	// WaitDelay's kill: the local provider's guest program, whose builds
	// are its children.
	Group bool
	// Limit bounds the captured bytes of each stream; zero imposes none.
	Limit int64
	// Drain, with a Limit, reads a stream on past it and drops the rest,
	// rather than failing the command: it then ends as it would have, its
	// exit status the verdict, as patch's is. A stream no longer read
	// broke the command's pipe, and failed it, or killed it, for its
	// output alone (the limits sweep, 2026-10-01).
	Drain bool
}

Spec describes one invocation.

Jump to

Keyboard shortcuts

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