Stream: wasi

Topic: wasmtime serve + wasi-http example


view this post on Zulip Andrew Brown (Jan 29 2024 at 20:14):

I was trying to use wasmtime serve today but ran into dependency problems: what is the set of versions and build options that will allow that to work?

I am currently (1) retrieving WIT dependencies with wit-dep, (2) building a simplistic (and raw) HTTP server by exporting ""wasi:http/incoming-handler" with wit-bindgen, (3) putting the pieces together using wasm-tools component new (including the preview1 adapter) and (4) attempting to run this with wasmtime serve. I invariably get errors like "import wasi:cli/environment@0.2.0-rc-2024-01-16 has the wrong type`."

I suspect that what is going on is that the preview1 adapter is bringing in unnecessary dependencies (unnecessary to my test program, I mean). I'm not sure why they would have the wrong type, seeing as I'm using Wasmtime on main.

view this post on Zulip Andrew Brown (Jan 29 2024 at 20:15):

I have looked at @Brooks Townsend's guide (here) for how to do this with the wash CLI but I wanted to understand all the pieces in raw form. Is there no working example for wasmtime serve somewhere?

With the release of WASI Preview 2, we take a step back and look at why it matters for wasmCloud and the WebAssembly ecosystem.

view this post on Zulip Alex Crichton (Jan 29 2024 at 20:15):

This is likely related to the adapter you're using. If you're using *.reactor.wasm you'll need to pass -S common to enable access to APIs not in the default proxy world. Otherwise you'll need to use *.proxy.wasm to not need -S common

view this post on Zulip Andrew Brown (Jan 29 2024 at 20:17):

I only see wasi_snapshot_preview1.wasm when I build the adapter with cargo build -p wasi-preview1-component-adapter --target wasm32-unknown-unknown --release

view this post on Zulip Alex Crichton (Jan 29 2024 at 20:18):

ah if building from source you'll need to frob cargo features

view this post on Zulip Andrew Brown (Jan 29 2024 at 20:18):

ok, let's try that

view this post on Zulip Alex Crichton (Jan 29 2024 at 20:18):

I think you'll need --no-default-features --features proxy

view this post on Zulip Andrew Brown (Jan 29 2024 at 20:23):

Ok, that works:

# In Wasmtime:
$ cargo build -p wasi-preview1-component-adapter --target wasm32-unknown-unknown --release --no-default-features --features proxy
$ ll -h target/wasm32-unknown-unknown/release/wasi_snapshot_preview1.wasm
-rwxr-xr-x. 2 abrown abrown 28K Jan 29 12:19 target/wasm32-unknown-unknown/release/wasi_snapshot_preview1.wasm

# In example project:
$ wasm-tools component new ./target/wasm32-wasi/debug/test_wasi_http.wasm \
        -o target/wasm32-wasi/debug/test-component.wasm \
        --adapt ../wasmtime/target/wasm32-unknown-unknown/release/wasi_snapshot_preview1.wasm
$ wasm-tools component wit target/wasm32-wasi/debug/test-component.wasm
package root:component;

world root {
  import wasi:io/error@0.2.0;
  import wasi:io/streams@0.2.0;
  import wasi:http/types@0.2.0;
  import wasi:cli/stdout@0.2.0;
  import wasi:cli/stderr@0.2.0;
  import wasi:cli/stdin@0.2.0;

  export wasi:http/incoming-handler@0.2.0;
}

That seems like the right number and kind of imports; way fewer than I was seeing earlier. And just a note: I don't see any special *.proxy.wasm or *.reactor.wasm in the adapter.

view this post on Zulip Alex Crichton (Jan 29 2024 at 20:25):

When you say "in the adapter", can you clarify? I should clarify when I say *.proxy.wasm I mean from Wasmtime's releases page where we manually rename a few things

view this post on Zulip Andrew Brown (Jan 29 2024 at 20:25):

And wasmtime serve now works:

$ wasmtime serve target/wasm32-wasi/debug/test-component.wasm
Serving HTTP on http://0.0.0.0:8080/

view this post on Zulip Alex Crichton (Jan 29 2024 at 20:25):

Note that -Scommon isn't necessary with the proxy version of the adapter

view this post on Zulip Andrew Brown (Jan 29 2024 at 20:26):

[edited to remove that part]

view this post on Zulip Andrew Brown (Jan 29 2024 at 20:26):

Alex Crichton said:

When you say "in the adapter", can you clarify? I should clarify when I say *.proxy.wasm I mean from Wasmtime's releases page where we manually rename a few things

I mean that when I build the adapter from source, I do not see any special file name suffixes. I didn't know to look in the release artifacts?

view this post on Zulip Alex Crichton (Jan 29 2024 at 20:28):

Oh you're correct that there's no suffix when building from source

view this post on Zulip Andrew Brown (Jan 29 2024 at 20:28):

I looked around in the Wasmtime repository for a CLI test of some kind that does all of this: did I miss it or is that just not there yet?

view this post on Zulip Alex Crichton (Jan 29 2024 at 20:28):

And by artifacts I mean at the top level of this page, not within tarballs themselves

17.0.0 Released 2024-01-25 The major feature of this release is that the WASI support in Wasmtime is now considered stable and flagged at an 0.2.0 version approved by the WASI subgroup. The release...

view this post on Zulip Alex Crichton (Jan 29 2024 at 20:29):

This is the closest equivalent to in-repo tests


Last updated: Nov 22 2024 at 16:03 UTC