Skip to content

Commit

Permalink
fix: concurrency control around Scala 3 compiler
Browse files Browse the repository at this point in the history
**Problem**
Early initialization thread throws ConcurrentModificationException.

**Solution**
This puts in synchronized to lock for the access.
  • Loading branch information
eed3si9n committed Dec 10, 2024
1 parent 35eda2e commit 3b6b025
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
28 changes: 15 additions & 13 deletions buildfile/src/main/scala/sbt/internal/parser/SbtParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ private[sbt] object SbtParser:

private[sbt] var scalacGlobalInitReporter: Option[ConsoleReporter] = None

private[sbt] val globalReporter = UniqueParserReporter()
private[sbt] val defaultGlobalForParser = ParseDriver()
private[sbt] lazy val globalReporter = UniqueParserReporter()
private lazy val defaultGlobalForParser = ParseDriver()
private[sbt] def getGlobalForParser: ParseDriver = synchronized:
defaultGlobalForParser
private[sbt] final class ParseDriver extends Driver:
override protected val sourcesRequired: Boolean = false
val compileCtx0 = initCtx.fresh
Expand Down Expand Up @@ -171,15 +173,17 @@ private[sbt] object SbtParser:
parsedTrees
end SbtParser

private class SbtParserInit {
new Thread("sbt-parser-init-thread") {
/**
* This gives JVM a head start to JIT Scala 3 compiler JAR.
* Called by sbt.internal.ClassLoaderWarmup.
*/
private class SbtParserInit:
val t = new Thread("sbt-parser-init-thread"):
setDaemon(true)
start()
override def run(): Unit = {
val _ = SbtParser.defaultGlobalForParser
}
}
}
override def run(): Unit =
val _ = SbtParser.getGlobalForParser
t.start()
end SbtParserInit

/**
* This method solely exists to add scaladoc to members in SbtParser which
Expand Down Expand Up @@ -222,8 +226,6 @@ private[sbt] case class SbtParser(path: VirtualFileRef, lines: Seq[String])
// parsed trees.
val (imports, settings, settingsTrees) = splitExpressions(path, lines)

import SbtParser.defaultGlobalForParser.*

private def splitExpressions(
path: VirtualFileRef,
lines: Seq[String]
Expand All @@ -239,7 +241,7 @@ private[sbt] case class SbtParser(path: VirtualFileRef, lines: Seq[String])
VirtualFile(reporterId, wrapCode.getBytes(StandardCharsets.UTF_8)),
scala.io.Codec.UTF8
)
given Context = compileCtx.fresh.setSource(sourceFile)
given Context = SbtParser.getGlobalForParser.compileCtx.fresh.setSource(sourceFile)
val parsedTrees = parse(fileName, reporterId)

// Check No val (a,b) = foo *or* val a,b = foo as these are problematic to range positions and the WHOLE architecture.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private[sbt] object SbtRefactorings:
commands: Seq[SessionSetting]
): Seq[String] = {
val split = SbtParser(FAKE_FILE, lines)
given ctx: Context = SbtParser.defaultGlobalForParser.compileCtx
given ctx: Context = SbtParser.getGlobalForParser.compileCtx
val recordedCommands = recordCommands(commands, split)
val sortedRecordedCommands = recordedCommands.sortBy(_._1)(reverseOrderingInt)

Expand Down

0 comments on commit 3b6b025

Please sign in to comment.