diff --git a/crates/cli-flags/src/lib.rs b/crates/cli-flags/src/lib.rs index bafdf377d940..3437f507f41a 100644 --- a/crates/cli-flags/src/lib.rs +++ b/crates/cli-flags/src/lib.rs @@ -457,6 +457,18 @@ wasmtime_option_group! { } } +impl WasiOptions { + /// Get the name of a relevant WASI option for a given import + pub fn option_for_import(import_name: &str) -> Option<&str> { + // TODO: do we have common import parsing machinery? + if import_name.contains("wasi:http") { + return Some("http"); + } + // TODO: fill out + return None; + } +} + #[derive(Debug, Clone, PartialEq)] pub struct WasiNnGraph { pub format: String, diff --git a/src/commands/run.rs b/src/commands/run.rs index 7baa4a37f812..6e1c0f19789a 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -472,12 +472,20 @@ impl RunCommand { let component = module.unwrap_component(); - let command = wasmtime_wasi::bindings::Command::instantiate_async( + let command = match wasmtime_wasi::bindings::Command::instantiate_async( &mut *store, component, linker, ) - .await?; + .await + { + Ok(c) => c, + Err(e) => { + print_wasi_import_option_hint(&e.to_string(), component, store); + bail!(e); + } + }; + let result = command .wasi_cli_run() .call_run(&mut *store) @@ -1026,3 +1034,28 @@ fn write_core_dump( .with_context(|| format!("failed to write core dump file at `{path}`"))?; Ok(()) } + +/// Print a hint about wasi import options for a given error +/// message, only if it contains an error that is likely to refer +/// to a missing option (i.e. -S ) +#[cfg(feature = "component-model")] +fn print_wasi_import_option_hint( + err_string: &str, + component: &wasmtime::component::Component, + store: &mut Store, +) { + if err_string.contains("matching implementation was not found in the linker") { + // Check if we're missing some imports that could have been provided by + for (name, _) in component.component_type().imports(store.engine()) { + if err_string.contains(&name) { + if let Some(option) = wasmtime_cli_flags::WasiOptions::option_for_import(&name) { + eprintln!( + "warning: missing import [{name}] has a WASI import option [{option}] \ + that has not been enabled -- consider re-running with '-S {option}'.\n\ + Run `wasmtime -S help` for a full list of WASI import options.\n", + ); + } + } + } + } +}