Skip to content

Commit 508840e

Browse files
committed
fix for 3725, with tests to verify this fix, passing in WSL
1 parent bb9c9f8 commit 508840e

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

modules/build/src/main/scala/scala/build/preprocessing/SheBang.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package scala.build.preprocessing
33
import scala.util.matching.Regex
44

55
object SheBang {
6-
private val sheBangRegex: Regex = s"""(^(#!.*(\\X)?)+(\\X*!#.*)?)""".r
6+
private val sheBangRegex: Regex = s"""(^(#!.*(\\X)?)+(\\X*^!#.*)?)""".r
77

88
def isShebangScript(content: String): Boolean = sheBangRegex.unanchored.matches(content)
99

modules/build/src/test/scala/scala/build/tests/SourcesTests.scala

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,14 @@ class SourcesTests extends TestUtil.ScalaCliBuildSuite {
325325
|VAL=1
326326
|!#
327327
|
328-
|println("Hello World #!")""".stripMargin
328+
|println("Hello World #!")""".stripMargin,
329+
os.rel / "hasBangHashInComment.sc" ->
330+
"""#!/usr/bin/scala-cli
331+
|
332+
|
333+
|
334+
|
335+
|println("Hello World !#")""".stripMargin
329336
)
330337
val expectedParsedCodes = Seq(
331338
"""
@@ -349,7 +356,14 @@ class SourcesTests extends TestUtil.ScalaCliBuildSuite {
349356
|
350357
|
351358
|
352-
|println("Hello World #!")""".stripMargin
359+
|println("Hello World #!")""".stripMargin,
360+
"""
361+
|
362+
|
363+
|
364+
|
365+
|
366+
|println("Hello World !#")""".stripMargin
353367
)
354368

355369
testInputs.withInputs { (root, inputs) =>

modules/build/src/test/scala/scala/build/tests/markdown/MarkdownCodeBlockTests.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,28 @@ class MarkdownCodeBlockTests extends TestUtil.ScalaCliBuildSuite {
6969
expect(actualResult == expectedResult)
7070
}
7171

72+
test("shebang closing line allowed in scala code") {
73+
val code = """println("Hello !#")""".stripMargin
74+
val markdown =
75+
s"""# Some snippet
76+
|
77+
|```scala
78+
|#!/usr/bin/env -S scala-cli shebang
79+
|$code
80+
|```
81+
|""".stripMargin
82+
val expectedResult =
83+
MarkdownCodeBlock(
84+
info = PlainScalaInfo,
85+
body = "\n" + code,
86+
startLine = 3,
87+
endLine = 4
88+
)
89+
val Right(Seq(actualResult: MarkdownCodeBlock)) =
90+
MarkdownCodeBlock.findCodeBlocks(os.sub / "Example.md", markdown)
91+
expect(actualResult == expectedResult)
92+
}
93+
7294
test("a raw Scala code block is extracted correctly from markdown") {
7395
val code = """object Main extends App {
7496
| println("Hello")

0 commit comments

Comments
 (0)