wasmerio / wasmer-go

🐹🕸️ WebAssembly runtime for Go
https://pkg.go.dev/github.com/wasmerio/wasmer-go
MIT License
2.84k stars 161 forks source link

Failed to instantiate the module: Missing import: `wasi_snapshot_preview1`.`fd_write` #312

Open npchp110 opened 3 years ago

npchp110 commented 3 years ago

Describe the bug

Failed to instantiate the module: Missing import: wasi_snapshot_preview1.fd_write

Steps to reproduce

  1. use tinygo to compile go code to wasm
package main

// This calls a JS function from Go.
func main() {
}

// This function is exported to JavaScript, so can be called using
// exports.multiply() in JavaScript.
//export multiply
func multiply(x, y int) int {
    return x * y
}
tinygo build -o test.wasm -target wasi .
  1. user wasmer to load test.wasm, Failed to instantiate the module

    func callWasm() {
    wasmBytes, _ := ioutil.ReadFile("test.wasm")
    engine := wasmer.NewEngine()
    store := wasmer.NewStore(engine)
    module, err := wasmer.NewModule(store, wasmBytes)
    if err != nil {
        fmt.Println("Failed to compile module:", err)
    }
    importObject := wasmer.NewImportObject()
    instance, err := wasmer.NewInstance(module, importObject)
    if err != nil {
        panic(fmt.Sprintln("Failed to instantiate the module:", err))
    }
    
    sum, err := instance.Exports.GetFunction("multiply")
    if err != nil {
        panic(fmt.Sprintln("Failed to get the `add_one` function:", err))
    }
    result, err := sum(1, 2)
    if err != nil {
        panic(fmt.Sprintln("Failed to call the `add_one` function:", err))
    }
    
    fmt.Println("Results of `sum`:", result)
    }
panic: Failed to instantiate the module: Missing import: `wasi_snapshot_preview1`.`fd_write`

Additional context

tinygo version 0.20.0 linux/amd64 (using go version go1.17.1 and LLVM version 11.0.0)

soulteary commented 3 years ago

@npchp110 The reason for this problem has been explained by the @Hywan in this issue. https://github.com/wasmerio/wasmer-go/issues/275

As for how to solve this problem, you can refer to the demo I recently submitted. Of course, you also need to compile a wasm program that conforms to the wasi standard. #315

If you want to know more details, you can read this technical blog, but it needs to be translated because I use Chinese for content writing. https://soulteary.com/2021/11/21/use-docker-and-golang-to-quickly-get-started-with-webassembly.html

AntonioND commented 1 year ago

I've been having this issue right now, even after following the blog article of @soulteary

https://github.com/wasmerio/wasmer-go/issues/275#issuecomment-1416547986

AntonioND commented 1 year ago

As I've mentioned in the other issue, my solution was to build my binary this way:

tinygo build --no-debug -o simple.wasm -target=wasi -panic=trap -scheduler=none main.go

The problem was that the default panic handler tries to print information. The option -panic=trap removes that behaviour, so all other unnecessary functions go away.

alanorwick commented 1 year ago

Just tagging along here, I'm also seeing Missing import: wasi_snapshot_preview1 . clock_time_get after resolving the fd_write import.

alanorwick commented 1 year ago

Just tagging along here, I'm also seeing Missing import: wasi_snapshot_preview1 . clock_time_get after resolving the fd_write import.

Resolved with this: https://github.com/wasmerio/wasmer-go/pull/315/files


wasiEnv, _ := wasmer.NewWasiStateBuilder("wasi-program").
        // Choose according to your actual situation
        // Argument("--foo").
        // Environment("ABC", "DEF").
        // MapDirectory("./", ".").
        Finalize()
    importObject, err := wasiEnv.GenerateImportObject(store, module)
    check(err)