Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wasmtime): add error hint for missing WASI imports #10259

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/cli-flags/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
37 changes: 35 additions & 2 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 <wasi import>)
#[cfg(feature = "component-model")]
fn print_wasi_import_option_hint(
err_string: &str,
component: &wasmtime::component::Component,
store: &mut Store<Host>,
) {
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",
);
}
}
}
}
}