Stream: platform development

Topic: elm-pages plattform


view this post on Zulip Johannes Maas (Feb 24 2024 at 08:51):

Hi everyone!

I recently had an idea of building a elm-pages plattform that would handle the custom BackendTasks (similar to ports). But I'm running into issues getting Roc to do what I want, so I though I'd ask here if someone could help me out.

What I want to achieve:

Currently, an idea that I want to play with is to write a feed reader. I want to write the part that fetches and reads the feed (fetchFeed(url)) in Roc. This function should be exposed as a JavaScript function in a special file (custom-backend-task.js) so that in Elm I can call BackendTask.custom.run "fetchFeed" feedUrlJsonValue fetchFeedResultDecoderand it will go into that .js file and look for a function "fetchFeed" to execute.

Additionally, I'd like Roc to output a definition like fetchFeed url = BackendTask.custom.run "fetchFeed" (encodeFetchFeedUrl url) fetchFeedResultDecoder along with the encodeFetchFeedUrl and feetchFeedResultDecoder so that I can conveniently call fetchFeed and all the types stay in sync with the Roc code.

Right now, I'm struggling with the first part: Getting Roc code running via JS. I'm trying to run the node-js wasm example in Windows Subsystem on Linux, but I'm getting the following error messages which I'm stuck on.

If there is a better approach for what I want to achieve, please tell me. :)

jojo@Windows-PC:~/code/roc/examples/nodejs-interop/wasm$ roc --version
roc nightly pre-release, built from commit b5f68bc on Fr 23 Feb 2024 09:01:37 UTC
jojo@Windows-PC:~/code/roc/examples/nodejs-interop/wasm$ roc build --target=wasm32
🔨 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 '<unnamed>' panicked at 'Error:
    Failed to rebuild host.zig:
        The executed command was:
            HOME=/home/jojo PATH=/home/jojo/.deno/bin:/home/jojo/.vscode-server/bin/903b1e9d8990623e3d7da1df3d33db3e42d80eda/bin/remote-cli:/home/jojo/.cargo/bin:/home/jojo/.deno/bin:/home/jojo/.nvm/versions/node/v18.16.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Python39/Scripts/:/mnt/c/Python39/:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/Program Files/dotnet/:/mnt/c/Program Files/Microsoft SQL Server/130/Tools/Binn/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Program Files/Microsoft SQL Server/110/Tools/Binn/:/mnt/c/ProgramData/chocolatey/bin:/mnt/c/Program Files (x86)/dotnet/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Users/Jojo/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/Jojo/AppData/Local/Programs/Microsoft VS Code/bin:/snap/bin:/usr/local/bin/roc_nightly-linux_x86_64:/opt/zig:/home/jojo/path/to/zig:/usr/local/bin/roc_nightly-linux_x86_64:/opt/zig zig build-obj platform/host.zig -femit-llvm-ir=platform/main.bc --mod glue::/usr/local/bin/roc_nightly-linux_x86_64/crates/compiler/builtins/bitcode/src/glue.zig --deps glue --library c -target wasm32-wasi -fPIC -fstrip
        stderr of that command:
            error: unrecognized file extension of parameter 'glue'
', crates/compiler/build/src/link.rs:1421:21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at 'Failed to (re)build platform.: Any { .. }', crates/compiler/build/src/program.rs:985:46
jojo@Windows-PC:~/code/roc/examples/nodejs-interop/wasm$ cargo run build --target=wasm32
   Compiling roc_bitcode_bc v0.0.1 (/home/jojo/code/roc/crates/compiler/builtins/bitcode/bc)
   Compiling roc_bitcode v0.0.1 (/home/jojo/code/roc/crates/compiler/builtins/bitcode)
   Compiling roc_gen_wasm v0.0.1 (/home/jojo/code/roc/crates/compiler/gen_wasm)
   Compiling wasi_libc_sys v0.0.1 (/home/jojo/code/roc/crates/wasi-libc-sys)
   Compiling roc_gen_dev v0.0.1 (/home/jojo/code/roc/crates/compiler/gen_dev)
