-
Notifications
You must be signed in to change notification settings - Fork 0
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
guest-async component build error #1
Comments
Hi @xdlin; thanks for reporting this. I've moved the contents of this repo into this PR as part of my effort to get everything upstreamed, so the code here (and the Meanwhile, my
|
Thank you for your quick response, I keep watching this PR as well :) I'm grateful for the info, I'll try with your PR later. Previously I build my project which support |
I guess this is a necessary step for 'wasm-wasip1'? sync WIT 1st tryI first tried with sync code, and with
I got this error:
sync WIT with adapter
After downloading the adapter and ran with this command, the sync code compiles async WITWhen I change the WIT to async mode:
with target
|
Hmm, it seems to working for me. Here's what I did: cargo install wasm-tools
mkdir -p foo/wit foo/guest/src foo/host/src
cd foo
cat >wit/round-trip.wit <<EOF
package local:local;
interface baz {
foo: func(s: string) -> string;
}
world round-trip {
import baz;
export baz;
}
EOF
cat >guest/Cargo.toml <<EOF
[package]
name = "guest"
version = "0.1.0"
edition = "2021"
[dependencies]
futures = "0.3.30"
once_cell = "1.19.0"
wit-bindgen = "0.37.0"
wit-bindgen-rt = "0.37.0"
[lib]
crate-type = ["cdylib"]
EOF
cat >guest/src/lib.rs <<EOF
mod bindings {
wit_bindgen::generate!({
path: "../wit",
world: "round-trip",
async: true,
});
use super::Component;
export!(Component);
}
use bindings::{exports::local::local::baz::Guest as Baz, local::local::baz};
struct Component;
impl Baz for Component {
async fn foo(s: String) -> String {
format!(
"{} - exited guest",
baz::foo(&format!("{s} - entered guest")).await
)
}
}
EOF
cargo build --manifest-path guest/Cargo.toml --release --target wasm32-wasip1
curl -OL https://github.com/bytecodealliance/wasmtime/releases/download/v28.0.1/wasi_snapshot_preview1.reactor.wasm
wasm-tools component new --adapt wasi_snapshot_preview1.reactor.wasm --skip-validation guest/target/wasm32-wasip1/release/guest.wasm -o guest.wasm
wasm-tools validate --features all guest.wasm
cat >host/Cargo.toml <<EOF
[package]
name = "host"
version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = { version = "1.0.81", features = ["backtrace"] }
tokio = { version = "1.36.0", features = ["fs", "macros", "rt-multi-thread", "time"] }
wasmtime = { git = "https://github.com/dicej/wasmtime", branch = "async", features = ["component-model-async"] }
wasmtime-wasi = { git = "https://github.com/dicej/wasmtime", branch = "async" }
EOF
cat >host/src/main.rs <<EOF
use {
anyhow::Result,
std::{env, future::Future, time::Duration},
tokio::fs,
wasmtime::{
component::{self, Component, Linker, PromisesUnordered, ResourceTable},
Config, Engine, Store, StoreContextMut,
},
wasmtime_wasi::{WasiCtx, WasiCtxBuilder, WasiView},
};
mod round_trip {
wasmtime::component::bindgen!({
trappable_imports: true,
path: "../wit",
world: "round-trip",
concurrent_imports: true,
concurrent_exports: true,
async: true,
});
}
struct Ctx {
wasi: WasiCtx,
table: ResourceTable,
}
impl WasiView for Ctx {
fn table(&mut self) -> &mut ResourceTable {
&mut self.table
}
fn ctx(&mut self) -> &mut WasiCtx {
&mut self.wasi
}
}
impl round_trip::local::local::baz::Host for Ctx {
type Data = Ctx;
#[allow(clippy::manual_async_fn)]
fn foo(
_: StoreContextMut<'_, Self>,
s: String,
) -> impl Future<
Output = impl FnOnce(StoreContextMut<'_, Self>) -> wasmtime::Result<String> + 'static,
> + Send
+ 'static {
async move {
tokio::time::sleep(Duration::from_millis(10)).await;
component::for_any(move |_: StoreContextMut<'_, Self>| {
Ok(format!("{s} - entered host - exited host"))
})
}
}
}
#[tokio::main]
async fn main() -> Result<()> {
let mut config = Config::new();
config.wasm_component_model(true);
config.wasm_component_model_async(true);
config.async_support(true);
let engine = Engine::new(&config)?;
let make_store = || {
Store::new(
&engine,
Ctx {
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
table: ResourceTable::default(),
},
)
};
let component = Component::new(&engine, &fs::read(env::args().nth(1).unwrap()).await?)?;
let mut linker = Linker::new(&engine);
wasmtime_wasi::add_to_linker_async(&mut linker)?;
round_trip::RoundTrip::add_to_linker(&mut linker, |ctx| ctx)?;
let mut store = make_store();
let round_trip =
round_trip::RoundTrip::instantiate_async(&mut store, &component, &linker).await?;
// Start three concurrent calls and then join them all:
let mut promises = PromisesUnordered::new();
for _ in 0..3 {
promises.push(
round_trip
.local_local_baz()
.call_foo(&mut store, "hello, world!".to_owned())
.await?,
);
}
while let Some(value) = promises.next(&mut store).await? {
println!("{value}");
}
Ok(())
}
EOF
cargo run --manifest-path host/Cargo.toml --release -- guest.wasm Does the above work for you? If so, maybe you can modify that to suit your needs. Otherwise, please let me know where it fails on your end. |
Wow, your version works~ And after comparing the code, I find the reason, I exported the function in
And the related guest code: mod bindings {
wit_bindgen::generate!({
path: "../wit",
world: "round-trip",
async: true,
});
use super::Component;
export!(Component);
}
use bindings::{Guest as Baz};
struct Component;
impl Baz for Component {
async fn foo(s: String) -> String {
format!(
"{} - exited guest",
s
)
}
} With this direct export function in |
Thanks for your help and your great work on this feature, I can continue my experiment without any blockers after changing the WIT syntax a little bit |
Ah interesting; I know I tested direct world exports in WAT, but maybe I'm missing a test for that which uses |
FYI, here's the fix for world-level exports: bytecodealliance/wasm-tools#1979 |
hi @dicej when I tried to integrate this feature into my own project, it was OK before, but with recent change, I got thie compile error:
I'm not sure whether this is the proper place to report this, if necessary, I can put this in another repo with full context. |
It looks like the definition of |
@xdlin we've shifted development over to the Also, please keep in mind that all of this code is very experimental and won't be supported until it's been merged into the |
@dicej Thank you so much for your response, I'm totally understand that this feature is in early stage and not production ready yet. While since I'm so eager for this feature, and would like to integrate that into my own project as soon as possible, so I'm happy to take the ABI breaking risk, and hopefully I can provide some valuable feedback |
Hi @dicej I'm very interested in the async component function, thanks for your great work! When I tried to build it locally, but I have this error:
I got this error:
I confirmed that I'm using the
wasm-tools
you provided here:cargo install --locked --git https://github.com/dicej/wasm-tools --branch async wasm-tools
. Is there anything I missed?The text was updated successfully, but these errors were encountered: