Skip to content

Commit

Permalink
Add support for wasmtime run --argv0 NAME ... (bytecodealliance#8961)
Browse files Browse the repository at this point in the history
This commit adds support for a new `--argv0` flag to the `wasmtime run`
CLI (and only `wasmtime run`, not other subcommands). This flag is used
to override the inferred value of the first argument which is sometimes
used to dispatch on various pieces of functionality. This enables
avoiding the need to copy around wasm files.

Closes bytecodealliance#8955
  • Loading branch information
alexcrichton authored Jul 15, 2024
1 parent de29ce3 commit af95475
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions crates/test-programs/src/bin/cli_argv0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
let mut args = std::env::args();
assert_eq!(args.next(), args.next());
}
14 changes: 13 additions & 1 deletion src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ pub struct RunCommand {
)]
pub preloads: Vec<(String, PathBuf)>,

/// Override the value of `argv[0]`, typically the name of the executable of
/// the application being run.
///
/// This can be useful to pass in situations where a CLI tool is being
/// executed that dispatches its functionality on the value of `argv[0]`
/// without needing to rename the original wasm binary.
#[arg(long)]
pub argv0: Option<String>,

/// The WebAssembly module to run and arguments to pass to it.
///
/// Arguments passed to the wasm module will be configured as WASI CLI
Expand Down Expand Up @@ -233,7 +242,10 @@ impl RunCommand {
// For argv[0], which is the program name. Only include the base
// name of the main wasm module, to avoid leaking path information.
let arg = if i == 0 {
Path::new(arg).components().next_back().unwrap().as_os_str()
match &self.argv0 {
Some(s) => s.as_ref(),
None => Path::new(arg).components().next_back().unwrap().as_os_str(),
}
} else {
arg.as_ref()
};
Expand Down
8 changes: 8 additions & 0 deletions tests/all/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1845,6 +1845,14 @@ stderr [1] :: after empty

Ok(())
}

#[test]
fn cli_argv0() -> Result<()> {
run_wasmtime(&["run", "--argv0=a", CLI_ARGV0, "a"])?;
run_wasmtime(&["run", "--argv0=b", CLI_ARGV0_COMPONENT, "b"])?;
run_wasmtime(&["run", "--argv0=foo.wasm", CLI_ARGV0, "foo.wasm"])?;
Ok(())
}
}

#[test]
Expand Down

0 comments on commit af95475

Please sign in to comment.