std.process

Program arguments and child-process management.

Importuse std.process

Types

struct RunOutput#

Captured child output from std.process.try_run.

status: int

The child's exit status.

stdout: string

Everything the child wrote to standard output.

stderr: string

Everything the child wrote to standard error.

struct CommandResult#

Captured child output from std.process.run*. A failed spawn is code = -1, ok = false, stderr = <error name>.

code: int

The child's exit code, or -1 when the spawn itself failed.

stdout: string

Everything the child wrote to standard output.

stderr: string

Everything the child wrote to standard error, or the error name when the spawn failed.

ok: bool

Whether the child ran and exited zero.

struct ActiveSubprocesses#

How many children are running, in total and per tool.

total: int

How many children are running right now.

by_tool: {string: int}

The same count broken down by program name.

struct SpawnResult#

What a finished child left behind. Pipes are byte strings, as everywhere else in this port — moon strings are bytes and nothing validates encoding, so a probe's JSON and a thumbnail's bytes travel the same way.

stdout: string

Buffered standard output as bytes; empty for a streaming run.

stderr: string

Buffered standard error as bytes.

exit_code: int

The child's exit code; a signal is reported negative.

struct RunOptions#

Where a checked run happens and with what environment. cwd empty means inherit; env entries are "K=V" and extend the current environment.

Deliberately not ProcessOptions, which is the low-level spawn's and also carries the three stdio dispositions. A checked run owns those: it drains both pipes concurrently, so stdout: "inherit" would make it read nothing or hang. An options type whose fields are silently overwritten is worse than two types.

cwd: string

Directory to run in; empty inherits the current one.

env: [string]

Extra environment entries spelled "KEY=VALUE", added to the current environment.

struct ProcessOptions#

Streaming child spawn configuration.

cwd: string
env: [string]
stdin: string
stdout: string
stderr: string
struct Child#

Streaming child process from std.process.spawn.

stdin: Optional<File>
stdout: Optional<File>
stderr: Optional<File>
_handle: ChildHandle
struct ChildHandle#

Internal process ownership handle.

An opaque handle: only std.process can produce one, and its contents are not reachable from moon source.

_handle: NativeHandle

The native resource.

Constants

const CANCELLED: string#

The error name a stopped child fails with, so a caller can tell "I stopped it" from "it failed".

A name, not a message suffix. This used to be failure.message.ends_with(" was cancelled"), which is a string sniff over text a caller may reasonably want to change or translate, and which a genuine failure whose message happened to end that way would satisfy.

Functions

fn spawn(argv: [string]) -> Child#

Spawns argv directly with piped stdout/stderr and null stdin.

  • argv: Program and its arguments; no shell parsing is applied.
async fn main() {
  match std.process.try_spawn(["moon", "--version"]) {
    ok(child) => {
      println(child.pid())
      println(await child.wait())
    },
    err(failure) => println(failure.message),
  }
}
fn try_spawn(argv: [string]) -> Result<Child, Error>#

Result-returning twin of spawn.

  • argv: Program and its arguments; no shell parsing is applied.
extern fn spawn_opts(argv: [string], options: ProcessOptions) -> Child#

Spawns argv with explicit cwd, environment additions, and stdio dispositions.

  • argv: Program and its arguments; no shell parsing is applied.
  • options: Working directory, environment additions, and the three stdio dispositions.
extern fn try_spawn_opts(argv: [string], options: ProcessOptions) -> Result<Child, Error>#

Result-returning twin of spawn_opts.

  • argv: Program and its arguments; no shell parsing is applied.
  • options: Working directory, environment additions, and the three stdio dispositions.
async fn spawn_checked(command: [string], options: RunOptions) -> Result<SpawnResult, Error>async#

Runs command, buffering both pipes, and fails when the child does.

  • command: Program and its arguments; no shell parsing is applied.
  • options: Working directory and environment additions for the run.
async fn main() {
  let options = std.process.in_dir(".")
  let captured = (await std.process.spawn_checked(["moon", "--version"], options)).unwrap()
  println(captured.exit_code)
  println(captured.stdout)

  let streamed = await std.process.spawn_streaming(
    ["moon", "--version"],
    std.process.no_options(),
    |line| println("child: " + line),
  )
  println(streamed.is_ok())
  println(std.process.active_subprocesses().total)
}
async fn spawn_streaming(command: [string], options: RunOptions, on_line: fn(string) -> void) -> Result<SpawnResult, Error>async#

