Skip to content

Commit 51d8d52

Browse files
feat(wasmtime): add error hint for missing WASI imports
This commit adds some code to print an error hint when a failed `wasmtime run` command could have been called with arguments (in particular `-S <capability>`) that would have enabled the command to succeed. Signed-off-by: Victor Adossi <vadossi@cosmonic.com>
1 parent 0b4c754 commit 51d8d52

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

crates/cli-flags/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,18 @@ wasmtime_option_group! {
457457
}
458458
}
459459

460+
impl WasiOptions {
461+
/// Get the name of a relevant WASI option for a given import
462+
pub fn option_for_import(import_name: &str) -> Option<&str> {
463+
// TODO: do we have common import parsing machinery?
464+
if import_name.contains("wasi:http") {
465+
return Some("http");
466+
}
467+
// TODO: fill out
468+
return None;
469+
}
470+
}
471+
460472
#[derive(Debug, Clone, PartialEq)]
461473
pub struct WasiNnGraph {
462474
pub format: String,

src/commands/run.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,12 +472,20 @@ impl RunCommand {
472472

473473
let component = module.unwrap_component();
474474

475-
let command = wasmtime_wasi::bindings::Command::instantiate_async(
475+
let command = match wasmtime_wasi::bindings::Command::instantiate_async(
476476
&mut *store,
477477
component,
478478
linker,
479479
)
480-
.await?;
480+
.await
481+
{
482+
Ok(c) => c,
483+
Err(e) => {
484+
print_wasi_import_option_hint(&e.to_string(), component, store);
485+
bail!(e);
486+
}
487+
};
488+
481489
let result = command
482490
.wasi_cli_run()
483491
.call_run(&mut *store)
@@ -1026,3 +1034,28 @@ fn write_core_dump(
10261034
.with_context(|| format!("failed to write core dump file at `{path}`"))?;
10271035
Ok(())
10281036
}
1037+
1038+
/// Print a hint about wasi import options for a given error
1039+
/// message, only if it contains an error that is likely to refer
1040+
/// to a missing option (i.e. -S <wasi import>)
1041+
#[cfg(feature = "component-model")]
1042+
fn print_wasi_import_option_hint(
1043+
err_string: &str,
1044+
component: &wasmtime::component::Component,
1045+
store: &mut Store<Host>,
1046+
) {
1047+
if err_string.contains("matching implementation was not found in the linker") {
1048+
// Check if we're missing some imports that could have been provided by
1049+
for (name, _) in component.component_type().imports(store.engine()) {
1050+
if err_string.contains(&name) {
1051+
if let Some(option) = wasmtime_cli_flags::WasiOptions::option_for_import(&name) {
1052+
eprintln!(
1053+
"warning: missing import [{name}] has a WASI import option [{option}] \
1054+
that has not been enabled -- consider re-running with '-S {option}'.\n\
1055+
Run `wasmtime -S help` for a full list of WASI import options.\n",
1056+
);
1057+
}
1058+
}
1059+
}
1060+
}
1061+
}

0 commit comments

Comments
 (0)