Skip to content

Commit dc18d1c

Browse files
authored
Merge pull request #10 from topheman/feat/handle-invalid-wit-based-wasm
feat: handle invalid WIT-based WASM plugins with helpful error messages
2 parents 6cc002c + 3915848 commit dc18d1c

File tree

4 files changed

+99
-10
lines changed

4 files changed

+99
-10
lines changed

crates/pluginlab/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,34 @@ pluginlab\
143143
--allow-all
144144
</pre>
145145
</summary>
146+
147+
<p></p>
148+
149+
If you pass to the `pluginlab` cli a valid plugin but with an incompatible version (cli and plugin not sharing the same `wit` files which defines the interfaces how they interact), you will get an error like the following, which tells you the problem and offers you two solutions:
150+
151+
- use a version of the plugin compatible with the version of the `pluginlab` cli you are using (giving you a link to the releases)
152+
- use the latest version of the `pluginlab` cli (by running `cargo install pluginlab`)
153+
154+
```
155+
pluginlab\
156+
--repl-logic https://topheman.github.io/webassembly-component-model-experiments/plugins/repl_logic_guest.wasm\
157+
--plugins https://topheman.github.io/webassembly-component-model-experiments/plugins/plugin_echo.wasm
158+
[Host] Starting REPL host...
159+
[Host] Loading REPL logic from: https://topheman.github.io/webassembly-component-model-experiments/plugins/repl_logic_guest.wasm
160+
[Host] Loading plugin: https://topheman.github.io/webassembly-component-model-experiments/plugins/plugin_echo.wasm
161+
[Host]
162+
[Host] Error: Failed instanciating https://topheman.github.io/webassembly-component-model-experiments/plugins/plugin_echo.wasm
163+
[Host] You are most likely trying to use a plugin not compatible with pluginlab@0.4.1
164+
[Host]
165+
[Host] Try using a compatible version of the plugin by passing the following flag:
166+
[Host] --plugins https://github.com/topheman/webassembly-component-model-experiments/releases/download/pluginlab@0.4.1/plugin_echo.wasm
167+
[Host]
168+
[Host] If it doesn't work, make sure to use the latest version of pluginlab: `cargo install pluginlab`
169+
[Host]
170+
[Host] Original error:
171+
Error: failed to convert function to given type
172+
173+
Caused by:
174+
0: type mismatch with results
175+
1: expected record of 3 fields, found 4 fields
176+
```

crates/pluginlab/src/wasm_host.rs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,36 @@ impl WasmHost {
3131

3232
pub async fn load_plugin(&mut self, engine: &WasmEngine, source: &str) -> Result<()> {
3333
let component = engine.load_component(source).await?;
34-
let plugin = engine
35-
.instantiate_plugin(&mut self.store, component)
36-
.await?;
37-
38-
// Get the plugin name from the plugin itself
39-
let plugin_name = plugin.repl_api_plugin().call_name(&mut self.store).await?;
40-
41-
self.plugins.insert(plugin_name, PluginInstance { plugin });
42-
43-
Ok(())
34+
match engine.instantiate_plugin(&mut self.store, component).await {
35+
Ok(plugin) => {
36+
// Get the plugin name from the plugin itself
37+
let plugin_name = plugin.repl_api_plugin().call_name(&mut self.store).await?;
38+
self.plugins.insert(plugin_name, PluginInstance { plugin });
39+
return Ok(());
40+
}
41+
Err(e) => {
42+
if e.to_string()
43+
.contains("failed to convert function to given type")
44+
{
45+
let plugin_filename = source.split("/").last().unwrap();
46+
let crate_version = env!("CARGO_PKG_VERSION");
47+
eprintln!("[Host]");
48+
eprintln!("[Host] Error: Failed instanciating {}", source);
49+
eprintln!(
50+
"[Host] You are most likely trying to use a plugin not compatible with pluginlab@{}",
51+
crate_version
52+
);
53+
eprintln!("[Host]");
54+
eprintln!("[Host] Try using a compatible version of the plugin by passing the following flag:");
55+
eprintln!("[Host] --plugins https://github.com/topheman/webassembly-component-model-experiments/releases/download/pluginlab@{}/{}", crate_version, plugin_filename);
56+
eprintln!("[Host]");
57+
eprintln!("[Host] If it doesn't work, make sure to use the latest version of pluginlab: `cargo install pluginlab`");
58+
eprintln!("[Host]");
59+
eprintln!("[Host] Original error:");
60+
}
61+
return Err(e);
62+
}
63+
}
4464
}
4565

4666
pub async fn load_repl_logic(&mut self, engine: &WasmEngine, source: &str) -> Result<()> {

crates/pluginlab/tests/e2e_cli_host.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,42 @@ mod e2e_cli_host {
8888
.exp_string("ls: : Operation not permitted")
8989
.expect("Didn't get expected error output");
9090
}
91+
92+
#[test]
93+
fn test_with_wrong_version_of_plugin() {
94+
let crate_version = env!("CARGO_PKG_VERSION");
95+
let project_root = find_project_root();
96+
println!("Setting current directory to: {:?}", project_root);
97+
std::env::set_current_dir(&project_root).unwrap();
98+
let mut session = spawn(
99+
&build_command(
100+
&["fixtures/valid-plugin-with-invalid-wit.wasm"],
101+
"repl_logic_guest.wasm",
102+
),
103+
Some(TEST_TIMEOUT),
104+
)
105+
.expect("Can't launch pluginlab with plugin greet");
106+
107+
session
108+
.exp_string("[Host] Starting REPL host...")
109+
.expect("Didn't see startup message");
110+
session
111+
.exp_string("[Host] Loading plugin:")
112+
.expect("Didn't see plugin loading message");
113+
session
114+
.exp_string(format!(
115+
"[Host] Error: Failed instanciating fixtures/valid-plugin-with-invalid-wit.wasm\r
116+
[Host] You are most likely trying to use a plugin not compatible with pluginlab@{}\r
117+
[Host]\r
118+
[Host] Try using a compatible version of the plugin by passing the following flag:\r
119+
[Host] --plugins https://github.com/topheman/webassembly-component-model-experiments/releases/download/pluginlab@{}/valid-plugin-with-invalid-wit.wasm\r
120+
[Host]\r
121+
[Host] If it doesn't work, make sure to use the latest version of pluginlab: `cargo install pluginlab`\r
122+
[Host]\r
123+
[Host] Original error:",
124+
crate_version,
125+
crate_version,
126+
).as_str())
127+
.expect("Didn't see error output");
128+
}
91129
}
1.98 MB
Binary file not shown.

0 commit comments

Comments
 (0)