Stream: wit-bindgen

Topic: module requires an import interface named env


view this post on Zulip benwis (Feb 24 2024 at 21:46):

I've tried using both cargo component and manual wit-bindgen, but both give me an error error: module requires an import interface named env`` converting a preview1 wasm module to a preview2 wasm component , and I can't find a reference to it in the example

view this post on Zulip Joel Dice (Feb 24 2024 at 22:26):

You can use e.g. wasm-tools print to see what functions the module is importing from env, which might point to what's wrong.

view this post on Zulip benwis (Feb 24 2024 at 22:29):

Interesting, I guess it's these:

  (import "env" "_Znwm" (func $_Znwm (;0;) (type 4)))
  (import "env" "_ZdlPv" (func $_ZdlPv (;1;) (type 0)))
  (import "env" "__cxa_allocate_exception" (func $__cxa_allocate_exception (;2;) (type 4)))
  (import "env" "_ZNSt12length_errorD1Ev" (func $_ZNSt12length_errorD1Ev (;3;) (type 4)))
  (import "env" "__cxa_throw" (func $__cxa_throw (;4;) (type 6)))
  (import "env" "_ZNSt20bad_array_new_lengthD1Ev" (func $_ZNSt20bad_array_new_lengthD1Ev (;5;) (type 4)))
  (import "env" "_ZNSt20bad_array_new_lengthC1Ev" (func $_ZNSt20bad_array_new_lengthC1Ev (;6;) (type 4)))
  (import "env" "_ZnwmSt11align_val_t" (func $_ZnwmSt11align_val_t (;20;) (type 3)))
  (import "env" "_ZdlPvSt11align_val_t" (func $_ZdlPvSt11align_val_t (;21;) (type 2)))

view this post on Zulip Dan Gohman (Feb 24 2024 at 22:32):

Did you by chance link C++ code using the clang command? If so, you may need to link with the clang++ command.

view this post on Zulip benwis (Feb 24 2024 at 22:33):

Yeah, this was the build command

RUSTFLAGS='-L /opt/wasi-sdk-21.0/share/wasi-sysroot/lib/wasm32-wasi' CXXSTDLIB=c++ CC=/opt/wasi-sdk-21.0/bin/clang CXX=/opt/wasi-sdk-21.0/bin/clang  cargo component build

view this post on Zulip Dan Gohman (Feb 24 2024 at 22:34):

Yeah; try changing the clang at the end of the CXX to clang++

view this post on Zulip Joel Dice (Feb 24 2024 at 22:35):

Context: this rust library pulls in a wrapper crate for tree-sitter, which is written (partly) in C++

view this post on Zulip benwis (Feb 24 2024 at 22:37):

This gives the same error and the same env lines

 RUSTFLAGS='-L /opt/wasi-sdk-21.0/share/wasi-sysroot/lib/wasm32-wasi' CXXSTDLIB=c++ CC=/opt/wasi-sdk-21.0/bin/clang CXX=/opt/wasi-sdk-21.0/bin/clang++  cargo component build

view this post on Zulip Joel Dice (Feb 24 2024 at 22:46):

IIRC, the linker was rust-lld

view this post on Zulip benwis (Feb 24 2024 at 22:47):

I did get that error at one point that mentioned rust-lld
This is the repo if anyone wants to try: https://github.com/benwis/femark/tree/wasi
Running on latest Rust stable on Arch Linux

A Markdown to HTML compiler and Syntax Highlighter, built using Rust's pulldown-cmark and tree-sitter-highlight crates. Compiled for use via the Node FFI. - GitHub - benwis/femark at wasi

view this post on Zulip benwis (Feb 24 2024 at 23:14):

Joel Dice said:

IIRC, the linker was rust-lld

I could tell cargo to try using an alternate linker, is the lld in the same folder the recommended one?

view this post on Zulip Dan Gohman (Feb 24 2024 at 23:33):

I'd guess rust-lld is fine, but what's needed is to link in the C++ standard library. You may need to add something like -lc++ -lc++abi to the link line..

view this post on Zulip benwis (Feb 25 2024 at 01:52):

Dan Gohman said:

I'd guess rust-lld is fine, but what's needed is to link in the C++ standard library. You may need to add something like -lc++ -lc++abi to the link line..

For a C build noob, where would I add that?

view this post on Zulip benwis (Feb 25 2024 at 03:15):

Ok I tried adding it to RUSTFLAGS like so

RUSTFLAGS='-L /opt/wasi-sdk-21.0/share/wasi-sysroot/lib/wasm32-wasi -lstatic=c++ -lstatic=c++abi' CXXSTDLIB=c++ CC=/opt/wasi-sdk-21.0/bin/clang CXX=/opt/wasi-sdk-21.0/bin/clang++  cargo component build

it seems to throw the same error, I'll play with this a bit. I don't understand where the env interface comes from and what controls what goes into it

view this post on Zulip benwis (Feb 25 2024 at 04:08):

A thought occurs to me that this might not be a cargo issue, since it successfully builds

   Compiling wit-parser v0.200.0
   Compiling wasm-metadata v0.200.0
   Compiling tree-sitter-highlight v0.20.1
   Compiling wit-bindgen-core v0.19.1
   Compiling wit-component v0.200.0
   Compiling femark v0.1.3 (/celCluster/projects/femark)
    Finished dev [unoptimized + debuginfo] target(s) in 10.67s
    Creating component /celCluster/projects/femark/target/wasm32-wasi/debug/femark.wasm
error: module requires an import interface named `env`

femark on  wasi [$!?] is 📦 v0.1.3 via 🦀 v1.76.0 took 10s

and it's only the conversion to preview2 that's failing

view this post on Zulip benwis (Feb 25 2024 at 06:10):

Fixed it, turns out I needed to set CXXFLAGS=-fno-exceptions and that cleared it right up


Last updated: Oct 23 2024 at 20:03 UTC