Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement initial-cwd for wasi:cli/environment #9831

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/cli-flags/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ wasmtime_option_group! {
pub config_var: Vec<KeyValuePair>,
/// Preset data for the In-Memory provider of WASI key-value API.
pub keyvalue_in_memory_data: Vec<KeyValuePair>,
/// Set the working directory to the path provided.
pub cwd: Option<String>,
}

enum Wasi {
Expand Down
12 changes: 12 additions & 0 deletions crates/wasi/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
/// ```
Expand All @@ -53,6 +54,7 @@ pub struct WasiCtxBuilder {
monotonic_clock: Box<dyn HostMonotonicClock + Send>,
allowed_network_uses: AllowedNetworkUses,
allow_blocking_current_thread: bool,
cwd: Option<String>,
built: bool,
}

Expand Down Expand Up @@ -102,6 +104,7 @@ impl WasiCtxBuilder {
monotonic_clock: monotonic_clock(),
allowed_network_uses: AllowedNetworkUses::default(),
allow_blocking_current_thread: false,
cwd: None,
built: false,
}
}
Expand Down Expand Up @@ -453,6 +456,12 @@ impl WasiCtxBuilder {
self
}

/// Sets the current working directory
pub fn cwd(&mut self, cwd: impl AsRef<str>) -> &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
Expand Down Expand Up @@ -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;
Expand All @@ -500,6 +510,7 @@ impl WasiCtxBuilder {
monotonic_clock,
allowed_network_uses,
allow_blocking_current_thread,
cwd,
}
}

Expand Down Expand Up @@ -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<String>,
}

impl WasiCtx {
Expand Down
3 changes: 1 addition & 2 deletions crates/wasi/src/host/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ where
Ok(self.ctx().args.clone())
}
fn initial_cwd(&mut self) -> anyhow::Result<Option<String>> {
// FIXME: expose cwd in builder and save in ctx
Ok(None)
Ok(self.ctx().cwd.clone())
}
}
Loading