Skip to content

Commit 9240d74

Browse files
philwalkGedochao
andauthored
fix for 3789 - updated (#3794)
* fix for 3789 - script if .sc, or (if no extension) has shebang * verify-non-directory in shebang-test * reorder isDir case ahead of isShebangScript * added integration test for enabled case * restore line endings * adjust new test * quote msg * fix quoted msg * Apply suggestions from code review suggested changes Co-authored-by: Piotr Chabelski <[email protected]> * remove isScript extra paren --------- Co-authored-by: Piotr Chabelski <[email protected]>
1 parent cfa254c commit 9240d74

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

modules/build/src/main/scala/scala/build/CrossSources.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ object CrossSources {
436436
else if (sourcePath == os.sub / Constants.projectFileName)
437437
Right(Seq(ProjectScalaFile(dir, subPath)))
438438
else if (sourcePath.ext == "scala") Right(Seq(SourceScalaFile(dir, subPath)))
439-
else if (sourcePath.ext == "sc") Right(Seq(Script(dir, subPath, None)))
439+
else if (sourcePath.isScript) Right(Seq(Script(dir, subPath, None)))
440440
else if (sourcePath.ext == "java") Right(Seq(JavaFile(dir, subPath)))
441441
else if (sourcePath.ext == "jar") Right(Seq(JarFile(dir, subPath)))
442442
else if (sourcePath.ext == "md") Right(Seq(MarkdownFile(dir, subPath)))

modules/build/src/main/scala/scala/build/input/ElementsUtils.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import scala.build.Directories
88
import scala.build.internal.Constants
99

1010
object ElementsUtils {
11+
extension (p: os.Path) {
12+
def hasShebang: Boolean = os.read.bytes(p, offset = 0, count = 2) == Array('#', '!')
13+
def isScript: Boolean = p.ext == "sc" || (p.hasShebang && p.ext != "scala")
14+
}
15+
1116
extension (d: Directory) {
1217
def singleFilesFromDirectory(enableMarkdown: Boolean): Seq[SingleFile] = {
1318
import Ordering.Implicits.seqOrdering
@@ -20,7 +25,7 @@ object ElementsUtils {
2025
ProjectScalaFile(d.path, p.subRelativeTo(d.path))
2126
case p if p.last.endsWith(".scala") =>
2227
SourceScalaFile(d.path, p.subRelativeTo(d.path))
23-
case p if p.last.endsWith(".sc") =>
28+
case p if p.last.endsWith(".sc") || p.hasShebang =>
2429
Script(d.path, p.subRelativeTo(d.path), None)
2530
case p if p.last.endsWith(".c") || p.last.endsWith(".h") =>
2631
CFile(d.path, p.subRelativeTo(d.path))

modules/build/src/main/scala/scala/build/input/Inputs.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,14 @@ object Inputs {
275275
}
276276
else if path.last == Constants.projectFileName then
277277
Right(Seq(ProjectScalaFile(dir, subPath)))
278-
else if arg.endsWith(".sc") then Right(Seq(Script(dir, subPath, Some(arg))))
278+
else if os.isDir(path) then Right(Seq(Directory(path)))
279+
else if arg.endsWith(".sc") || (os.exists(path) && isShebangScript(String(content))) then
280+
Right(Seq(Script(dir, subPath, Some(arg))))
279281
else if arg.endsWith(".scala") then Right(Seq(SourceScalaFile(dir, subPath)))
280282
else if arg.endsWith(".java") then Right(Seq(JavaFile(dir, subPath)))
281283
else if arg.endsWith(".jar") then Right(Seq(JarFile(dir, subPath)))
282284
else if arg.endsWith(".c") || arg.endsWith(".h") then Right(Seq(CFile(dir, subPath)))
283285
else if arg.endsWith(".md") then Right(Seq(MarkdownFile(dir, subPath)))
284-
else if os.isDir(path) then Right(Seq(Directory(path)))
285286
else if acceptFds && arg.startsWith("/dev/fd/") then
286287
Right(Seq(VirtualScript(content, arg, os.sub / s"input-${idx + 1}.sc")))
287288
else if programInvokeData.subCommand == SubCommand.Shebang && os.exists(path) then

modules/integration/src/test/scala/scala/cli/integration/RunScriptTestDefinitions.scala

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,21 @@ trait RunScriptTestDefinitions { _: RunTestDefinitions =>
573573
}
574574
}
575575

576+
test("script file with shebang header and no extension run with scala-cli") {
577+
val msg = "compiled and ran"
578+
val inputs = TestInputs(
579+
os.rel / "scriptWithShebang" ->
580+
s"""|#!/usr/bin/env -S ${TestUtil.cli.mkString(" ")} shebang
581+
|//> using scala $actualScalaVersion
582+
|println(s"$msg")""".stripMargin
583+
)
584+
inputs.fromRoot { root =>
585+
val output = os.proc(TestUtil.cli, "scriptWithShebang")
586+
.call(cwd = root).out.trim()
587+
expect(output == msg)
588+
}
589+
}
590+
576591
test("script file with shebang header and no extension run with scala-cli shebang") {
577592
val inputs = TestInputs(
578593
os.rel / "script-with-shebang" ->
@@ -591,7 +606,6 @@ trait RunScriptTestDefinitions { _: RunTestDefinitions =>
591606
expect(output == "List(1, 2, 3, -v)")
592607
}
593608
}
594-
595609
test("script file with NO shebang header and no extension run with scala-cli shebang") {
596610
val inputs = TestInputs(
597611
os.rel / "script-no-shebang" ->

0 commit comments

Comments
 (0)