error: failed to run custom build command for `roc_bitcode_bc v0.0.1 (/home/jojo/code/roc/crates/compiler/builtins/bitcode/bc)`

Caused by:
  process didn't exit successfully: `/home/jojo/code/roc/target/debug/build/roc_bitcode_bc-e514aeb23796a812/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-changed=build.rs
  Compiling host ir to: /home/jojo/code/roc/crates/compiler/builtins/bitcode/bc/../zig-out/builtins-host.ll
  Compiling 64-bit bitcode to: /home/jojo/code/roc/crates/compiler/builtins/bitcode/bc/../zig-out/builtins-host.bc

  --- stderr
  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 'zig build ir -Drelease=true failed with:

    /home/jojo/code/roc/crates/compiler/builtins/bitcode/build.zig:101:9: error: local variable is never mutated
  /home/jojo/code/roc/crates/compiler/builtins/bitcode/build.zig:101:9: note: consider using 'const'

  ', crates/compiler/builtins/bitcode/bc/build.rs:126:21
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
error: failed to run custom build command for `roc_bitcode v0.0.1 (/home/jojo/code/roc/crates/compiler/builtins/bitcode)`

Caused by:
  process didn't exit successfully: `/home/jojo/code/roc/target/debug/build/roc_bitcode-7ed61b98c8726b16/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-changed=build.rs
  Compiling zig object `object` to: /home/jojo/code/roc/crates/compiler/builtins/bitcode/zig-out/builtins-host.o

  --- stderr
  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 'zig build object -Drelease=true failed with:

    /home/jojo/code/roc/crates/compiler/builtins/bitcode/build.zig:101:9: error: local variable is never mutated
  /home/jojo/code/roc/crates/compiler/builtins/bitcode/build.zig:101:9: note: consider using 'const'

  ', crates/compiler/builtins/bitcode/build.rs:188:21
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

view this post on Zulip Johannes Maas (Feb 24 2024 at 09:07):

I think I found a workaround by using the basic-cli plattform and calling the binary via Node's child_process.execFile. So I can start playing around with programming in Roc.

But I'd still be interested in how to do this properly and also how to generate the accompanying Elm bindings, if anyone would have the time and interest to teach me about it. :)

view this post on Zulip Anton (Feb 24 2024 at 12:45):

Can you share the output of zig version @Johannes Maas?

view this post on Zulip Johannes Maas (Feb 24 2024 at 13:25):

jojo@Windows-PC:~/code/feed-reader$ zig version
0.12.0-dev.2825+dd1fc1cb8

view this post on Zulip Johannes Maas (Feb 24 2024 at 13:26):

Oh, I should try to install v11. Will do that later.

view this post on Zulip Johannes Maas (Feb 24 2024 at 13:43):

I'm getting different errors, but I have to do something else right now. Will have another look later.

view this post on Zulip Johannes Maas (Apr 01 2024 at 12:12):

Ok, so I got the NodeJS-Interop-WASM example running with cargo run build --target=wasm32. (roc build --target=wasm32 crashes with thread 'main' panicked at 'cannot find wasi-libc.a', crates/compiler/build/src/link.rs:121:5, though I just installed the current nightly.)

So I don't yet understand what the WASM platform does, but that's a separate issue. Before I dive in, is there a way to output the TS-types to call my Roc code from NodeJS? Is this what roc glue is for? Where can I find out more? :)

view this post on Zulip Drew Lazzeri (Apr 27 2024 at 03:25):

Hey! This is a cool idea. I think you're right that roc glue will be the mechanism for getting types for a new platform. I don't think it's set up for js yet though.

view this post on Zulip Drew Lazzeri (Apr 27 2024 at 03:57):

Here's a recent comment about the current state of Glue: https://roc.zulipchat.com/#narrow/stream/302903-Writing-a-platform/topic/.E2.9C.94.20How.20do.20I.20generate.20glue.20with.20roc.20glue.3F/near/350189504

It seems to me that only Rust and Zig have started glue plugins.


Last updated: Jul 05 2025 at 12:14 UTC