roc-lang / roc

A fast, friendly, functional language.
https://roc-lang.org
Universal Permissive License v1.0
4.21k stars 303 forks source link

Parsing comments before module headers #1135

Open rtfeldman opened 3 years ago

rtfeldman commented 3 years ago

Currently, the parser fails when trying to parse a module that has a comment before its module header.

This is unfortunate because if it worked, we could have "roc scripts" on UNIX systems as easily as making this the first line in the module:

#!/usr/bin/env roc run

...and then marking Example.roc executable. I verified that this technique works, except that whenever you execute Example.roc, it immediately crashes with a parse error. 😄

Ivo-Balbaert commented 1 year ago

For this Example.roc program:

#!/usr/bin/env roc
# comment abc
# comment cde
app "helloWorld"
    packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.4.0/DI4lqn7LIZs8ZrCDUgLK-tHHpQmxGF1ZrlevRKq5LXk.tar.br" }
    imports [pf.Stdout]
    provides [main] to pf

main =
    Stdout.line "Hello, World!"

making it executable and running with ./Example.roc works (Ubuntu 22).

(However roc run doesn't work: /usr/bin/env: ‘roc run’: No such file or directory /usr/bin/env: use -[v]S to pass options in shebang lines)

Perhaps this bug can be declared as solved and the issue closed? @Anton-4 ?

lukewilliamboswell commented 6 months ago

No longer reproducible.

Tested using

#!/usr/bin/env roc
# Some comment
app ""
    packages {
        pf: "https://github.com/roc-lang/basic-cli/releases/download/0.8.1/x8URkvfyi9I0QhmVG98roKBUs_AZRkLFwFJVJ3942YA.tar.br",
    }
    imports [pf.Stdout]
    provides [main] to pf

main = Stdout.line "Hi"
13:37:26 ~/Documents/GitHub/roc main $ chmod +x test.roc           
15:13:47 ~/Documents/GitHub/roc main $ ./test.roc
Hi
evanrelf commented 5 months ago

I think the compiler has regressed on this. I'm using Roc built with Nix from https://github.com/roc-lang/roc/commit/b8d9367029710e76205381b6bac36ef01c498604.

Taking Luke's example above verbatim:

#!/usr/bin/env roc
# Some comment
app ""
    packages {
        pf: "https://github.com/roc-lang/basic-cli/releases/download/0.8.1/x8URkvfyi9I0QhmVG98roKBUs_AZRkLFwFJVJ3942YA.tar.br",
    }
    imports [pf.Stdout]
    provides [main] to pf

main = Stdout.line "Hi"

Things work fine:

$ ./test.roc
Hi

But formatting the file changes it to a new syntax:

# !/usr/bin/env roc
# Some comment
app [main] {
    pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.8.1/x8URkvfyi9I0QhmVG98roKBUs_AZRkLFwFJVJ3942YA.tar.br",
}

import pf.Stdout

main = Stdout.line "Hi"

And running that doesn't work:

$ ./test.roc
./test.roc: line 3: app: command not found
./test.roc: line 4: pf:: command not found
./test.roc: line 5: syntax error near unexpected token `}'
./test.roc: line 5: `}'

It seems the reason is because the formatter adds a space after the first # in the shebang, and roc isn't expecting that, so it stops treating it like a script. Removing that space fixes things.