Hi here, I just converted my wasm to a component, and I indeed see that in the wit that wasm-tools told be, it is now asking multiple imports. Like wasi-stderr, wasi-stdio...
And when trying to instantiate with wasmtime, well it panics because apparently, the WASM misses some imports.
https://github.com/bytecodealliance/wasmtime/issues/5830
This is a very similar problem to this issue, but I didn't understand the answer given by pchickey. How can I do the bindings? Thanks in advance
A host implementation of those imports is provided over in the preview2-prototyping repo under host/
it uses a fork of wasi-common, and its very incomplete and buggy
and its rapidly changing
Oh I see, so I cannot make the bindings for now? Am I forced to use this?
so, you need to depend on that crate, and use its add_to_linker
and WasiCommand
export like here: https://github.com/bytecodealliance/preview2-prototyping/blob/main/host/src/main.rs
but note that you need to depend on the previee2-prototyping wasi-common, not the crates.io wasi-common, so please use a git dependency
you can either use the implementation in prototyping's host, or you can write your own using wasmtime's wit bindgen. i dont recommend writing your own because its quite hard
but if you need to, it is possible
you'll have to follow the general pattern we used in that repo of invoking wasmtime::component::bindgen!() to do so, and then impl all of the traits.
Yup, I tried to pull your host project :
[dependencies]
.......
wasmtime = { version = "^6.0.0", features = ["component-model", "async"] }
host = { git = "https://github.com/bytecodealliance/preview2-prototyping" }
But when trying to build, I have a version conflict that I... don't understand o.o
wasmtime_host_test git:(main) ✗ cargo check
Updating crates.io index
Updating git repository `https://github.com/bytecodealliance/preview2-prototyping`
Updating git repository `https://github.com/bytecodealliance/wasmtime`
error: failed to select a version for `wasmtime-fiber`.
... required by package `wasmtime v6.0.0 (https://github.com/bytecodealliance/wasmtime?branch=release-6.0.0#c00d3f05)`
... which satisfies git dependency `wasmtime` of package `host v0.0.0 (https://github.com/bytecodealliance/preview2-prototyping#7736fd0c)`
... which satisfies git dependency `host` of package `wasmtime_host_test v0.1.0 (/home/yohann/devs/neo-michel/wasmtime_host_test)`
versions that meet the requirements `=6.0.0` are: 6.0.0
the package `wasmtime-fiber` links to the native library `wasmtime-fiber-shims`, but it conflicts with a previous package which links to `wasmtime-fiber-shims` as well:
package `wasmtime-fiber v6.0.0`
... which satisfies dependency `wasmtime-fiber = "=6.0.0"` of package `wasmtime v6.0.0`
... which satisfies dependency `wasmtime = "^6.0.0"` of package `wasmtime_host_test v0.1.0 (/home/yohann/devs/neo-michel/wasmtime_host_test)`
Only one package in the dependency graph may specify the same links value. This helps ensure that only one copy of a native library is linked in the final binary. Try to adjust your dependencies so that only one package uses the links ='wasmtime-fiber' value. For more information, see https://doc.rust-lang.org/cargo/reference/resolver.html#links.
failed to select a version for `wasmtime-fiber` which could resolve this conflict
thats crgo's way of showing you there are multiple versions of wasmtime pulled in
(not ideal)
You need to use
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", features = ["component-model"], branch = 'release-6.0.0' }
as that is what preview2-prototyping used too.
Yeah, but all I see are version 6.0.0, so there should be no problem haha
Thank to both of you, I'm gonna try this :)
git versions and crates.io versions are not compatible, even if they say the same string
we can update preview2-prototyping to point at the crates.io release, it landed this morning, but all of the people who hack on that are in a meeting today so we havent gotten to it yet
Okay, I understand better now :) And this is working like this! Thank you for your kind and quick answers and your hard work :)
bachrc has marked this topic as resolved.
bachrc has marked this topic as unresolved.
Well, I guess using the add_to_linker
is incompatible with the one given by my bindgen! macro? :(
let mut linker = Linker::new(&engine);
host::add_to_linker(&mut linker, |x| x)?;
Michel::add_to_linker(&mut linker, |state: &mut MichelApiForPlugins| state)?;
can you share the error message?
Here the compiler isn't happy because there is two types or states given to the linker : WasiCtx (by the host), and MichelApiForPlugins (which is my state)
error[E0631]: type mismatch in closure arguments
--> wasmtime_host_test/src/main.rs:34:5
|
34 | Michel::add_to_linker(&mut linker, |state: &mut MichelApiForPlugins| state)?;
| ^^^^^^^^^^^^^^^^^^^^^ --------------------------------- found signature defined here
| |
| expected due to this
|
= note: expected closure signature `for<'a> fn(&'a mut WasiCtx) -> _`
found closure signature `for<'a> fn(&'a mut MichelApiForPlugins) -> _`
right, so you want to make some struct Ctx { wasi: WasiCtx, michel: MichaelApiForPlugins }
, and in the linker you use |ctx| &mut ctx.wasi
for wasi, |ctx| &mut ctx.michel
for michel
thats how the linker is designed to deal with compositions of state
Oh okay I see! I hope my struggles will help people searching here :) Thanks again for your answer!
Yup, but making the Linker contain our new Ctx
is incompatible with the instantiate_async
generated by the bindgen!.
let (bindings,_) = Michel::instantiate_async(&mut store, &component, &linker).await?;
Here it waits for the same generic type for store and linker. Well I guess my Store should not be MichelPluginForApi
anymore, but Ctx
? But for creating a Ctx object, I need to create a WasiCtx. You know how I could do this? :(
Oh yes, the WasiCtxBuilder !
I'm trying this now.
yes
and your store will need to be a Store<Ctx>
Sorry to bother again, but trying to get the WasiCtsBuilder with the wasmtime-wasi dependency :
wasmtime-wasi = { git = "https://github.com/bytecodealliance/wasmtime", branch = 'release-6.0.0' }
But again, a version conflict. I don't see what's wrong here :(
stdout : Updating crates.io index
Updating git repository `https://github.com/bytecodealliance/wasmtime`
error: failed to select a version for `wasi-common`.
... required by package `wasmtime-wasi v6.0.0 (https://github.com/bytecodealliance/wasmtime?branch=release-6.0.0#c00d3f05)`
... which satisfies git dependency `wasmtime-wasi` of package `wasmtime_host_test v0.1.0 (/home/yohann/devs/neo-michel/wasmtime_host_test)`
versions that meet the requirements `=6.0.0` are: 6.0.0
the package `wasi-common` links to the native library `wasi-common-19`, but it conflicts with a previous package which links to `wasi-common-19` as well:
package `wasi-common v0.0.0 (https://github.com/bytecodealliance/preview2-prototyping#7736fd0c)`
... which satisfies git dependency `wasi-common` (locked to 0.0.0) of package `host v0.0.0 (https://github.com/bytecodealliance/preview2-prototyping#7736fd0c)`
... which satisfies git dependency `host` (locked to 0.0.0) of package `wasmtime_host_test v0.1.0 (/home/yohann/devs/neo-michel/wasmtime_host_test)`
Only one package in the dependency graph may specify the same links value. This helps ensure that only one copy of a native library is linked in the final binary. Try to adjust your dependencies so that only one package uses the links ='wasi-common' value. For more information, see https://doc.rust-lang.org/cargo/reference/resolver.html#links.
failed to select a version for `wasi-common` which could resolve this conflict
You can’t use wasi-common from the prototype repo and the one upstream in Wasmtime in the same project right now.
We will merge all that functionality back in upstream at some point, but right now they can’t coexist
If you delete the links section in the prototype cargo.toml it might work
Ah. So I can't use WasiCtxBuilder, and so using wit-bindgen
I can't modify the prototype Cargo.toml without doing a fork
And i don't see any links section in https://github.com/bytecodealliance/preview2-prototyping/blob/main/Cargo.toml
Well I see here (https://bytecodealliance.zulipchat.com/#narrow/stream/327223-wit-bindgen/topic/.5Brust.5D.20wasmtime.20.26.20custom.20wit.20type.20.28record.29) that i should maybe import wasi_cap_sync from the std!
Hey i managed to get it working by adding the following line to my cargo.toml of the host project:
wasi-cap-std-sync = { git = "https://github.com/bytecodealliance/preview2-prototyping", rev = "7736fd0" }
Then i was able to use the WasiCtxBuilder.
Yeah, to make this work, I was forced to clone locally the preview2-prototyping, and remove the [links] section.
bachrc has marked this topic as resolved.
Could you send me the example you used to get it working?
Last updated: Dec 23 2024 at 12:05 UTC