Hello, are there any up-to-date tutorials on how to use wit-bindgen with Rust? If I understand correctly, wit-bindgen itself is "outdated" in favor of the component model, and the component model is using the .wit format for protocol definition.
I want to make a plugin in Rust, and then load and execute it also in Rust using Wasmtime. I tried this tutorial (see repo here) but loading the module as a component gives this error:
Error: decoding custom section component-type:testwasi
Caused by:
component-type version 1 does not match supported version 3
The tutorial is using a file called "wasi_snapshot_preview1.wasm" to convert from a wit-bindgen wasm module to a component module, so I am assuming I need "wasi_snapshot_preview3.wasm" instead, but I have no idea where to find it.
Thanks for any help.
wit-bindgen isn't necessarily "outdated" because it's still in use. What is happening is that it's being used inside of other tools instead of being used directly more these days. For example, if you want to build a Rust component, you can use cargo-component
to get a better component authoring experience in Rust. When you're trying to use components from a language like Rust, you'll probably want to use the wasmtime-component-macro
crate which is very nice if a bit under-documented at the moment.
I've recently updated my template compiler project to the latest release and the tests include a couple minimal use cases of the component macro as a reference point: https://github.com/Kylebrown9/template-compiler/blob/main/tests/website.rs
Kyle Brown said:
I've recently updated my template compiler project to the latest release and the tests include a couple minimal use cases of the component macro as a reference point: https://github.com/Kylebrown9/template-compiler/blob/main/tests/website.rs
Thanks, it's always good to see a minimal, but full (including all dependencies) working example, but I don't think your template compiler is what I am looking for.
I want to have a protocol defined in a .wit file (or any other file, if need be), and implement that protocol in a Rust plugin, and then call that implementation from a Rust runtime.
The Cargo component command still seems to be using the "preview1" version of the coversion from WASM module to WASM component, so I don't think that would help me.
I guess I can try https://lib.rs/crates/wasmtime-component-macro and see if it generates a host that requires the "preview1" version, or I can try https://crates.io/crates/wasmtime-wit-bindgen to see if it will work without using the component model, which would honestly be better for me.
I wasn't suggesting you use the template compiler, just that the test I wrote for its output shows how to get and use bindings for a component in wasmtime.
Kyle Brown said:
I wasn't suggesting you use the template compiler, just that the test I wrote for its output shows how to get and use bindings for a component in wasmtime.
In your test you call "config.wasm_component_model(true);" - but when I look in the C API for 10.0.1 that method doesn't exist (and I am not even sure where it comes from) - is that a bug or is there a repo for the C-API that needs to be updated?
The function comes from here: https://docs.rs/wasmtime/latest/wasmtime/struct.Config.html#method.wasm_component_model
I can't speak to how it works in the C API, I haven't tested it.
Per https://bytecodealliance.zulipchat.com/#narrow/stream/327223-wit-bindgen/topic/understanding.20status.20of.20resources/near/369729391, component support has not yet been added to the Wasmtime C API.
Karel Hrkal said:
I want to have a protocol defined in a .wit file (or any other file, if need be), and implement that protocol in a Rust plugin, and then call that implementation from a Rust runtime.
Perhaps https://github.com/cpetig/component-model-demo fits that description.
I just updated this example to work with this week's wasmtime version
I'm not sure what exactly Cargo component isn't doing that you need, but if it has a problem you should file an issue and someone can take a look at it. The goal is for users not to need to write their own logic using ComponentEncoder
to make a component.
Thanks everyone, I will have a look at it tomorrow.
Hopefully, I do no hijack this post but let me try to resume this from my point of view:
Until recently, after having compiled for wasm32-unknown-unknown
when doing
wasm-tools component new ./app.wasm -o component.wasm --adapt ./wasi_snapshot_preview1.reactor.wasm
I got a new component. After having updated to wit-bindgen = "0.7.0"
, however, I get a
component-type version 3 does not match supported version 2
My assumption is that I have to use a different/updated adapter now. Is that assumption correct? If yes, what is the current adapter to use?
Christof Petig said:
Perhaps https://github.com/cpetig/component-model-demo fits that description.
Yes, that works. After I removed the "wasi_snapshot_preview1.wasm" adapter, it works using the wasm32-unknown-unknown target. Thanks.
Kyle Brown said:
I wasn't suggesting you use the template compiler, just that the test I wrote for its output shows how to get and use bindings for a component in wasmtime.
Hello, I took a look at your tests, it seems you are loading the component with a custom gen_component function that looks very scary.
Thankfully, I don't have to do that, both options described in the newer component model demo repo work fine. But thanks for the effort.
Kyle Brown said:
I'm not sure what exactly Cargo component isn't doing that you need, but if it has a problem you should file an issue and someone can take a look at it. The goal is for users not to need to write their own logic using
ComponentEncoder
to make a component.
I do not know how to install or use cargo component. Using the install script from this website gives as error: Could not find protoc
installation and this build crate cannot proceed without this knowledge.
"wasm-tools component" works fine, so I don't need to suffer through making cagro component work.
Feel free to ignore that example, but the gen_component
is just where I make my component binary. If you already have a component binary and are trying to just use it in Wasmtime then it's the lines after that matter.
protoc
, the protobuf compiler is an installation dependency as stated in the requirements. If you install it, that error should go away.
We're also looking into ways to not depend on users installingprotoc
which look promising.
Karel Hrkal has marked this topic as resolved.
Last updated: Dec 23 2024 at 12:05 UTC