Stream: beginners

Topic: listing files in a directory


view this post on Zulip Jakub (Nov 20 2023 at 21:45):

@Brian Carroll thanks a lot for the recommendation. By gluing together few examples I managed to put together something that should give me a list of files, but because I don't really know what I'm doing yet - it fails. I want to figure it out on my own but I'm not really sure where can I find all the error types in the task result section:

app "file-read"
    packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.5.0/Cufzl36_SnJ4QbOoEmiJ5dIpUxBvdB3NEySvuH82Wio.tar.br" }
    imports [
        pf.Stdout,
        pf.Task.{ Task },
        pf.Path,
        pf.Env,
        pf.Dir,
    ]
    provides [main] to pf

main =
    task =
        cwd <- Env.cwd |> Task.await

        dirEntries <- Dir.list cwd |> Task.await
        contentsStr = Str.joinWith (List.map dirEntries Path.display) "\n    "
        Stdout.line contentsStr

    Task.attempt task \result ->
        when result is
            Ok {} -> Stdout.line "Success"
            Err err ->
                msg =
                    when err is
                        IOError _ _ -> "Error reading dir"
                        DecodeError -> "Decode Error"
                        _ -> "there was an error!"
                Stdout.line msg

view this post on Zulip Notification Bot (Nov 20 2023 at 21:51):

A message was moved here from #introductions > introductions by Richard Feldman.

view this post on Zulip Richard Feldman (Nov 20 2023 at 21:51):

@Jakub welcome! :wave:

view this post on Zulip Richard Feldman (Nov 20 2023 at 21:51):

would you mind printing out the error you're seeing?

view this post on Zulip Jakub (Nov 20 2023 at 21:52):

I'm getting there was an error!

view this post on Zulip Jakub (Nov 20 2023 at 21:55):

Debian GNU/Linux 11 I'm using mcr.microsoft.com/devcontainers/base:bullseye in VSCode DevContainers. BTW I was thinking about creating dev Docker image in case people would like to try out Roc without having to go through the installation process. Would it add any value / help anyone?

view this post on Zulip Jakub (Nov 20 2023 at 21:58):

and one more thing, the code above is a clumsy copy / paste of: https://raw.githubusercontent.com/roc-lang/roc/main/examples/cli/file.roc - I tried to go backwards to get to the shortest code that could possibly work. Maybe I'm importing a wrong platform...

view this post on Zulip Luke Boswell (Nov 20 2023 at 22:37):

Yeah so Dir.list on basic cli is currently unimplemented and is returning an error tag rather than crashing or panicking in rust.

view this post on Zulip Jakub (Nov 20 2023 at 22:43):

fixing this might be beyond my current grasp of internals of Roc but as a genera question, does it make sense to write tests for functions kept in roc/examples/cli/cli-platform/src/lib.rs

view this post on Zulip Luke Boswell (Nov 20 2023 at 22:45):

I have been talking about https://github.com/roc-lang/basic-cli sorry for any confusion

view this post on Zulip Anton (Nov 21 2023 at 12:22):

Hi @Jakub,
I'll look into the DIr.list failure, in the meantime you could use the ls command as a workaround.

I was thinking about creating dev Docker image in case people would like to try out Roc without having to go through the installation process.

You can add a Dockerfile to the repo if you are also willing to set up a github workflow to test it, see this script for inspiration.

view this post on Zulip Anton (Nov 21 2023 at 16:39):

There is now an issue for setting up a docker image #6045


Last updated: Jul 06 2025 at 12:14 UTC