Skip to content

Commit 66ea06b

Browse files
authored
Add support for the -with-compiler runner option (#1780)
1 parent 445c0b8 commit 66ea06b

File tree

10 files changed

+145
-0
lines changed

10 files changed

+145
-0
lines changed

modules/cli/src/main/scala/scala/cli/commands/shared/SharedOptions.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ final case class SharedOptions(
147147
@Tag(tags.implementation)
148148
@Hidden
149149
scalaLibrary: Option[Boolean] = None,
150+
@Group("Scala")
151+
@HelpMessage("Allows to include the Scala compiler artifacts on the classpath.")
152+
@Tag(tags.must)
153+
@Name("withScalaCompiler")
154+
@Name("-with-compiler")
155+
withCompiler: Option[Boolean] = None,
150156
@Group("Java")
151157
@HelpMessage("Do not add dependency to Scala Standard library. This is useful, when Scala CLI works with pure Java projects.")
152158
@Tag(tags.implementation)
@@ -281,6 +287,7 @@ final case class SharedOptions(
281287
.map(bo.MaybeScalaVersion(_)),
282288
scalaBinaryVersion = scalaBinaryVersion.map(_.trim).filter(_.nonEmpty),
283289
addScalaLibrary = scalaLibrary.orElse(java.map(!_)),
290+
addScalaCompiler = withCompiler,
284291
generateSemanticDbs = semanticDb,
285292
scalacOptions = scalac
286293
.scalacOption

modules/cli/src/main/scala/scala/cli/exportCmd/Mill.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ final case class Mill(
3232
private def scalaVersionSettings(options: BuildOptions, sources: Sources): MillProject = {
3333

3434
val pureJava = !options.scalaOptions.addScalaLibrary.contains(true) &&
35+
!options.scalaOptions.addScalaCompiler.contains(true) &&
3536
sources.paths.forall(_._1.last.endsWith(".java")) &&
3637
sources.inMemory.forall(_.generatedRelPath.last.endsWith(".java")) &&
3738
options.classPathOptions.extraDependencies.toSeq

modules/cli/src/main/scala/scala/cli/exportCmd/Sbt.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ final case class Sbt(
3838
private def pureJavaSettings(options: BuildOptions, sources: Sources): SbtProject = {
3939

4040
val pureJava = !options.scalaOptions.addScalaLibrary.contains(true) &&
41+
!options.scalaOptions.addScalaCompiler.contains(true) &&
4142
sources.paths.forall(_._1.last.endsWith(".java")) &&
4243
sources.inMemory.forall(_.generatedRelPath.last.endsWith(".java")) &&
4344
options.classPathOptions.extraDependencies.toSeq

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,4 +540,32 @@ abstract class CompileTestDefinitions(val scalaVersionOpt: Option[String])
540540
expect(classFiles.contains(os.rel / "Hello.class"))
541541
}
542542
}
543+
544+
private def compilerArtifactName: String =
545+
if (actualScalaVersion.startsWith("3")) "scala3-compiler" else "scala-compiler"
546+
547+
test(s"ensure the -with-compiler option adds $compilerArtifactName to the classpath") {
548+
TestInputs(os.rel / "s.sc" -> """println("Hello")""")
549+
.fromRoot { root =>
550+
val compileRes = os.proc(
551+
TestUtil.cli,
552+
"compile",
553+
"s.sc",
554+
"--print-classpath",
555+
extraOptions
556+
)
557+
.call(cwd = root)
558+
expect(!compileRes.out.trim().contains(compilerArtifactName))
559+
val compileWithCompilerRes = os.proc(
560+
TestUtil.cli,
561+
"compile",
562+
"s.sc",
563+
"-with-compiler",
564+
"--print-classpath",
565+
extraOptions
566+
)
567+
.call(cwd = root)
568+
expect(compileWithCompilerRes.out.trim().contains(compilerArtifactName))
569+
}
570+
}
543571
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,4 +466,29 @@ trait RunScalacCompatTestDefinitions { _: RunTestDefinitions =>
466466
}
467467
}
468468
}
469+
470+
if (actualScalaVersion.startsWith("3"))
471+
test("ensure -with-compiler is supported for Scala 3") {
472+
TestInputs(
473+
os.rel / "s.sc" ->
474+
"println(dotty.tools.dotc.config.Properties.simpleVersionString)"
475+
)
476+
.fromRoot { root =>
477+
val res =
478+
os.proc(TestUtil.cli, "-with-compiler", "s.sc", extraOptions)
479+
.call(cwd = root)
480+
expect(res.out.trim() == actualScalaVersion)
481+
}
482+
}
483+
484+
if (actualScalaVersion.startsWith("2.13"))
485+
test("ensure -with-compiler is supported for Scala 2.13") {
486+
TestInputs(os.rel / "s.sc" -> "println(scala.tools.nsc.Properties.versionString)")
487+
.fromRoot { root =>
488+
val res =
489+
os.proc(TestUtil.cli, "-with-compiler", "s.sc", extraOptions)
490+
.call(cwd = root)
491+
expect(res.out.trim() == s"version $actualScalaVersion")
492+
}
493+
}
469494
}

modules/options/src/main/scala/scala/build/options/BuildOptions.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ final case class BuildOptions(
8686
}
8787
}
8888

89+
private def scalaCompilerDependencies: Either[BuildException, Seq[AnyDependency]] = either {
90+
value(scalaParams).toSeq.flatMap { sp =>
91+
if (scalaOptions.addScalaCompiler.getOrElse(false))
92+
Seq(
93+
if (sp.scalaVersion.startsWith("3."))
94+
dep"org.scala-lang::scala3-compiler::${sp.scalaVersion}"
95+
else
96+
dep"org.scala-lang:scala-compiler:${sp.scalaVersion}"
97+
)
98+
else Nil
99+
}
100+
}
101+
89102
private def maybeJsDependencies: Either[BuildException, Seq[AnyDependency]] = either {
90103
if (platform.value == Platform.JS)
91104
value(scalaParams).toSeq.flatMap { scalaParams0 =>
@@ -104,6 +117,7 @@ final case class BuildOptions(
104117
value(maybeJsDependencies).map(Positioned.none(_)) ++
105118
value(maybeNativeDependencies).map(Positioned.none(_)) ++
106119
value(scalaLibraryDependencies).map(Positioned.none(_)) ++
120+
value(scalaCompilerDependencies).map(Positioned.none(_)) ++
107121
classPathOptions.extraDependencies.toSeq
108122
}
109123

modules/options/src/main/scala/scala/build/options/ScalaOptions.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ final case class ScalaOptions(
88
scalaVersion: Option[MaybeScalaVersion] = None,
99
scalaBinaryVersion: Option[String] = None,
1010
addScalaLibrary: Option[Boolean] = None,
11+
addScalaCompiler: Option[Boolean] = None,
1112
generateSemanticDbs: Option[Boolean] = None,
1213
scalacOptions: ShadowingSeq[Positioned[ScalacOpt]] = ShadowingSeq.empty,
1314
extraScalaVersions: Set[String] = Set.empty,

website/docs/reference/cli-options.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,12 @@ Specify platform
13791379
### `--scala-library`
13801380

13811381
[Internal]
1382+
### `--with-compiler`
1383+
1384+
Aliases: `-with-compiler`, `--with-scala-compiler`
1385+
1386+
Allows to include the Scala compiler artifacts on the classpath.
1387+
13821388
### `--java`
13831389

13841390
[Internal]

website/docs/reference/scala-command/cli-options.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,14 @@ Specify platform
926926

927927
`IMPLEMENTATION specific` per Scala Runner specification
928928

929+
### `--with-compiler`
930+
931+
Aliases: `-with-compiler`, `--with-scala-compiler`
932+
933+
`MUST have` per Scala Runner specification
934+
935+
Allows to include the Scala compiler artifacts on the classpath.
936+
929937
### `--java`
930938

931939
`IMPLEMENTATION specific` per Scala Runner specification

website/docs/reference/scala-command/runner-specification.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ Add a resource directory
8686

8787
Aliases: `--resource-dir`
8888

89+
**--with-compiler**
90+
91+
Allows to include the Scala compiler artifacts on the classpath.
92+
93+
Aliases: `-with-compiler` ,`--with-scala-compiler`
94+
8995
**--compilation-output**
9096

9197
Copy compilation results to output directory using either relative or absolute path
@@ -597,6 +603,12 @@ Add a resource directory
597603

598604
Aliases: `--resource-dir`
599605

606+
**--with-compiler**
607+
608+
Allows to include the Scala compiler artifacts on the classpath.
609+
610+
Aliases: `-with-compiler` ,`--with-scala-compiler`
611+
600612
**--compilation-output**
601613

602614
Copy compilation results to output directory using either relative or absolute path
@@ -1112,6 +1124,12 @@ Add a resource directory
11121124

11131125
Aliases: `--resource-dir`
11141126

1127+
**--with-compiler**
1128+
1129+
Allows to include the Scala compiler artifacts on the classpath.
1130+
1131+
Aliases: `-with-compiler` ,`--with-scala-compiler`
1132+
11151133
**--compilation-output**
11161134

11171135
Copy compilation results to output directory using either relative or absolute path
@@ -1641,6 +1659,12 @@ Add a resource directory
16411659

16421660
Aliases: `--resource-dir`
16431661

1662+
**--with-compiler**
1663+
1664+
Allows to include the Scala compiler artifacts on the classpath.
1665+
1666+
Aliases: `-with-compiler` ,`--with-scala-compiler`
1667+
16441668
**--compilation-output**
16451669

16461670
Copy compilation results to output directory using either relative or absolute path
@@ -2208,6 +2232,12 @@ Add a resource directory
22082232
22092233
Aliases: `--resource-dir`
22102234
2235+
**--with-compiler**
2236+
2237+
Allows to include the Scala compiler artifacts on the classpath.
2238+
2239+
Aliases: `-with-compiler` ,`--with-scala-compiler`
2240+
22112241
**--compilation-output**
22122242
22132243
Copy compilation results to output directory using either relative or absolute path
@@ -2755,6 +2785,12 @@ Add a resource directory
27552785
27562786
Aliases: `--resource-dir`
27572787
2788+
**--with-compiler**
2789+
2790+
Allows to include the Scala compiler artifacts on the classpath.
2791+
2792+
Aliases: `-with-compiler` ,`--with-scala-compiler`
2793+
27582794
**--compilation-output**
27592795
27602796
Copy compilation results to output directory using either relative or absolute path
@@ -3312,6 +3348,12 @@ Add a resource directory
33123348
33133349
Aliases: `--resource-dir`
33143350
3351+
**--with-compiler**
3352+
3353+
Allows to include the Scala compiler artifacts on the classpath.
3354+
3355+
Aliases: `-with-compiler` ,`--with-scala-compiler`
3356+
33153357
**--compilation-output**
33163358
33173359
Copy compilation results to output directory using either relative or absolute path
@@ -3922,6 +3964,12 @@ Add a resource directory
39223964
39233965
Aliases: `--resource-dir`
39243966
3967+
**--with-compiler**
3968+
3969+
Allows to include the Scala compiler artifacts on the classpath.
3970+
3971+
Aliases: `-with-compiler` ,`--with-scala-compiler`
3972+
39253973
**--compilation-output**
39263974
39273975
Copy compilation results to output directory using either relative or absolute path
@@ -4729,6 +4777,12 @@ Add a resource directory
47294777
47304778
Aliases: `--resource-dir`
47314779
4780+
**--with-compiler**
4781+
4782+
Allows to include the Scala compiler artifacts on the classpath.
4783+
4784+
Aliases: `-with-compiler` ,`--with-scala-compiler`
4785+
47324786
**--compilation-output**
47334787
47344788
Copy compilation results to output directory using either relative or absolute path

0 commit comments

Comments
 (0)