Skip to content

support in scalac for ignoring hash-bang line in script files #10858

Closed
@philwalk

Description

@philwalk

The current SNAPSHOT reports a "Syntax Error" when compiling a script file with a hash-bang on the first line.
The scalac version tested is:

version:=3.0.0-M3-bin-SNAPSHOT
revision:=73d942cca5936e3f760be327a5a4d6ee9f9c194f
buildTime:=2020-12-16 12:52:04-0700

For example, with the following script file and compile command:

Script file s3new.sc:

#!/usr/bin/env scala3.sh

object S3new {
  //@main def main = printf("YoYo!\n") // does not work, as of dotty-0.27.0-RC1
  def main(args:Array[String]):Unit =
    printf("Yo!\n")
}
$ bin/scalac -color:never -script s3new.sc
+++ dirname bin/scalac
++ cd bin
++ pwd
+ ROOT=/Users/philwalk/workspace/dotty/bin/..
+ /Users/philwalk/workspace/dotty/bin/../bin/common /Users/philwalk/workspace/dotty/bin/../dist/target/pack/bin/scalac -color:never -script s3new.sc
-- [E080] Syntax Error: C:\Users\philwalk\workspace\dotty\s3new.sc:1:0 ---------
1 |#!/usr/bin/env scala3.sh
  |^^^
  |Expected a toplevel definition
1 error found
Error: Errors encountered during compilation

I noticed some references to #! in some source files, but am not yet familiar with the compiler, so it's not clear if this feature is already in the plan:

dotty/tools/dotc/util/SourceFile.scala:23:  private val headerStarts  = List("#!", "::#!")
dotty/tools/dotc/util/SourceFile.scala:27:     *  The header begins with "#!" or "::#!" and ends with a line starting
dotty/scala-backend/src/compiler/scala/tools/cmd/Interpolation.scala:53:    |#!/bin/sh
dotty/scala-backend/src/compiler/scala/tools/nsc/ScriptRunner.scala:18: *    #!/bin/sh
dotty/scala-backend/src/compiler/scala/tools/nsc/ScriptRunner.scala:26: *    ::#!

Without this compiler feature, it becomes unavoidable to make filtered copies of script sources, with compiler error messages referring the the copy rather than the original source file.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions