Skip to main content

arg0_dispatch_or_else

Function arg0_dispatch_or_else 

Source
pub fn arg0_dispatch_or_else<F, Fut>(main_fn: F) -> Result<()>
where F: FnOnce(Option<PathBuf>) -> Fut, Fut: Future<Output = Result<()>>,
Expand description

While we want to deploy the Codex CLI as a single executable for simplicity, we also want to expose some of its functionality as distinct CLIs, so we use the “arg0 trick” to determine which CLI to dispatch. This effectively allows us to simulate deploying multiple executables as a single binary on Mac and Linux (but not Windows).

When the current executable is invoked through the hard-link or alias named codex-linux-sandbox we directly execute [codex_linux_sandbox::run_main] (which never returns). Otherwise we:

  1. Use dotenvy::from_path and dotenvy::dotenv to modify the environment before creating any threads.
  2. Construct a Tokio multi-thread runtime.
  3. Derive the path to the current executable (so children can re-invoke the sandbox) when running on Linux.
  4. Execute the provided async main_fn inside that runtime, forwarding any error. Note that main_fn receives codex_linux_sandbox_exe: Option<PathBuf>, as an argument, which is generally needed as part of constructing [codex_core::config::Config].

This function should be used to wrap any main() function in binary crates in this workspace that depends on these helper CLIs.