Stream: beginners

Topic: ✔ Using Cmd module to launch host apps


view this post on Zulip Karakatiza (Dec 25 2023 at 09:17):

Hey all! I am trying to open a browser window from CLI which works from a Linux terminal: xdg-open http://localhost:8080
But this code yields IOError "No such file or directory (os error 2)":

envPath <-
    Env.var "PATH"
    |> Task.onErr (\ VarNotFound -> crash "PATH env not found, really?")
    |> await
res <-
    Cmd.new "xdg-open"
    |> Cmd.arg 'http://localhost:8080'
    |> Cmd.env "PATH" envPath
    |> Cmd.output
    |> Task.onErr (\e -> crash (Inspect.toStr e))
    |> await

I have also tried Cmd.new "/usr/bin/xdg-open", Cmd.new "sh -c" |> Cmd.arg "xdg-open http://localhost:8080" and manually initializing PATH env var, but didn't get anywhere.
I am using --linker=legacy

view this post on Zulip Anton (Dec 25 2023 at 12:48):

Hmm, that code does work for me (on NixOS), can you share your OS and architecture?

view this post on Zulip Karakatiza (Dec 25 2023 at 17:40):

Ubuntu 22.04.3 LTS, non-WSL, x86_64
Minimal example to reproduce for me, either with or without --linker=legacy:

app "hello"
  packages {
      pf: "https://github.com/roc-lang/basic-cli/releases/download/0.7.0/bkGby8jb0tmZYsy2hg1E_B2QrCgcSTxdUlHtETwm5m4.tar.br",
      json: "https://github.com/lukewilliamboswell/roc-json/releases/download/0.6.0/hJySbEhJV026DlVCHXGOZNOeoOl7468y9F9Buhj0J18.tar.br"
  }
  imports [pf.Task.{ Task, await }, pf.Cmd, pf.Env]
  provides [main] to pf

url = "http://localhost:8080"
main =
  envPath <- Env.var "PATH" |> Task.onErr (\ VarNotFound -> crash "PATH env not found, really?") |> await
  {} <-
      Cmd.new "xdg-open"
      |> Cmd.arg url
      |> Cmd.env "PATH" envPath
      |> Cmd.output
      |> Task.onErr (\e -> crash (Inspect.toStr e))
      |> await
  Task.ok {}

view this post on Zulip Anton (Dec 25 2023 at 18:08):

Thanks, I can take a look on my ubuntu vm tomorrow

view this post on Zulip Anton (Dec 26 2023 at 10:30):

That also works for me (on Ubuntu 22.04). To narrow this down, I would try:

view this post on Zulip Karakatiza (Dec 27 2023 at 11:12):

I realized I was running the file in Ubuntu Docker container (same version as the host machine). Directly on host the code works fine. I can keep developing in container and testing on host, but eventually I'd like to be able run my CLI app within the container, too.
Here is the strace when running in the container:

open("/proc/self/exe", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3
fcntl(3, F_SETFD, FD_CLOEXEC)           = 0
fstat(3, {st_mode=S_IFREG|0755, st_size=2759264, ...}) = 0
mmap(NULL, 2759264, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fb4b2e00000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb4b3449000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb4b3448000
munmap(0x7fb4b3449000, 4096)            = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb4b3446000
munmap(0x7fb4b3448000, 4096)            = 0
mmap(NULL, 16384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb4b3442000
munmap(0x7fb4b3446000, 8192)            = 0
mmap(NULL, 28672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb4b343b000
munmap(0x7fb4b3442000, 16384)           = 0
mmap(NULL, 53248, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb4b31f3000
munmap(0x7fb4b343b000, 28672)           = 0
mmap(NULL, 102400, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb4b31da000
munmap(0x7fb4b31f3000, 53248)           = 0
stat("/usr/lib/debug", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/usr/lib/debug/.build-id/c9/d2cee6f567f1cc0aacc6fa91c163c14279364b.debug", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/proc/self/exe.dwp", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory)

/usr/lib/debug/.build-id/ was indeed missing inside the container, so I ran
sudo apt-get update && sudo apt-get install libc6-dbg in the container and recompiled the Roc program but still getting the same strace. The issue persists with --optimize flag, too.

view this post on Zulip Anton (Dec 27 2023 at 12:38):

Strange, I'm investigating...

view this post on Zulip Anton (Dec 27 2023 at 12:41):

Can you share your Dockerfile?

view this post on Zulip Karakatiza (Dec 27 2023 at 12:42):

https://github.com/feldera/feldera/blob/main/.devcontainer/Dockerfile

view this post on Zulip Karakatiza (Dec 27 2023 at 12:48):

I am using a nightly build of Roc, not compiling from source btw

view this post on Zulip Anton (Dec 27 2023 at 13:57):

It doesn't really make sense to run xdg-open from inside docker right?

view this post on Zulip Karakatiza (Dec 27 2023 at 14:08):

Oh, crap. I just realized it probably doesn't :sweat_smile: And thus xdg-utils are not even installed in a Docker container. Sorry for leading you down the wrong path!

view this post on Zulip Notification Bot (Dec 27 2023 at 14:09):

Karakatiza has marked this topic as resolved.

view this post on Zulip Karakatiza (Dec 27 2023 at 14:11):

To sum up: the original code is valid and works in an environment with a desktop subsystem (and with xdg-utils) installed.


Last updated: Jul 06 2025 at 12:14 UTC