grain icon indicating copy to clipboard operation
grain copied to clipboard

Duplicate imports

Open lann opened this issue 1 year ago • 3 comments

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) }

lann avatar Aug 28 '24 15:08 lann

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

spotandjake avatar Aug 28 '24 15:08 spotandjake

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.

ospencer avatar Aug 28 '24 15:08 ospencer

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

lann avatar Aug 28 '24 15:08 lann