grain-lang / grain

The Grain compiler toolchain and CLI. Home of the modern web staple. 🌾
https://grain-lang.org/
GNU Lesser General Public License v3.0
3.25k stars 114 forks source link

Duplicate imports #2152

Open lann opened 2 weeks ago

lann commented 2 weeks ago

Grain produces Wasm modules with duplicate imports. This is permitted by the Wasm core spec, but with caveats:

https://webassembly.github.io/spec/core/syntax/modules.html#imports

It is possible to import the same module/name pair multiple times [...] However, embedders are not required to support such overloading

The Wasm Component Model in particular does not support duplicate imports, which means that WASI preview1 modules compiled with Grain cannot be converted to components with the preview1 adapter.

Example:

$ grain --version
0.6.6
$ cat hello.gr
module Main
print("Hello, World")
$ grain compile hello.gr
$ wasm-tools dump hello.gr.wasm | grep Import
   0x16b | 16 77 61 73 | import [func 0] Import { module: "wasi_snapshot_preview1", name: "fd_write", ty: Func(2) }
   0x18d | 16 77 61 73 | import [func 1] Import { module: "wasi_snapshot_preview1", name: "fd_write", ty: Func(2) }
   0x1af | 16 77 61 73 | import [func 2] Import { module: "wasi_snapshot_preview1", name: "fd_write", ty: Func(2) }
   0x1d1 | 16 77 61 73 | import [func 3] Import { module: "wasi_snapshot_preview1", name: "fd_write", ty: Func(2) }
spotandjake commented 2 weeks ago

If you run grain compile <file> --release it will produce a module without multiple imports.

ospencer commented 2 weeks ago

To add to what @spotandjake mentioned, we have successfully used the adapter to make components. Let me know if you want some pointers!

We've been aware of this issue but have been slow to fix it because of the available workaround. This is something we can resolve in the next major release.

lann commented 2 weeks ago

Aha thanks! Feel free to resolve this unless you'd like to keep it for tracking.