From 45b25b2fc62afc17ec7eae3dc10da070bbc36815 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 25 Feb 2025 07:42:04 -0800 Subject: [PATCH] Mention the filename when a module can't be opened This commit improves the error message of the `wasmtime` CLI when running a file that can't be opened. This can happen for example when an invalid subcommand is passed such as `wasmtime foo` by accident. --- src/common.rs | 3 ++- tests/all/cli_tests.rs | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/common.rs b/src/common.rs index fdcdef63d456..f9275e289a48 100644 --- a/src/common.rs +++ b/src/common.rs @@ -160,7 +160,8 @@ impl RunCommon { Some("-") => "/dev/stdin".as_ref(), _ => path, }; - let file = File::open(path)?; + let file = + File::open(path).with_context(|| format!("failed to open wasm module {path:?}"))?; // First attempt to load the module as an mmap. If this succeeds then // detection can be done with the contents of the mmap and if a diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rs index 1215809911dc..0f7ddaaf0454 100644 --- a/tests/all/cli_tests.rs +++ b/tests/all/cli_tests.rs @@ -2255,3 +2255,12 @@ fn config_cli_flag() -> Result<()> { Ok(()) } + +#[test] +fn invalid_subcommand() -> Result<()> { + let output = run_wasmtime_for_output(&["invalid-subcommand"], None)?; + dbg!(&output); + assert!(!output.status.success()); + assert!(String::from_utf8_lossy(&output.stderr).contains("invalid-subcommand")); + Ok(()) +}