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

script files (.sc) do not strip multiline shebang #2901

Closed
bishabosha opened this issue May 10, 2024 · 0 comments · Fixed by #2908
Closed

script files (.sc) do not strip multiline shebang #2901

bishabosha opened this issue May 10, 2024 · 0 comments · Fixed by #2908
Labels
bug Something isn't working missing feature Something should have been implemented, but wasn't. scripting Issues tied to *.sc script inputs.

Comments

@bishabosha
Copy link
Contributor

Version(s)
1.3.1

Describe the bug
If a script file (.sc) has a multiline shebang, then only the first line is stripped. Meaning the rest of the shebang is parsed as code by the Scala Compiler, leading to errors.

To Reproduce

make a file shebangTest.sc with the following content, and then run it with scala-cli run shebangTest.sc

#!/usr/bin/env fake-program-to-test-shebang-removal
# comment
STUFF=nada
!#
// everything above this point should be ignored by the compiler
def main(args: Array[String]): Unit =
  args.zipWithIndex.foreach { case (arg,i) => printf("arg %d: [%s]\n",i,arg) }
  System.err.printf("mainClassFromStack: %s\n",mainFromStack)
  assert(mainFromStack.contains("sheBang"),s"fromStack[$mainFromStack]")

  lazy val mainFromStack:String = {
    val result = new java.io.StringWriter()
    new RuntimeException("stack").printStackTrace(new java.io.PrintWriter(result))
    val stack = result.toString.split("[\r\n]+").toList
    if verbose then for( s <- stack ){ System.err.printf("[%s]\n",s) }
    stack.filter { str => str.contains(".main(") }.map {
      // derive main class name from stack when main object is NOT declared in source
      _.replaceAll("[.].*","").
      replaceAll("\\s+at\\s+","")
    }.distinct.take(1).mkString("")
  }

  lazy val verbose = Option(System.getenv("DOTC_VERBOSE")) match
    case None => false
    case _ => true

you should get errors like so:

-- Error: /Users/jamie/workspace/dotty/compiler/test-resources/scripting/.scala-build/scripting_6199f560ec-7ad3f4e164/src_generated/main/sheBang.scala:8:0
8 |# comment
  |^
  |end of statement expected but '#' found
-- [E006] Not Found Error: /Users/jamie/workspace/dotty/compiler/test-resources/scripting/.scala-build/scripting_6199f560ec-7ad3f4e164/src_generated/main/sheBang.scala:9:0
9 |STUFF=nada
  |^^^^^
  |Not found: STUFF
  |
  | longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: /Users/jamie/workspace/dotty/compiler/test-resources/scripting/.scala-build/scripting_6199f560ec-7ad3f4e164/src_generated/main/sheBang.scala:9:6
9 |STUFF=nada
  |      ^^^^
  |      Not found: nada
  |
  | longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: /Users/jamie/workspace/dotty/compiler/test-resources/scripting/.scala-build/scripting_6199f560ec-7ad3f4e164/src_generated/main/sheBang.scala:10:0
10 |!#
   |^^
   |Not found: !# - did you mean !=? or perhaps != or ## or ##?
   |
   | longer explanation available when compiling with `-explain`
4 errors found
Compilation failed

Expected behaviour
no errors, because the shebang should be stripped by scala-cli when wrapping the script source

@bishabosha bishabosha added the bug Something isn't working label May 10, 2024
@Gedochao Gedochao added scripting Issues tied to *.sc script inputs. missing feature Something should have been implemented, but wasn't. labels May 10, 2024
@Gedochao Gedochao linked a pull request May 15, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working missing feature Something should have been implemented, but wasn't. scripting Issues tied to *.sc script inputs.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants