Skip to content

Commit 466579e

Browse files
authored
Pass scalac arguments as file (#2584)
* Add writing scalac arguments to file * Add tests
1 parent aa78658 commit 466579e

File tree

5 files changed

+64
-24
lines changed

5 files changed

+64
-24
lines changed

build.sc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,8 @@ trait Core extends ScalaCliSbtModule with ScalaCliPublishModule with HasTests
431431
| def workspaceDirName = "$workspaceDirName"
432432
| def projectFileName = "$projectFileName"
433433
| def jvmPropertiesFileName = "$jvmPropertiesFileName"
434+
| def scalacArgumentsFileName = "scalac.args.txt"
435+
| def maxScalacArgumentsCount = 5000
434436
|
435437
| def defaultGraalVMJavaVersion = ${deps.graalVmJavaVersion}
436438
| def defaultGraalVMVersion = "${deps.graalVmVersion}"

modules/build/src/main/scala/scala/build/Build.scala

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,10 @@ object Build {
472472
}
473473
}
474474

475+
def projectRootDir(root: os.Path, projectName: String): os.Path =
476+
root / Constants.workspaceDirName / projectName
475477
def classesRootDir(root: os.Path, projectName: String): os.Path =
476-
root / Constants.workspaceDirName / projectName / "classes"
478+
projectRootDir(root, projectName) / "classes"
477479
def classesDir(root: os.Path, projectName: String, scope: Scope, suffix: String = ""): os.Path =
478480
classesRootDir(root, projectName) / s"${scope.name}$suffix"
479481

