Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support shebang in .scala files with the Scala 3 dialect #3267

Closed
rmgk opened this issue Jul 2, 2022 · 3 comments · Fixed by #3353
Closed

Support shebang in .scala files with the Scala 3 dialect #3267

rmgk opened this issue Jul 2, 2022 · 3 comments · Fixed by #3353

Comments

@rmgk
Copy link

rmgk commented Jul 2, 2022

Configuration (required)

version = 3.5.8
runner.dialect = scala3

Steps

Given code like this:

#!/usr/bin/env scala

@main def run() = println("hello world!")

Problem

Scalafmt formats code like this:

.../main.scala:1: error: [dialect scala3] illegal start of definition identifier
#!/usr/bin/env scala
^

Expectation

The file is legal scala3 (can be run with scala main.scala and compiled with scalac main.scala), and should be formatted while ignoring the first line.

Notes

There seems to be supported for ammonite scripts, or rather anything with a .sc file ending

@kitbellew
Copy link
Collaborator

according to the blog below, there's no good solution: https://www.codementor.io/@martinbrosenberg/shello-world-writing-a-scala-script-z8qg4i7d4

@rmgk
Copy link
Author

rmgk commented Jul 3, 2022

This bug report is about the fact that Scala 3 supports shebang lines in source files (in the sense that they are just ignored by the compiler), but scalafmt does not, thus is unable to format such Scala source files even though they are accepted by the compiler.
I updated the title to make that more clear.

The blog is about the issue that not all all tools support shebang lines for Scala 2.
This has drastically improved since 2019, with scalafmt being the only holdout of the tools I use.

@rmgk rmgk changed the title Support shebang in .scala files Support shebang in .scala files with the Scala 3 dialect. Jul 3, 2022
@rmgk rmgk changed the title Support shebang in .scala files with the Scala 3 dialect. Support shebang in .scala files with the Scala 3 dialect Jul 3, 2022
@kitbellew
Copy link
Collaborator

please include a link to documentation. I'd prefer not to revisit this issue if it turns out that such scripts are handled even more permissively. for instance, the blog seems to suggest that scripts don't require a top level object or class (which is not the same as simply ignoring).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants