scalafmt icon indicating copy to clipboard operation
scalafmt copied to clipboard

Support shebang in .scala files with the Scala 3 dialect

Open rmgk opened this issue 3 years ago • 3 comments

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

rmgk avatar Jul 02 '22 11:07 rmgk

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

kitbellew avatar Jul 02 '22 15:07 kitbellew

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 avatar Jul 03 '22 16:07 rmgk

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).

kitbellew avatar Jul 03 '22 17:07 kitbellew