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

Compiling a rust program to wasm32-wasi gives unknown import error #3937

Closed
Mossaka opened this issue Mar 16, 2022 · 8 comments
Closed

Compiling a rust program to wasm32-wasi gives unknown import error #3937

Mossaka opened this issue Mar 16, 2022 · 8 comments

Comments

@Mossaka
Copy link
Member

Mossaka commented Mar 16, 2022

Hey there, I have the following module that tries to implement a handler function for export

wit_bindgen_rust::export!("../../wit/ephemeral/wasi-ce.wit");
use wasi_ce::*;
use cloudevents::{Event, AttributesReader};

struct WasiCe {}

impl wasi_ce::WasiCe for WasiCe {
    fn ce_handler(event: String) -> Result<String,Error> {
        let event_: Event = serde_json::from_str(event.as_str()).unwrap();
        println!("event id: {}", event_.id());
        Ok(event)
    }
}

It uses the cloudevents-sdk to deserialize the argument to a struct.

The wit file is extremely simple, just contains one handler function:
ce-handler: function(event: string) -> expected<string, error>

I am using the wit-bindgen tool for high level interface types.

After I compiled the program to wasm module using cargo build --target wasm32-wasi --release, and then I tried to run this wasm module in the wasmtime host (in python), and it outputs this runtime error:

Traceback (most recent call last):
  File "/Users/mossaka/Developer/wasi-experimental-toolkit/tests/host_py.py", line 43, in <module>
    run(event)
  File "/Users/mossaka/Developer/wasi-experimental-toolkit/tests/host_py.py", line 24, in run
    wasm = WasiCe(store, linker, module)
  File "/Users/mossaka/Developer/wasi-experimental-toolkit/tests/bindings.py", line 73, in __init__
    self.instance = linker.instantiate(store, module)
  File "/usr/local/Caskroom/miniconda/base/envs/myenv/lib/python3.9/site-packages/wasmtime/_linker.py", line 165, in instantiate
    raise WasmtimeError._from_ptr(error)
wasmtime._error.WasmtimeError: unknown import: `__wbindgen_placeholder__::__wbindgen_describe` has not been defined

Any suggestions on how to understand this error message and hints on solve would be greatly appreciated!

@bjorn3
Copy link
Contributor

bjorn3 commented Mar 17, 2022

One of cloudevents' dependencies uses wasm-bindgen which requires you to run the wasm-bindegen tool over the wasm file. Wasm-bindgen is a tool to allow interaction between wasm and javascript (eg browser or nodejs). It does not work with wasm runtimes that don't use javascript like wasmtime. Note that cloudflare workers uses the v8 js engine to run wasm.

@Mossaka
Copy link
Member Author

Mossaka commented Mar 17, 2022

Okay thanks! Does this mean that if I want to use wasmtime as the runtime, I can't use cloudevents-sdk crate anymore, right?

@bjorn3
Copy link
Contributor

bjorn3 commented Mar 17, 2022

I think so.

@alexcrichton
Copy link
Member

Yes it looks like one of your dependency crates is using wasm-bindgen and wasm-bindgen injects imports into the wasm module which are expected to be filled in by JS but that's not happening here, hence the unknown import error.

@Haishi2016
Copy link

I have a similar issue. Using cargo tree, I don't see any dependencies on wasm-bindgen at any levels, though the generated wasm using cargo build --target wasm32-wasi has unresolved __wbindgen_placeholder__ imports.

@sunfishcode
Copy link
Member

@Haishi2016 It's difficult to say without knowing more about your project. Could you file a new issue about this, and say more about how your project works and what dependencies you have?

@b4stien
Copy link

b4stien commented Dec 7, 2022

FYI there is a doc currently pointing to wasm-bindgen in the repo, with a small disclaimer that the whole section actually does not work at all.

@sunfishcode
Copy link
Member

Thanks! #5394 is now a PR which updates that documentation.

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

6 participants