Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wasi target command flag vs environment variables #4439

Open
mattjohnsonpint opened this issue Aug 27, 2024 · 2 comments
Open

Wasi target command flag vs environment variables #4439

mattjohnsonpint opened this issue Aug 27, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@mattjohnsonpint
Copy link

mattjohnsonpint commented Aug 27, 2024

My understanding is that setting GOOS=wasip1 GOARCH=wasm should produce identical output as passing the -target wasi command line flag, but it doesn't. Rather, it seem to match -target wasm.

Repro:

package main

func main() {
	println("Hello, World!")
}

You'll need wasm2wat from The WebAssembly Binary Toolkit.

Then:

  • Compile the different ways. Optimizations off to see everything.
tinygo build -target wasi -opt 0 -o test1.wasm
tinygo build -target wasm -opt 0 -o test2.wasm
GOOS=wasip1 GOARCH=wasm tinygo build -opt 0 -o test3.wasm
  • Convert output to WAT format for readability
wasm2wat test1.wasm -o test1.wat
wasm2wat test2.wasm -o test2.wat
wasm2wat test3.wasm -o test3.wat

Open each wat file in a text editor. Imports are near the top, exports are near the bottom.

test1.wat

...
(import "wasi_snapshot_preview1" "fd_write" (func $runtime.fd_write (type 4)))
(import "wasi_snapshot_preview1" "poll_oneoff" (func $runtime.poll_oneoff (type 4)))
(import "wasi_snapshot_preview1" "clock_time_get" (func $runtime.clock_time_get (type 16)))
...
(export "memory" (memory 0))
(export "malloc" (func $malloc))
(export "free" (func $free))
(export "calloc" (func $calloc))
(export "realloc" (func $realloc))
(export "_start" (func $_start))
(export "asyncify_start_unwind" (func 168))
(export "asyncify_stop_unwind" (func 169))
(export "asyncify_start_rewind" (func 170))
(export "asyncify_stop_rewind" (func 171))
(export "asyncify_get_state" (func 172))
...

test2.wat / test3.wat

...
(import "gojs" "runtime.ticks" (func $runtime.ticks (type 15)))
(import "gojs" "runtime.sleepTicks" (func $runtime.sleepTicks (type 16)))
(import "wasi_snapshot_preview1" "fd_write" (func $runtime.fd_write (type 4)))
...
(export "memory" (memory 0))
(export "malloc" (func $malloc.command_export))
(export "free" (func $free.command_export))
(export "calloc" (func $calloc.command_export))
(export "realloc" (func $realloc.command_export))
(export "_start" (func $_start.command_export))
(export "resume" (func $resume.command_export))
(export "go_scheduler" (func $go_scheduler.command_export))
(export "asyncify_start_unwind" (func 175))
(export "asyncify_stop_unwind" (func 176))
(export "asyncify_start_rewind" (func 177))
(export "asyncify_stop_rewind" (func 178))
(export "asyncify_get_state" (func 179))
...

They should be identical, but test3 is importing from gojs, and is also exporting go_scheduler. It's identical to test2, whereas I would expect it to match test1.

@dgryski
Copy link
Member

dgryski commented Aug 27, 2024

I think this will probably be solved by #4437

/cc @aykevl

@aykevl
Copy link
Member

aykevl commented Aug 29, 2024

The problem here is the file extension, which apparently overrides the environment variables. That's a bug.

tinygo/main.go

Lines 1569 to 1571 in ef4f46f

if options.Target == "" && filepath.Ext(outpath) == ".wasm" {
options.Target = "wasm"
}

Maybe we should just remove this special case? It means that people that currently rely on the extension will have to provide some explicit flags or environment variables, but it's certainly more explicit and less "magic".
(I think it was originally introduced when wasm was mostly a browser thing and WASI didn't really exist yet).

aykevl added a commit that referenced this issue Aug 29, 2024
Currently this overrides GOOS/GOARCH, which are also used for wasm.
This will break people who rely on a command like this:

    tinygo build -o foo.wasm path/to/package

They will need to update to explicitly set the target, for example:

    tinygo build -o foo.wasm -target=wasm path/to/package

Fixes: #4439
@deadprogram deadprogram added the bug Something isn't working label Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants