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

scalafmt --check fails with an error on scripts with valid hashbang lines #3642

Closed
philwalk opened this issue Sep 26, 2023 · 0 comments · Fixed by #3657
Closed

scalafmt --check fails with an error on scripts with valid hashbang lines #3642

philwalk opened this issue Sep 26, 2023 · 0 comments · Fixed by #3657

Comments

@philwalk
Copy link

philwalk commented Sep 26, 2023

Configuration (required)

version = 3.7.14
preset = default
align.preset = more
maxColumn = 200
assumeStandardLibraryStripMargin = true
align.stripMargin = true
runner.dialect = Scala3
fileOverride {
  "glob:**/scala-3.3.1/**" {
    runner.dialect = scala3
  }
}

NB: before submitting, please confirm that the problem is observed in the
latest published version of the formatter! We don't publish hotfixes for older
versions, and the problem you have observed in an older version may have already
been fixed.

Command-line parameters (required)

When I run scalafmt via CLI like this: scalafmt --check

Steps

Any directory with scripts having the following hashbang line causes problems:

#!/usr/bin/env -S scala @classpathAtfile
def main(args: Array[String]): Unit =
   println("hello")

Problem

Scalafmt rejects the hashbang line, complaining about the @ character:

Looking for unformatted files...
<input>:1: error: [dialect scala3] ; expected but @ found
#!/usr/bin/env -S scala @classpathAtfile
                        ^
<input>:1: error: [dialect scala3] ; expected but @ found
#!/usr/bin/env -S scala @classpathAtfile
                        ^
<input>:1: error: [dialect scala3] ; expected but @ found
#!/usr/bin/env -S scala @classpathAtfile
                        ^
<input>:1: error: [dialect scala3] ; expected but @ found
#!/usr/bin/env -S scala @classpathAtfile
                        ^
<input>:1: error: [dialect scala3] ; expected but @ found
#!/usr/bin/env -S scala @classpathAtfile
                        ^
<input>:1: error: [dialect scala3] ; expected but @ found
#!/usr/bin/env -S scala @classpathAtfile
                        ^
error: --test failed

Another possibly related rejection is the following script hashbang:

scalafmt --check ./biz.sc
<input>:1: error: [dialect scala3] ; expected but double constant found
#!/usr/bin/env -S scala -cp target/scala-3.3.1/classes

error: ParseError=2                                            ^

philwalk@d5 MINGW64 ~/work

Expectation

If the file ./classpathAtfile is not found, a warning is printed, and in any case, as long as scala in the PATH is scala 3, the script prints hello.

It's also necessary that /usr/bin/env accept the -S option, as is the case for recent versions.
The result is equivalent to invoking the script via the following command line:

scala @classpathAtfile biz.sc

In this simple hello world example, the file classpathAtfile can be empty, and need not exist. In general, it's a way to set the classpath.

Workaround

On the command line, pass a list of all directories other than those with the offending script hashbang lines.
A better workaround would be to be able to exclude a directory in .scalafmt.conf, or similar.

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.

1 participant