Search code examples
gowebassemblytinygowasmtime

Golang to wasm compilation using tinygo. Execution using wasmtime


I have the following test code

func main() {
    path, err := os.Getwd()
    log.Println(path, err)
    files, err := ioutil.ReadDir("/etc/dse")
    log.Println(files, err)
}

I compile it to wasm using

tinygo build -target wasi -o list.wasm list.go

Then I executeit using

wasmtime list.wasm

The output is

2023/05/05 16:00:50 / <nil>
2023/05/05 16:00:50 [] open /etc/dse: errno 76

At the same time, the directory /etc/dse exists and has permissions 777.

What is the source of this error and how to fix this?


Solution

  • errno 76 means capabilities insufficient.

    You have to give it capabilities to access files in the requisite directories, with the wasmtime option --dir (see wasmtime WASI tutorial):

    The --dir= option instructs wasmtime to preopen a directory, and make it available to the program as a capability which can be used to open files inside that directory.

    $ wasmtime --dir=/etc/dse list.wasm
    2023/05/06 01:36:31 / <nil>
    2023/05/06 01:36:31 [] readdir unimplemented : errno 54
    

    Unfortunately, readdir is not implemented in TinyGo yet.

    References: