HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulerio-0.1.22.0Haskell2010

RIO.Process

Interacting with external processes.

This module provides a layer on top of System.Process.Typed, with the following additions:

  • For efficiency, it will cache PATH lookups.

  • For convenience, you can set the working directory and env vars overrides in a RIO environment instead of on the individual calls to the process.

  • Built-in support for logging at the debug level.

In order to switch over to this API, the main idea is:

  • Like most of the rio library, you need to create an environment value (this time ProcessContext), and include it in your RIO environment. See mkProcessContext.

  • Instead of using the proc function from System.Process.Typed for creating a ProcessConfig, use the locally defined proc function, which will handle overriding environment variables, looking up paths, performing logging, etc.

Once you have your ProcessConfig, use the standard functions from System.Process.Typed (reexported here for convenient) for running the ProcessConfig.

  • 10 types
  • 1 class
  • 67 values
  • Packagerio-0.1.22.0
  • Exports78
  • LanguageHaskell2010
  • LicenceMIT
  • SourceProcess.hs

Process context

9 declarations

Lenses

Get the environment variables. We cannot provide a Lens here, since updating the environment variables requires an IO action to allocate a new IORef for holding the executable path cache.

Actions

Configuring

1 declaration
valueproc
  1. :: (HasProcessContext env, HasLogFunc env, MonadReader env m, MonadIO m, HasCallStack)
  2. => FilePath

    command to run

  3. -> [String]

    command line arguments

  4. -> (ProcessConfig () () () -> m a)
  5. -> m a
#

Provide a ProcessConfig based on the ProcessContext in scope. Deals with resolving the full path, setting the child process's environment variables, setting the working directory, and wrapping the call with withProcessTimeLog for debugging output.

This is intended to be analogous to the proc function provided by the System.Process.Typed module, but has a different type signature to (1) allow it to perform IO actions for looking up paths, and (2) allow logging and timing of the running action.

Spawning (run child process)

6 declarations

Exec (replacing current process)

2 declarations
valueexec
  1. :: (HasProcessContext env, HasLogFunc env)
  2. => String
  3. -> [String]
  4. -> RIO env b
#

Execute a process within the configured environment.

Execution will not return, because either:

1) On non-windows, execution is taken over by execv of the sub-process. This allows signals to be propagated (#527)

2) On windows, an ExitCode exception will be thrown.

Environment helper

2 declarations

Exceptions

1 declaration

Utilities

7 declarations
valuefindExecutable
  1. :: (MonadIO m, MonadReader env m, HasProcessContext env)
  2. => String

    Name of executable

  3. -> m (Either ProcessException FilePath)

    Full path to that executable on success

#

Find the complete path for the given executable name.

On POSIX systems, filenames that match but are not exectuables are excluded.

On Windows systems, the executable names tried, in turn, are the supplied name (only if it has an extension) and that name extended by each of the exeExtensions. Also, this function may behave differently from findExecutable. The latter excludes as executables filenames without a .bat, .cmd, .com or .exe extension (case-insensitive).

valueexeExtensions :: (MonadIO m, MonadReader env m, HasProcessContext env) => m [String]
#

Get the filename extensions for executable files, including the dot (if any).

On POSIX systems, this is [""].

On Windows systems, the list is determined by the value of the PATHEXT environment variable, if it present in the environment. If the variable is absent, this is its default value on a Windows system. This function may, therefore, behave differently from exeExtension, which returns only ".exe".

Augment the given value (assumed to be that of an environment variable that lists paths, such as PATH; this is not checked) with the given extra paths. Those paths are prepended (as in: they take precedence).

Show a process arg including speechmarks when necessary. Just for debugging purposes, not functionally important.

Reexports

45 declarations
datadata ProcessConfig stdin stdout stderr
#

An abstract configuration for a process, which can then be launched into an actual running Process. Takes three type parameters, providing the types of standard input, standard output, and standard error, respectively.

There are three ways to construct a value of this type:

  • With the proc smart constructor, which takes a command name and a list of arguments.

  • With the shell smart constructor, which takes a shell string

  • With the IsString instance via OverloadedStrings. If you provide it a string with no spaces (e.g., "date"), it will treat it as a raw command with no arguments (e.g., proc "date" []). If it has spaces, it will use shell.

In all cases, the default for all three streams is to inherit the streams from the parent process. For other settings, see the setters below for default values.

Once you have a ProcessConfig you can launch a process from it using the functions in the section Launch a process.

Instances3Show, IsString, Display
  • Show (ProcessConfig stdin stdout stderr)Defined in typed-process-0.2.13.0 · System.Process.Typed.Internal
  • (stdin ~ (), stdout ~ (), stderr ~ ()) => IsString (ProcessConfig stdin stdout stderr)Defined in typed-process-0.2.13.0 · System.Process.Typed.Internal
  • Display (ProcessConfig a b c)Defined in rio-0.1.22.0 · RIO.Prelude.Display
datadata StreamSpec (streamType :: StreamType) a
#

