Stream: beginners

Topic: wasm32 basic-cli


view this post on Zulip Matthew Phillips (Nov 20 2023 at 18:13):

Hello, I tried running the hello world, wanting to create a wasm output, ./roc build --target wasm32 but got this error:

I was expecting this file to exist:

/Users/matthew/.cache/roc/packages/github.com/roc-lang/basic-cli/releases/download/0.5.0/Cufzl36_SnJ4QbOoEmiJ5dIpUxBvdB3NEySvuH82Wio/host.zig

However, it was not there!

view this post on Zulip Matthew Phillips (Nov 20 2023 at 18:14):

The help suggests that using --target wasm32 should not assume prebuilt, but that doesn't seem to be the case. Any advice?

view this post on Zulip Notification Bot (Nov 20 2023 at 18:14):

2 messages were moved here from #beginners > my from time to time feedback about Roc by Anton.

view this post on Zulip Anton (Nov 20 2023 at 18:22):

Hi @Matthew Phillips,
The basic-cli platform does not support targeting wasm. That's because a lot of basic-cli functions can't work in a wasm/browser context, like reading from stdin. We should have a better error message for this case though.

You may want to checkout this wasm platform.

Can you share what you would like to build/test with wasm?

view this post on Zulip Anton (Nov 20 2023 at 18:22):

That may help us guide you better.

view this post on Zulip Matthew Phillips (Nov 20 2023 at 18:23):

Ah ok thank you. So the idea is you use a different cli when building to wasm?

view this post on Zulip Anton (Nov 20 2023 at 18:24):

A different platform to be specific.

view this post on Zulip Matthew Phillips (Nov 20 2023 at 18:24):

Hm, ok I think the problem is I don't know what "platform" is yet

view this post on Zulip Matthew Phillips (Nov 20 2023 at 18:24):

so i'll read that page

view this post on Zulip Matthew Phillips (Nov 20 2023 at 18:26):

ok, so the pf in the app config defines the platform? So I just need to swap that out? Will try it.

view this post on Zulip Matthew Phillips (Nov 20 2023 at 18:29):

Do you need a zig file to use wasm?

view this post on Zulip Anton (Nov 20 2023 at 18:29):

Yes, I believe so

view this post on Zulip Anton (Nov 20 2023 at 18:31):

I recommend downloading this platform locally and following along with the README

view this post on Zulip Matthew Phillips (Nov 20 2023 at 18:32):

ok, i'll test that out, thanks

view this post on Zulip Anton (Nov 20 2023 at 18:32):

So I just need to swap that out?

Different platforms work with different types and provide different functionality so in most cases it will not be possible to swap out the platform without changing anything else.

view this post on Zulip Anton (Nov 20 2023 at 18:32):

I have to go now, but other people should be able to help out :)

view this post on Zulip Matthew Phillips (Nov 20 2023 at 18:33):

Oh, well it's already included in the nightly.

view this post on Zulip Matthew Phillips (Nov 20 2023 at 18:33):

Thanks @Anton

view this post on Zulip Matthew Phillips (Nov 20 2023 at 18:35):

I get a compiler error. Getting closer!

view this post on Zulip Matthew Phillips (Nov 20 2023 at 18:36):

I think it's because I have the wrong version of zig installed.

view this post on Zulip Matthew Phillips (Nov 20 2023 at 18:37):

Fixing that part now.

view this post on Zulip Matthew Phillips (Nov 20 2023 at 18:40):

Now installing Rust...

view this post on Zulip Matthew Phillips (Nov 20 2023 at 18:45):

:hammer: Rebuilding platform...
An internal compiler expectation was broken.
This is definitely a compiler bug.
Please file an issue here: https://github.com/roc-lang/roc/issues/new/choose
thread 'main' panicked at 'cannot find wasi-libc.a', crates/compiler/build/src/link.rs:133:5

view this post on Zulip Matthew Phillips (Nov 20 2023 at 18:45):

rustc --version
rustc 1.74.0 (79e9716c9 2023-11-13)

view this post on Zulip Matthew Phillips (Nov 20 2023 at 18:45):

zig version
0.11.0

view this post on Zulip Matthew Phillips (Nov 20 2023 at 18:54):

app "rocLovesWebAssembly"
packages { pf: "./examples/platform-switching/web-assembly-platform/main.roc" }
imports []
provides [main] to pf

main = "Roc <3 Web Assembly!\n"

view this post on Zulip Brendan Hansknecht (Nov 20 2023 at 19:00):

My guess is that we build that file but have failed to include it in the nightly releases. So the platform works if the compiler is built from source, but not if it is downloaded from nightly.

view this post on Zulip Matthew Phillips (Nov 20 2023 at 19:08):

Ok, might try that

view this post on Zulip Matthew Phillips (Nov 20 2023 at 19:28):

If you run cargo build where are the binaries? I'm not familiar with rust conventions

view this post on Zulip Ayaz Hafiz (Nov 20 2023 at 19:30):

target/debug/roc (or target/release/roc if you built with --release)

view this post on Zulip Matthew Phillips (Nov 20 2023 at 19:30):

Perfect, thanks

view this post on Zulip Matthew Phillips (Nov 20 2023 at 19:31):

woohoo, it all works now

view this post on Zulip Brendan Hansknecht (Nov 20 2023 at 19:32):

I'll file a bug for that

view this post on Zulip Brendan Hansknecht (Nov 20 2023 at 19:32):

Should definitely work for nightly too

view this post on Zulip Brendan Hansknecht (Nov 20 2023 at 19:36):

#6029

view this post on Zulip Matthew Phillips (Nov 20 2023 at 19:37):

Not sure if this is another bug or I did something wrong:

LinkError: WebAssembly.instantiate(): Import #2 module="wasi_snapshot_preview1" function="random_get": function import requires a callable

That's in the browser.

view this post on Zulip Matthew Phillips (Nov 20 2023 at 19:40):

Yeah, i think that's a bug too

view this post on Zulip Matthew Phillips (Nov 20 2023 at 19:41):

I would guess that's just the example missing that function

view this post on Zulip Matthew Phillips (Nov 20 2023 at 19:41):

But I don't see anything in the repo about "random_get"

view this post on Zulip Matthew Phillips (Nov 20 2023 at 19:43):

  random_get(v) {
    return Math.random();
  }

Adding this to the importObj fixed it. I assume this is not right though!

view this post on Zulip Matthew Phillips (Nov 20 2023 at 19:44):

good enough to unblock me, for now.

view this post on Zulip Brendan Hansknecht (Nov 20 2023 at 19:48):

Hmm. So I think I see the real issue, this is compiling for wasi, which is web assembly not in the browser. I think it should be compiling for just wasm without any extra wasi stuff.

view this post on Zulip Matthew Phillips (Nov 20 2023 at 19:49):

ah yeah that makes sense

view this post on Zulip Matthew Phillips (Nov 20 2023 at 19:53):

That problems means the binary size is a little larger than it would normally be

view this post on Zulip Matthew Phillips (Nov 20 2023 at 19:54):

not that it's big currently or anything


Last updated: Jul 05 2025 at 12:14 UTC