Run a bounded subprocess, inspect its termination and access invocation arguments.
Import
import stdlib/process as process
For an async function, select the async module instead:
import stdlib/process/async as process
Await effectful operations. In the signatures below, an ordinary effectful function becomes async under the async import; parameter and result types stay the same. Pure functions and value methods remain ordinary calls. The two imports share their value and error types.
run()
run(command: Command): Result[ProcessOutput,ProcessError]
Parameters
command: Command— executable, arguments, input, working directory, environment, timeout and combined output limit; all fields are required.
Returns: Result[ProcessOutput,ProcessError]. A nonzero child exit is still Ok(output).
Runs the exact executable path. Relative executable paths resolve against the child working directory. There is no PATH search or implicit shell. Arguments are passed separately and exclude argv[0]. The async form suspends while the process runs. Timeout or cancellation stops and reaps the process group under the host policy; descendants that leave that group are not a general containment guarantee. Timeout and output-limit failures return no partial output.
Errors: Invalid arguments/environment, launch failure, timeout and combined-output exhaustion have distinct categories.
arguments()
arguments(): [Str]
Parameters
None.
Returns: [Str], the arguments supplied to the current invocation.
Use the async import and await process.arguments() inside an async function.
exit()
exit(status: Nat): Unit
Parameters
status: Nat— requested exit status.
Returns: Declared Unit; normal execution does not continue after exiting.
Ends the current invocation with the requested status. Use the async form inside async code.
Command
record Command {
executable: Path, arguments: [Str], stdin: Bytes, working_directory: Path,
environment: ProcessEnvironment, timeout: Duration, output_limit: Nat
}
executableis the exact path to run.argumentsexcludes argv[0]; values must not contain NUL.stdinis the complete byte input, at most 16 MiB.working_directoryis the child working directory.environmentselects inherited or exact entries.timeoutmust be positive and at most 365 days.output_limitbounds stdout and stderr combined, at most 16 MiB. Arguments and environment lists each allow at most 4096 entries. Host argument-size limits may be smaller.
enum ProcessEnvironment { InheritEnvironment([Str]), ExactEnvironment([Str]) }
Entries are NAME=value strings with unique, nonempty names and no NUL. Empty values are allowed. InheritEnvironment overrides entries in the invocation environment; ExactEnvironment supplies only the listed entries. An exact environment does not sandbox the child.
ProcessOutput
record ProcessOutput { termination: ProcessTermination, stdout: Bytes, stderr: Bytes }
enum ProcessTermination { Exited(Nat), Signalled(Nat) }
Exited(code) identifies normal termination; check whether the code is zero. Signalled(signal) identifies signal termination. Output is bytes: use checked UTF-8 decoding when text is expected.
ProcessError
record ProcessError {
executable: Path, working_directory: Path, timeout: Duration, output_limit: Nat,
kind: ProcessErrorKind, native: HostError
}
The record preserves executable, directory and bounds, but excludes arguments, environment and input/output contents. Use problem.kind for control flow and render for a human-readable explanation. The native field preserves the original failure. Host-backed native errors contain operation: Str, code: Str and native_code: Nat; a zero native number means no host error number was supplied.
enum ProcessErrorKind {
InvalidTimeout, LimitExceeded, InvalidArgument, InvalidEnvironment, LaunchFailed,
TimedOut, OutputLimitExceeded, ClockUnavailable, ResourceLimit, Cancelled,
HostFailure, UnsupportedHost, OtherFailure
}
ProcessError.render()
pure render(): Str
Parameters
None.
Returns: Str, a human-readable explanation of this error, including the available operation and failure details.
Pure. Use the structured error fields for control flow; the wording is diagnostic text and must not be parsed.
Example
Run the complete bounded subprocess example.
Complete example: async pipe input
import stdlib/encoding/utf8 as utf8
import stdlib/path as paths
import stdlib/process/async as process
import stdlib/time/async as time
import stdlib/console/async as console
async copy_text(): Result[Str,Str] {
match paths.from_text("/bin/cat") {
Error(path_error) => Error("The executable path is invalid."),
Ok(executable) => {
command = process.Command(executable, [], "hello λ".encode_utf8(),
paths.current(), process.ExactEnvironment([]), time.seconds(5), 64)
match await process.run(command) {
Error(problem) => Error(problem.render()),
Ok(output) => match output.termination {
process.Signalled(signal) => Error("cat was terminated by signal ${signal}."),
process.Exited(status) => {
if status != 0 { Error("cat exited with status ${status}.") }
else { match utf8.decode(output.stdout) {
Error(decode_error) => Error("cat returned invalid UTF-8."), Ok(text) => Ok(text)
} }
}
}
}
}
}
}
async main(): Unit {
match await copy_text() {
Error(message) => {
reported = await console.write_error_line(message)
await process.exit(1)
},
Ok(text) => match await console.write_line(text) {
Error(write_error) => await process.exit(1), Ok(written) => ()
}
}
}Implementation: view source on GitHub.