A specification for how to create one of the three standard child streams, stdin, stdout and stderr. A StreamSpec can be thought of as containing

  1. A type safe version of StdStream from System.Process. This determines whether the stream should be inherited from the parent process, piped to or from a Handle, etc.

  2. A means of accessing the stream as a value of type a

  3. A cleanup action which will be run on the stream once the process terminates

To create a StreamSpec see the section Stream specs.

Instances2Functor, IsString
  • Functor (StreamSpec streamType)Defined in typed-process-0.2.13.0 · System.Process.Typed.Internal
  • (streamType ~ 'STInput, res ~ ()) => IsString (StreamSpec streamType res)Defined in typed-process-0.2.13.0 · System.Process.Typed.Internal

    This instance uses byteStringInput to convert a raw string into a stream of input for a child process.

datadata StreamType
#

Whether a stream is an input stream or output stream. Note that this is from the perspective of the child process, so that a child's standard input stream is an STInput, even though the parent process will be writing to it.

datadata Process stdin stdout stderr
#

A running process. The three type parameters provide the type of the standard input, standard output, and standard error streams.

To interact with a Process use the functions from the section Interact with a process.

Instances1Show
  • Show (Process stdin stdout stderr)Defined in typed-process-0.2.13.0 · System.Process.Typed
valuemkStreamSpec
  1. :: StdStream
  2. -> (ProcessConfig () () () -> Maybe Handle -> IO (a, IO ()))
  3. -> StreamSpec streamType a
#

Create a new StreamSpec from the given StdStream and a helper function. This function:

  • Takes as input the raw Maybe Handle returned by the createProcess function. The handle will be Just Handle if the StdStream argument is CreatePipe and Nothing otherwise. See createProcess for more details.

  • Returns the actual stream value a, as well as a cleanup function to be run when calling stopProcess.

If making a StreamSpec with CreatePipe, prefer mkPipeStreamSpec, which encodes the invariant that a Handle is created.

valueinherit :: StreamSpec anyStreamType ()
#

A stream spec which simply inherits the stream of the parent process.

valueclosed :: StreamSpec anyStreamType ()
#

A stream spec which will close the stream for the child process. You usually do not want to use this, as it will leave the corresponding file descriptor unassigned and hence available for re-use in the child process. Prefer nullStream unless you're certain you want this behavior.

Capture the output of a process in a ByteString.

This function will fork a separate thread to consume all input from the process, and will only make the results available when the underlying Handle is closed. As this is provided as an STM action, you can either check if the result is available, or block until it's ready.

In the event of any exception occurring when reading from the Handle, the STM action will throw a ByteStringOutputException.

valueuseHandleOpen :: Handle -> StreamSpec anyStreamType ()
#

Use the provided Handle for the child process, and when the process exits, do not close it. This is useful if, for example, you want to have multiple processes write to the same log file sequentially.

valuestopProcess :: MonadIO m => Process stdin stdout stderr -> m ()
#

Close a process and release any resources acquired. This will ensure terminateProcess is called, wait for the process to actually exit, and then close out resources allocated for the streams. In the event of any cleanup exceptions being thrown this will throw an exception.

valuecheckExitCode :: MonadIO m => Process stdin stdout stderr -> m ()
#

Wait for a process to exit, and ensure that it exited successfully. If not, throws an ExitCodeException.

Exceptions thrown by this function will not include stdout or stderr (This prevents unbounded memory usage from reading them into memory). However, some callers such as readProcess_ catch the exception, add the stdout and stderr, and rethrow.

valuegetStdin :: Process stdin stdout stderr -> stdin
#

Get the child's standard input stream value.

valuegetStdout :: Process stdin stdout stderr -> stdout
#

Get the child's standard output stream value.

valuegetStderr :: Process stdin stdout stderr -> stderr
#

Get the child's standard error stream value.

datadata ExitCodeException
#

Exception thrown by checkExitCode in the event of a non-success exit code. Note that checkExitCode is called by other functions as well, like runProcess_ or readProcess_.

Note that several functions that throw an ExitCodeException intentionally do not populate eceStdout or eceStderr. This prevents unbounded memory usage for large stdout and stderrs.

Functions which do include eceStdout or eceStderr (like readProcess_) state so in their documentation.

Instances2Show, Exception
valueunsafeProcessHandle :: Process stdin stdout stderr -> ProcessHandle
#

Take ProcessHandle out of the Process. This method is needed in cases one need to use low level functions from the process package. Use cases for this method are:

  1. Send a special signal to the process.

  2. Terminate the process group instead of terminating single process.

  3. Use platform specific API on the underlying process.

This method is considered unsafe because the actions it performs on the underlying process may overlap with the functionality that typed-process provides. For example the user should not call waitForProcess on the process handle as either waitForProcess or stopProcess will lock. Additionally, even if process was terminated by the terminateProcess or by sending signal, stopProcess should be called either way in order to cleanup resources allocated by the typed-process.