Runs command with stdout streamed line by line to on_line rather than buffered; the returned stdout is empty, as it was in the Bun port.

  • command: Program and its arguments; no shell parsing is applied.
  • options: Working directory and environment additions for the run.
  • on_line: Called with each stdout line, without its newline.
async fn spawn_cancellable(command: [string], options: RunOptions, on_line: fn(string) -> void, should_stop: fn() -> bool) -> Result<SpawnResult, Error>async#

Runs command streaming, and terminates it if should_stop starts reporting true. A run stopped that way fails with a cancellation message rather than the child's exit status, which is what tells a caller apart from a genuine ffmpeg failure.

  • command: Program and its arguments; no shell parsing is applied.
  • options: Working directory and environment additions for the run.
  • on_line: Called with each stdout line, without its newline.
  • should_stop: Polled while the child runs; the first true terminates it.
fn no_options() -> RunOptions#

Returns default process execution options.

fn in_dir(cwd: string) -> RunOptions#

Returns process options rooted in the supplied directory.

  • cwd: Directory the run happens in.
fn active_subprocesses() -> ActiveSubprocesses#

Returns the current subprocess tracker.

fn is_cancellation(failure: Error) -> bool#

Whether a failure came from spawn_cancellable stopping the child rather than the child failing on its own.

  • failure: The error a checked run returned.
extern fn deinit(child: Child)#

Closes the child's pipes and terminates and reaps it if still running. Register defer child.deinit() immediately after a successful spawn. A prior wait or pipe close is safe; repeated cleanup has no effect.

  • child: Child and its remaining pipes to release.
extern fn wait(child: Child) -> Future<int>#

Returns a future that reaps the child and resolves to its exit code; signals are negative.

  • child: Child to reap; it may be waited on only once.
extern fn kill(child: Child)#

Sends the platform termination signal without consuming wait.

  • child: Child to signal; wait still has to reap it.
extern fn kill_with(child: Child, signal: string)#

Sends term, kill, int, or hup.

  • child: Child to signal.
  • signal: One of term, kill, int, or hup.
extern fn pid(child: Child) -> int#

Returns the process id, or -1 after reaping.

  • child: Child to identify.
extern fn exit(code: int)#

Terminates the process with the supplied exit code.

  • code: Exit status for this process, taken modulo 256.
fn arg_count() -> int#

Returns the number of process arguments.

extern fn run_command(command: string) -> int#

Runs a shell command and returns its exit code.

  • command: Command line handed to the platform shell.
extern fn try_run(command: string) -> Result<RunOutput, Error>#

Runs a shell command and returns output or an error value.

  • command: Command line handed to the platform shell.
extern fn run(command: string) -> CommandResult#

Runs a shell command and captures its status and output.

  • command: Command line handed to the platform shell.
let shell = std.process.try_run("printf moon").unwrap()
println(shell.status)
println(shell.stdout)

let direct = std.process.run_args(["moon", "--version"])
println(direct.ok)
println(std.process.run_args_in(".", ["moon", "--version"]).code)
println(std.process.run_with_env("env", ["MOON_MODE=tour"]).stdout)
extern fn run_in(cwd: string, command: string) -> CommandResult#

Runs a shell command in the supplied working directory.

  • cwd: Directory to run in.
  • command: Command line handed to the platform shell.
extern fn run_args(argv: [string]) -> CommandResult#

Runs an argv vector without shell parsing.

  • argv: Program and its arguments; no shell parsing is applied.
extern fn run_args_in(cwd: string, argv: [string]) -> CommandResult#

Runs an argv vector in the supplied working directory.

  • cwd: Directory to run in.
  • argv: Program and its arguments; no shell parsing is applied.
extern fn run_with_env(command: string, env: [string]) -> CommandResult#

Runs a shell command with explicit environment entries.

  • command: Command line handed to the platform shell.
  • env: Environment entries spelled "KEY=VALUE".
fn arg(index: int) -> string#

Returns one process argument, or an empty string out of range.

  • index: Zero-based position among the process arguments.
println(std.process.program_name())
println(std.process.arg_count())
println(std.process.arg(0))
println(std.process.args())
println(std.process.args_after(1))
extern fn args() -> [string]#

Returns the process arguments.

fn args_after(index: int) -> [string]#

Returns process arguments after the supplied index.

  • index: Zero-based position to start after.
extern fn program_name() -> string#

Returns the invoked program name.