Skip to content

Commit 210657a

Browse files
authored
Fix using directives not working with the shebang line in .scala files (#1639)
1 parent 79e7380 commit 210657a

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,9 @@ case object ScalaPreprocessor extends Preprocessor {
187187
maybeRecoverOnError: BuildException => Option[BuildException],
188188
allowRestrictedFeatures: Boolean
189189
): Either[BuildException, Option[ProcessingOutput]] = either {
190+
val (contentWithNoShebang, _) = SheBang.ignoreSheBangLines(content)
190191
val extractedDirectives = value(ExtractedDirectives.from(
191-
content.toCharArray,
192+
contentWithNoShebang.toCharArray,
192193
path,
193194
logger,
194195
UsingDirectiveKind.values(),
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package scala.build.tests
2+
3+
import com.eed3si9n.expecty.Expecty.expect
4+
5+
import scala.build.input.SourceScalaFile
6+
import scala.build.preprocessing.{PreprocessedSource, ScalaPreprocessor}
7+
8+
class ScalaPreprocessorTests extends munit.FunSuite {
9+
10+
test("should respect using directives in a .scala file with the shebang line") {
11+
TestInputs(os.rel / "Main.scala" ->
12+
"""#!/usr/bin/env -S scala-cli shebang
13+
|//> using lib "com.lihaoyi::os-lib::0.8.1"
14+
|
15+
|object Main {
16+
| def main(args: Array[String]): Unit = {
17+
| println(os.pwd)
18+
| }
19+
|}""".stripMargin).fromRoot { root =>
20+
val scalaFile = SourceScalaFile(root, os.sub / "Main.scala")
21+
val Some(Right(result)) = ScalaPreprocessor.preprocess(
22+
scalaFile,
23+
logger = TestLogger(),
24+
allowRestrictedFeatures = false
25+
)
26+
expect(result.nonEmpty)
27+
val Some(directivesPositions) = result.head.directivesPositions
28+
expect(directivesPositions.specialCommentDirectives.startPos == 0 -> 0)
29+
expect(directivesPositions.specialCommentDirectives.endPos == 3 -> 0)
30+
}
31+
}
32+
33+
test("should respect using directives in a .sc file with the shebang line") {
34+
TestInputs(os.rel / "sample.sc" ->
35+
"""#!/usr/bin/env -S scala-cli shebang
36+
|//> using lib "com.lihaoyi::os-lib::0.8.1"
37+
|println(os.pwd)
38+
|""".stripMargin).fromRoot { root =>
39+
val scalaFile = SourceScalaFile(root, os.sub / "sample.sc")
40+
val Some(Right(result)) = ScalaPreprocessor.preprocess(
41+
scalaFile,
42+
logger = TestLogger(),
43+
allowRestrictedFeatures = false
44+
)
45+
expect(result.nonEmpty)
46+
val Some(directivesPositions) = result.head.directivesPositions
47+
expect(directivesPositions.specialCommentDirectives.startPos == 0 -> 0)
48+
expect(directivesPositions.specialCommentDirectives.endPos == 2 -> 0)
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)