Stream: ideas

Topic: Handle non roc files


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

I just accidentally atetmpted to roc run on an exectutable non-.roc file and got the following error.

$ roc run examples/csv-movies
── MISSING HEADER in examples/csv-movies ───────────────────────────────────────

I am expecting a header, but the file is not UTF-8 encoded.

I am expecting a module keyword next, one of interface, app, package
or platform.%

Should the cli check for the .roc extension and return a more helpful error? or is it expected that roc scripts/apps may use alternative file extensions?

view this post on Zulip Brendan Hansknecht (Jan 23 2024 at 02:07):

They may have no extension if we want roc to be used for scripting

view this post on Zulip Brendan Hansknecht (Jan 23 2024 at 02:07):

But probably just that and .roc is good

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

Why would a roc file have no extension?

view this post on Zulip Brendan Hansknecht (Jan 23 2024 at 02:26):

Cause when making a script it is nice to just write app-name or ./app-name. For example a lot of shell scripts omit the extension instead of app-name.sh, it is just app-name

view this post on Zulip Richard Feldman (Jan 23 2024 at 04:43):

or sometimes you have a third-party tool which runs a hardcoded executable name (e.g. one it expects to be on your PATH) and you want to have it run your script

view this post on Zulip Luke Boswell (Jan 23 2024 at 04:51):

Ok, thanks for explaining that. I'll just leave this as is for now then.

view this post on Zulip Brendan Hansknecht (Jan 23 2024 at 05:02):

I think we should totally give an error for anything that is a .not-roc

view this post on Zulip Brendan Hansknecht (Jan 23 2024 at 05:03):

So either no extension or specifically just .roc

view this post on Zulip Luke Boswell (Jan 23 2024 at 05:43):

Ok, I'll make an issue

view this post on Zulip Luke Boswell (Jan 23 2024 at 05:48):

https://github.com/roc-lang/roc/issues/6418

view this post on Zulip Brian Carroll (Jan 23 2024 at 07:47):

If the filename has no extension, we could require the first line to start with#! and contain roc somewhere?

view this post on Zulip Brendan Hansknecht (Jan 23 2024 at 15:36):

Yeah, that's a good idea

view this post on Zulip Kevin Gillette (Jan 24 2024 at 15:00):

Are we sure someone won't legitimately rename their interpreter?

view this post on Zulip Kevin Gillette (Jan 24 2024 at 15:18):

we can certainly enforce that the first line for an extensionless file starts with #!, though I'm not sure that's necessary either?

Do bourne shells enforce extensions or having a shebang line? sh doesn't parse the whole file before executing, but rather just parses the next statement (or if it's something like a loop or conditional, just parses until the done or fi before executing that block of code. Granted, that's somewhat of a historical feature of shells of that kind, because you could have an executive #!/bin/sh file that checks, for example, if unzip is installed, and if not, installs it, and then runs unzip on the same file.

Since Roc parses the whole file before any code will get executed, the chance of an arbitrary non-Roc file being valid Roc code is pretty low.
I feel like this is better handled via clear errors rather than up-front enforcement: when you attempt to run, and there are any syntax errors, check if the extension is .roc or there was a shebang line containing roc, and if not, crash with a message like:

You attempted to run this file with Roc, but it does not appear to be a *.roc or #!/.../roc file.

If this is actually a Roc file, please run with:
roc run --all-errors relpath/to/this-file

view this post on Zulip Kevin Gillette (Jan 24 2024 at 15:19):

If instead, the file does not have syntax errors, but does have other errors (like a type error), then treat it as a Roc file despite having an unusual file extension or shebang line.


Last updated: Jun 16 2026 at 16:19 UTC