Skip to content
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

Serve command behaves differently for jco and wasmtime / wasm produces by jco is not compatible with wasmtime #559

Open
mschuwalow opened this issue Jan 29, 2025 · 2 comments

Comments

@mschuwalow
Copy link

Using the example: https://github.com/bytecodealliance/jco/tree/e083d5a0699856bfa6b4eab2776ca04ada125138/examples/components/http-hello-world

With jco:

❯ jco serve out/js_default_wasi_http_comp.wasm

> serve
> jco serve out/js_default_wasi_http_comp.wasm

Server listening on 8000...

With wasm:

❯ wasmtime serve out/js_default_wasi_http_comp.wasm 
Error: component imports instance `wasi:cli/[email protected]`, but a matching implementation was not found in the linker

Caused by:
    0: instance export `terminal-input` has the wrong type
    1: resource implementation is missing

Exception: wasmtime exited with 1
  [tty 87]:1:1-50: wasmtime serve out/js_default_wasi_http_comp.wasm 
@guybedford
Copy link
Collaborator

Make sure you are using Jco 1.9.1 with the latest WASI support.

@vados-cosmonic
Copy link
Contributor

vados-cosmonic commented Feb 3, 2025

Hey @mschuwalow , I think the issue you're running into is that when you run wasmtime serve, you need to enable support for WASI interfaces that are used by the components that you're trying to run.

Short story

Like in the README, use -S common with wasmtime serve

$ wasmtime serve -S common http-hello-world.wasm
Serving HTTP on http://0.0.0.0:8080/

Long story

When jco builds a component, it includes some WASI interfaces that are accessible to JS code -- which the runtime needs to supply. For example, if fetch can be accessed (at any time!), then we'd have to include wasi:http/outgoing-handler as a matter of course.

It's possible to --disable some WASI interfaces, but you may not want to do that for the general case (and this is somewhat tricky because they're in use wasi:http/incoming-handler, for example wasi:io).

You can use wasm-tools to figure out what is required for any given component:

wasm-tools component wit path/to/your/component.wasm

If I run that for the http-hello-world component, there's a lot of output, but at the very top there is the root world, which is kind of like the fused requirements:

world root {
  import wasi:io/poll@0.2.2;
  import wasi:clocks/monotonic-clock@0.2.2;
  import wasi:io/error@0.2.2;
  import wasi:io/streams@0.2.2;
  import wasi:http/types@0.2.2;
  import wasi:cli/stdin@0.2.2;
  import wasi:cli/stdout@0.2.2;
  import wasi:cli/stderr@0.2.2;
  import wasi:cli/terminal-input@0.2.2;
  import wasi:cli/terminal-output@0.2.2;
  import wasi:cli/terminal-stdin@0.2.2;
  import wasi:cli/terminal-stdout@0.2.2;
  import wasi:cli/terminal-stderr@0.2.2;
  import wasi:clocks/wall-clock@0.2.2;
  import wasi:filesystem/types@0.2.2;
  import wasi:filesystem/preopens@0.2.2;
  import wasi:random/random@0.2.2;
  import wasi:http/outgoing-handler@0.2.2;

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

// ... omitted 

As you can see there, wasi:cli/terminal-stdin is in the list of imported interfaces -- that's where the requirement comes from.

So this is where your error is coming from -- I can reproduce it when I run wasmtime serve http-hello-world.wasm locallyj:

❯ wasmtime serve http-hello-world.wasm 
Error: component imports instance `wasi:cli/[email protected]`, but a matching implementation was not found in the linker

Caused by:
    0: instance export `terminal-input` has the wrong type
    1: resource implementation is missing

If I run with -S common:

wasmtime serve -S common http-hello-world.wasm
Serving HTTP on http://0.0.0.0:8080/

And things work fine :)

[EDIT] Also note that you can run wasmtime -S help to see all the options! -S common is actually deprecated (now it's -S cli), so if you wanted to fix that in our docs I'd welcome the PR :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants