diff --git a/crates/cli-flags/src/lib.rs b/crates/cli-flags/src/lib.rs index f819f8d544ca..a5be5058971e 100644 --- a/crates/cli-flags/src/lib.rs +++ b/crates/cli-flags/src/lib.rs @@ -424,6 +424,8 @@ wasmtime_option_group! { pub config_var: Vec, /// Preset data for the In-Memory provider of WASI key-value API. pub keyvalue_in_memory_data: Vec, + /// Set the working directory to the path provided. + pub cwd: Option, } enum Wasi { diff --git a/crates/wasi/src/ctx.rs b/crates/wasi/src/ctx.rs index 693876ea896a..1aa7bdedc877 100644 --- a/crates/wasi/src/ctx.rs +++ b/crates/wasi/src/ctx.rs @@ -33,6 +33,7 @@ use wasmtime::component::ResourceTable; /// wasi.arg("./foo.wasm"); /// wasi.arg("--help"); /// wasi.env("FOO", "bar"); +/// wasi.cwd("/home/steve/foo"); /// /// let wasi: WasiCtx = wasi.build(); /// ``` @@ -53,6 +54,7 @@ pub struct WasiCtxBuilder { monotonic_clock: Box, allowed_network_uses: AllowedNetworkUses, allow_blocking_current_thread: bool, + cwd: Option, built: bool, } @@ -102,6 +104,7 @@ impl WasiCtxBuilder { monotonic_clock: monotonic_clock(), allowed_network_uses: AllowedNetworkUses::default(), allow_blocking_current_thread: false, + cwd: None, built: false, } } @@ -453,6 +456,12 @@ impl WasiCtxBuilder { self } + /// Sets the current working directory + pub fn cwd(&mut self, cwd: impl AsRef) -> &mut Self { + self.cwd = Some(cwd.as_ref().to_owned()); + self + } + /// Uses the configured context so far to construct the final [`WasiCtx`]. /// /// Note that each `WasiCtxBuilder` can only be used to "build" once, and @@ -481,6 +490,7 @@ impl WasiCtxBuilder { monotonic_clock, allowed_network_uses, allow_blocking_current_thread, + cwd, built: _, } = mem::replace(self, Self::new()); self.built = true; @@ -500,6 +510,7 @@ impl WasiCtxBuilder { monotonic_clock, allowed_network_uses, allow_blocking_current_thread, + cwd, } } @@ -654,6 +665,7 @@ pub struct WasiCtx { pub(crate) socket_addr_check: SocketAddrCheck, pub(crate) allowed_network_uses: AllowedNetworkUses, pub(crate) allow_blocking_current_thread: bool, + pub(crate) cwd: Option, } impl WasiCtx { diff --git a/crates/wasi/src/host/env.rs b/crates/wasi/src/host/env.rs index db259d8caa14..c069e871e734 100644 --- a/crates/wasi/src/host/env.rs +++ b/crates/wasi/src/host/env.rs @@ -12,7 +12,6 @@ where Ok(self.ctx().args.clone()) } fn initial_cwd(&mut self) -> anyhow::Result> { - // FIXME: expose cwd in builder and save in ctx - Ok(None) + Ok(self.ctx().cwd.clone()) } }