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

Add -S inherit-env for inheriting the entire environment #8168

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/cli-flags/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ wasmtime_option_group! {
pub udp: Option<bool>,
/// Allows imports from the `wasi_unstable` core wasm module.
pub preview0: Option<bool>,
/// Inherit all environment variables from the parent process.
///
/// This option can be further overwritten with `--env` flags.
pub inherit_env: Option<bool>,
}

enum Wasi {
Expand Down
10 changes: 10 additions & 0 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,11 @@ impl RunCommand {
let mut builder = WasiCtxBuilder::new();
builder.inherit_stdio().args(&self.compute_argv()?)?;

if self.run.common.wasi.inherit_env == Some(true) {
for (k, v) in std::env::vars() {
builder.env(&k, &v)?;
}
}
for (key, value) in self.vars.iter() {
let value = match value {
Some(value) => value.clone(),
Expand Down Expand Up @@ -779,6 +784,11 @@ impl RunCommand {
let mut builder = wasmtime_wasi::WasiCtxBuilder::new();
builder.inherit_stdio().args(&self.compute_argv()?);

if self.run.common.wasi.inherit_env == Some(true) {
for (k, v) in std::env::vars() {
builder.env(&k, &v);
}
}
for (key, value) in self.vars.iter() {
let value = match value {
Some(value) => value.clone(),
Expand Down
9 changes: 9 additions & 0 deletions tests/all/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,15 @@ fn specify_env() -> Result<()> {
.output()?;
assert!(output.status.success());

// Inherit all env vars
let output = get_wasmtime_command()?
.args(&["run", "-Sinherit-env", "tests/all/cli_tests/print_env.wat"])
.env("FOO", "bar")
.output()?;
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.contains("FOO=bar"), "bad output: {stdout}");

Ok(())
}

Expand Down