@@ -948,6 +950,8 @@ object Build {
948950

949951
val project = Project(
950952
directory = inputs.workspace / Constants.workspaceDirName,
953+
argsFilePath =
954+
projectRootDir(inputs.workspace, inputs.projectName) / Constants.scalacArgumentsFileName,
951955
workspace = inputs.workspace,
952956
classesDir = classesDir0,
953957
scaladocDir = scaladocDir,
@@ -1026,14 +1030,24 @@ object Build {
10261030

10271031
val projectChanged = compiler.prepareProject(project, logger)
10281032

1029-
if (compiler.usesClassDir && projectChanged && os.isDir(classesDir0)) {
1030-
logger.debug(s"Clearing $classesDir0")
1031-
os.list(classesDir0).foreach { p =>
1032-
logger.debug(s"Removing $p")
1033-
try os.remove.all(p)
1033+
if (projectChanged) {
1034+
if (compiler.usesClassDir && os.isDir(classesDir0)) {
1035+
logger.debug(s"Clearing $classesDir0")
1036+
os.list(classesDir0).foreach { p =>
1037+
logger.debug(s"Removing $p")
1038+
try os.remove.all(p)
1039+
catch {
1040+
case ex: FileSystemException =>
1041+
logger.debug(s"Ignoring $ex while cleaning up $p")
1042+
}
1043+
}
1044+
}
1045+
if (os.exists(project.argsFilePath)) {
1046+
logger.debug(s"Removing ${project.argsFilePath}")
1047+
try os.remove(project.argsFilePath)
10341048
catch {
10351049
case ex: FileSystemException =>
1036-
logger.debug(s"Ignoring $ex while cleaning up $p")
1050+
logger.debug(s"Ignoring $ex while cleaning up ${project.argsFilePath}")
10371051
}
10381052
}
10391053
}
@@ -1073,18 +1087,6 @@ object Build {
10731087
)
10741088
}
10751089

1076-
if (compiler.usesClassDir && projectChanged && os.isDir(classesDir0)) {
1077-
logger.debug(s"Clearing $classesDir0")
1078-
os.list(classesDir0).foreach { p =>
1079-
logger.debug(s"Removing $p")
1080-
try os.remove.all(p)
1081-
catch {
1082-
case ex: FileSystemException =>
1083-
logger.debug(s"Ignore $ex while removing $p")
1084-
}
1085-
}
1086-
}
1087-
10881090
buildClient.clear()
10891091
buildClient.setGeneratedSources(scope, generatedSources)
10901092

modules/build/src/main/scala/scala/build/Project.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import scala.build.options.{ScalacOpt, Scope, ShadowingSeq}
1515
final case class Project(
1616
workspace: os.Path,
1717
directory: os.Path,
18+
argsFilePath: os.Path,
1819
classesDir: os.Path,
1920
scaladocDir: os.Path,
2021
scalaCompiler: Option[ScalaCompilerParams],

modules/build/src/main/scala/scala/build/compiler/SimpleScalaCompiler.scala

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package scala.build.compiler
22

33
import java.io.File
44

5-
import scala.build.internal.Runner
5+
import scala.build.internal.{Constants, Runner}
66
import scala.build.{Logger, Positioned, Project}
77

88
/** A simple Scala compiler designed to handle scaladocs, Java projects & get `scalac` outputs.
@@ -51,6 +51,8 @@ final case class SimpleScalaCompiler(
5151
* sources to be passed when running `scalac` (optional)
5252
* @param outputDir
5353
* output directory for the compiler (optional)
54+
* @param argsFileDir
55+
* output directory for the args file (optional)
5456
* @param cwd
5557
* working directory for running the compiler
5658
* @param logger
@@ -67,6 +69,7 @@ final case class SimpleScalaCompiler(
6769
compilerClassPath: Seq[os.Path],
6870
sources: Seq[String],
6971
outputDir: Option[os.Path],
72+
argsFilePath: Option[os.Path],
7073
cwd: os.Path,
7174
logger: Logger
7275
): Int = {
@@ -80,7 +83,16 @@ final case class SimpleScalaCompiler(
8083
Seq("-cp", classPath.map(_.toString).mkString(File.pathSeparator))
8184
else Nil
8285

83-
val args = scalacOptions ++ outputDirArgs ++ classPathArgs ++ sources
86+
val args = {
87+
val freeArgs = scalacOptions ++ outputDirArgs ++ classPathArgs ++ sources
88+
89+
if (freeArgs.size > Constants.maxScalacArgumentsCount)
90+
argsFilePath.fold(freeArgs) { path =>
91+
os.write(path, freeArgs.mkString(System.lineSeparator()))
92+
Seq(s"@$path")
93+
}
94+
else freeArgs
95+
}
8496

8597
val javaCommand =
8698
javaHomeOpt.map(SimpleJavaCompiler.javaCommand(_)).getOrElse(defaultJavaCommand)
@@ -130,6 +142,7 @@ final case class SimpleScalaCompiler(
130142
compilerClassPath = project.scalaCompiler.map(_.compilerClassPath).getOrElse(Nil),
131143
sources = project.sources.map(_.toString),
132144
outputDir = Some(outputDir),
145+
argsFilePath = Some(project.argsFilePath),
133146
cwd = project.workspace,
134147
logger = logger
135148
)
@@ -176,6 +189,7 @@ final case class SimpleScalaCompiler(
176189
compilerClassPath = compilerClassPath,
177190
sources = Nil,
178191
outputDir = None,
192+
argsFilePath = None,
179193
cwd = os.pwd,
180194
logger = logger
181195
)

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ abstract class BuildTests(server: Boolean) extends TestUtil.ScalaCliBuildSuite {
5353
)
5454
)
5555

56-
def sv2 = "2.13.5"
56+
def sv2 = Constants.defaultScala213Version
5757
val defaultOptions = baseOptions.copy(
5858
scalaOptions = baseOptions.scalaOptions.copy(
5959
scalaVersion = Some(MaybeScalaVersion(sv2)),
@@ -62,7 +62,7 @@ abstract class BuildTests(server: Boolean) extends TestUtil.ScalaCliBuildSuite {
6262
scriptOptions = ScriptOptions(Some(true))
6363
)
6464

65-
def sv3 = "3.0.0"
65+
def sv3 = Constants.defaultScalaVersion
6666
val defaultScala3Options = defaultOptions.copy(
6767
scalaOptions = defaultOptions.scalaOptions.copy(
6868
scalaVersion = Some(MaybeScalaVersion(sv3)),
@@ -390,10 +390,11 @@ abstract class BuildTests(server: Boolean) extends TestUtil.ScalaCliBuildSuite {
390390
(root, _, maybeBuild) =>
391391
val expectedDiag = {
392392
val start = new bsp4j.Position(2, 0)
393-
val end = new bsp4j.Position(2, 0) // would have expected (2, 2) here :|
393+
val end = new bsp4j.Position(2, 2)
394394
val range = new bsp4j.Range(start, end)
395395
val d = new bsp4j.Diagnostic(range, "Not found: zz")
396396
d.setSource("bloop")
397+
d.setCode("6")
397398
d.setSeverity(bsp4j.DiagnosticSeverity.ERROR)
398399
val bScalaDiagnostic = new bsp4j.ScalaDiagnostic
399400
bScalaDiagnostic.setActions(List().asJava)
@@ -930,4 +931,24 @@ abstract class BuildTests(server: Boolean) extends TestUtil.ScalaCliBuildSuite {
930931
}
931932
}
932933
}
934+
935+
for (options <- Seq(defaultOptions, defaultScala3Options))
936+
test(s"compile 12k sources for Scala ${options.scalaOptions.scalaVersion.get.asString}") {
937+
val mainInput = os.rel / "main.sc" ->
938+
"""//> using jvm 11
939+
|println("Hello from big build")
940+
|""".stripMargin
941+
942+
val additionalInputs = 1 to 12000 map { i =>
943+
os.rel / s"Foo$i.scala" ->
944+
s"""object Foo$i
945+
|""".stripMargin
946+
}
947+
948+
val testInputs = TestInputs(mainInput +: additionalInputs: _*)
949+
950+
testInputs.withBuild(options, buildThreads, bloopConfigOpt) { (_, _, maybeBuild) =>
951+
expect(maybeBuild.exists(_.success))
952+
}
953+
}
933954
}

0 commit comments

Comments
 (0)