Stream: beginners

Topic: Imports inside a module


view this post on Zulip Adam Bobrow (Sep 02 2024 at 11:38):

Hi all,
I have a question regarding importing packages inside a module.
I have an app that requires CSV parsing and I want to separate this logic from my main app logic. I tried to put the logic in a module but I'm not sure if/how I can import the roc-parser package from within my module. is that possible? if not, whats the proper way of separating my code when it depends on external packages?
Thanks!

view this post on Zulip Anton (Sep 02 2024 at 12:00):

Hi @Adam Bobrow,
Declaration of dependencies happens in the app file, like so:

app [main] {
    cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
    parser: "https://github.com/Anton-4/roc-parser/releases/download/0.7.2/xcLMjkUXqAJoSsfKblZhVn3slAv3sdFcBxRbqBDoUJk.tar.br",
}

And in your module you should then be able to import what you need:

import parser.Core exposing [Parser, many, oneOf, map]
import parser.String exposing [parseStr, codeunit, anyCodeunit]

view this post on Zulip Anton (Sep 02 2024 at 12:02):

I'll put something together for www.roc-lang.org/examples

view this post on Zulip Adam Bobrow (Sep 02 2024 at 17:44):

Hi @Anton , thank you I'll try that!

view this post on Zulip Luke Boswell (Sep 02 2024 at 23:08):

@Agus Zubiaga @Richard Feldman can you clarify if the intention is for imports to work this way into the future?

I thought I heard Agus mention that it was a work around until we have module params (which we do now), but it seems strange/unrealistic that you would have to thread something like this all the way through the application.

Specifically, should you be able to import a package from within a random module?

view this post on Zulip Luke Boswell (Sep 02 2024 at 23:09):

For context I'm just looking at https://github.com/roc-lang/examples/pull/203 and thought I'd seek clarification on this before we add an example on it.

view this post on Zulip Agus Zubiaga (Sep 02 2024 at 23:10):

No, this isn’t really a workaround. You should be able to import anything that’s available to the app from any of its modules.

view this post on Zulip Luke Boswell (Sep 02 2024 at 23:12):

Thank you

view this post on Zulip Luke Boswell (Sep 02 2024 at 23:13):

And hence roc "walks" back up the tree until it finds a main.roc to see what those imports are.

view this post on Zulip Luke Boswell (Sep 02 2024 at 23:15):

Although this is still an issue or bug? we currently have. I am seeing "UNRECOGNIZED PACKAGE" errors from the LS.

Screenshot-2024-09-03-at-09.15.06.png

view this post on Zulip Agus Zubiaga (Sep 02 2024 at 23:15):

We’re probably going to change how platforms expose effects to the app. Right now, you can import them from its modules directly, but ideally the platform would provide them to the app’s entry point and you’d then thread them using module params.

view this post on Zulip Luke Boswell (Sep 02 2024 at 23:17):

I just realised that if I change my app.roc to main.roc it makes the language server happy. :smiley: I just never actually read that error message all the way through to the hint part :sweat_smile:

view this post on Zulip Agus Zubiaga (Sep 02 2024 at 23:18):

Yeah, at the moment the language server doesn’t haven’t a way to configure a different entry point.

view this post on Zulip Agus Zubiaga (Sep 02 2024 at 23:19):

The CLI, however, accepts a —main flag to do so


Last updated: Jul 06 2025 at 12:14 UTC