Stream: wit-bindgen

Topic: ✔ Make and run a wit-bindgen component in Rust


view this post on Zulip Karel Hrkal (kajacx) (Jun 27 2023 at 15:16):

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.

Various projects related to WASM, each in it's own branch, so that I don't have 50 repositories. - GitHub - kajacx/wasm-playground at version-1-does-not-match-3

view this post on Zulip Robin Brown (Jun 27 2023 at 15:27):

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-macrocrate which is very nice if a bit under-documented at the moment.

view this post on Zulip Robin Brown (Jun 27 2023 at 15:29):

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

Contribute to Kylebrown9/template-compiler development by creating an account on GitHub.

view this post on Zulip Karel Hrkal (kajacx) (Jun 27 2023 at 16:36):

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.

A Cargo subcommand for creating WebAssembly components based on the component model proposal. - GitHub - bytecodealliance/cargo-component: A Cargo subcommand for creating WebAssembly components bas...

view this post on Zulip Robin Brown (Jun 27 2023 at 16:44):

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.

view this post on Zulip Gordon Smith (Jun 27 2023 at 17:02):

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?

view this post on Zulip Robin Brown (Jun 27 2023 at 17:07):

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.

view this post on Zulip Joel Dice (Jun 27 2023 at 17:10):

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.

view this post on Zulip Christof Petig (Jun 27 2023 at 17:24):

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.

This repository demonstrate how to use component model with wasmtime. - GitHub - cpetig/component-model-demo: This repository demonstrate how to use component model with wasmtime.

view this post on Zulip Christof Petig (Jun 27 2023 at 17:35):

I just updated this example to work with this week's wasmtime version

view this post on Zulip Robin Brown (Jun 27 2023 at 17:40):

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.

view this post on Zulip Karel Hrkal (kajacx) (Jun 27 2023 at 18:50):

Thanks everyone, I will have a look at it tomorrow.

view this post on Zulip Christoph Brewing (Jun 28 2023 at 07:53):

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?

view this post on Zulip Karel Hrkal (kajacx) (Jun 28 2023 at 11:12):

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.

view this post on Zulip Karel Hrkal (kajacx) (Jun 28 2023 at 11:18):

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.

Contribute to Kylebrown9/template-compiler development by creating an account on GitHub.
This repository demonstrate how to use component model with wasmtime. - GitHub - cpetig/component-model-demo: This repository demonstrate how to use component model with wasmtime.

view this post on Zulip Karel Hrkal (kajacx) (Jun 28 2023 at 13:41):

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.

A Cargo subcommand for creating WebAssembly components based on the component model proposal. - GitHub - bytecodealliance/cargo-component: A Cargo subcommand for creating WebAssembly components bas...

view this post on Zulip Robin Brown (Jun 28 2023 at 14:17):

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.

view this post on Zulip Robin Brown (Jun 28 2023 at 14:18):

protoc, the protobuf compiler is an installation dependency as stated in the requirements. If you install it, that error should go away.

A Cargo subcommand for creating WebAssembly components based on the component model proposal. - GitHub - bytecodealliance/cargo-component: A Cargo subcommand for creating WebAssembly components bas...

view this post on Zulip Robin Brown (Jun 28 2023 at 15:44):

We're also looking into ways to not depend on users installingprotoc which look promising.

view this post on Zulip Notification Bot (Jul 01 2023 at 08:14):

Karel Hrkal has marked this topic as resolved.


Last updated: Dec 23 2024 at 12:05 UTC