Skip to content

Commit

Permalink
Fix passing empty arguments on the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Oct 9, 2023
1 parent c1b4b54 commit 5517074
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
8 changes: 1 addition & 7 deletions crates/test-programs/src/bin/cli_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ fn main() {
let args = std::env::args().skip(1).collect::<Vec<_>>();
assert_eq!(
args,
[
"hello",
"this",
// "", // TODO: this should be here.
"is an argument",
"with 🚩 emoji"
]
["hello", "this", "", "is an argument", "with 🚩 emoji"]
);
}
13 changes: 8 additions & 5 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use crate::common::{Profile, RunCommon, RunTarget};

use anyhow::{anyhow, bail, Context as _, Error, Result};
use clap::Parser;
use std::ffi::OsString;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::thread;
use wasmtime::{
Expand Down Expand Up @@ -102,7 +103,7 @@ pub struct RunCommand {
/// arguments unless the `--invoke` CLI argument is passed in which case
/// arguments will be interpreted as arguments to the function specified.
#[clap(value_name = "WASM", trailing_var_arg = true, required = true)]
module_and_args: Vec<PathBuf>,
module_and_args: Vec<OsString>,
}

enum CliLinker {
Expand Down Expand Up @@ -135,7 +136,9 @@ impl RunCommand {
let engine = Engine::new(&config)?;

// Read the wasm module binary either as `*.wat` or a raw binary.
let main = self.run.load_module(&engine, &self.module_and_args[0])?;
let main = self
.run
.load_module(&engine, self.module_and_args[0].as_ref())?;

// Validate coredump-on-trap argument
if let Some(path) = &self.run.common.debug.coredump {
Expand Down Expand Up @@ -212,7 +215,7 @@ impl RunCommand {
.with_context(|| {
format!(
"failed to run main module `{}`",
self.module_and_args[0].display()
self.module_and_args[0].to_string_lossy()
)
}) {
Ok(()) => (),
Expand Down Expand Up @@ -262,7 +265,7 @@ 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 {
arg.components().next_back().unwrap().as_os_str()
Path::new(arg).components().next_back().unwrap().as_os_str()
} else {
arg.as_ref()
};
Expand Down
2 changes: 1 addition & 1 deletion tests/all/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ mod test_programs {
CLI_ARGS_COMPONENT,
"hello",
"this",
// "", // TODO: this should work
"",
"is an argument",
"with 🚩 emoji",
])?;
Expand Down

0 comments on commit 5517074

Please sign in to comment.