Merge pull request #8377 from eed3si9n/wip/compiler-bridge

[2.x] perf: Cache compiler bridge binary
This commit is contained in:
eugene yokota 2025-11-22 15:30:00 -05:00 committed by GitHub
commit fe6e9b4802
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 46 additions and 28 deletions

View File

@ -707,6 +707,7 @@ lazy val mainProj = (project in file("main"))
Compile / doc / sources := Nil,
mimaSettings,
mimaBinaryIssueFilters ++= Vector(
exclude[DirectMissingMethodProblem]("sbt.Keys.scalaCompilerBridgeBinaryJar"),
),
)
.dependsOn(lmCore, lmIvy, lmCoursierShadedPublishing)

View File

@ -727,27 +727,38 @@ object Defaults extends BuildCommon {
}
clean.value
},
scalaCompilerBridgeBinaryJar := Def.uncached {
val sv = scalaVersion.value
val managed = managedScalaInstance.value
val hasSbtBridge = ScalaArtifacts.isScala3(sv) || ZincLmUtil.hasScala2SbtBridge(sv)
if hasSbtBridge && managed then
scalaCompilerBridgeBin := Def
.ifS(Def.task {
val sv = scalaVersion.value
val managed = managedScalaInstance.value
val hasSbtBridge = ScalaArtifacts.isScala3(sv) || ZincLmUtil.hasScala2SbtBridge(sv)
hasSbtBridge && managed
})(Def.cachedTask {
val sv = scalaVersion.value
val conv = fileConverter.value
val s = streams.value
val t = target.value
val r = dependencyResolution.value
val uc = updateConfiguration.value
val jar = ZincLmUtil.fetchDefaultBridgeModule(
sv,
dependencyResolution.value,
updateConfiguration.value,
r,
uc,
(update / unresolvedWarningConfiguration).value,
streams.value.log
s.log
)
Some(jar)
else None
},
val out = t / "compiler-bridge" / jar.getName()
val outVf = conv.toVirtualFile(out.toPath())
IO.copyFile(jar, out)
Def.declareOutput(outVf)
Vector(outVf: HashedVirtualFileRef)
})(Def.task(Vector.empty))
.value,
scalaCompilerBridgeSource := ZincLmUtil.getDefaultBridgeSourceModule(scalaVersion.value),
auxiliaryClassFiles ++= {
if (ScalaArtifacts.isScala3(scalaVersion.value)) List(TastyFiles.instance)
else Nil
},
consoleProject / scalaCompilerBridgeBinaryJar := Def.uncached(None),
consoleProject / scalaCompilerBridgeSource := ZincLmUtil.getDefaultBridgeSourceModule(
appConfiguration.value.provider.scalaProvider.version
),
@ -832,16 +843,17 @@ object Defaults extends BuildCommon {
val app = appConfiguration.value
val launcher = app.provider.scalaProvider.launcher
val dr = scalaCompilerBridgeDependencyResolution.value
val conv = fileConverter.value
val scalac =
scalaCompilerBridgeBinaryJar.value match {
case Some(jar) =>
scalaCompilerBridgeBin.value.toList match
case jar :: xs =>
AlternativeZincUtil.scalaCompiler(
scalaInstance = scalaInstance.value,
classpathOptions = classpathOptions.value,
compilerBridgeJar = jar,
compilerBridgeJar = conv.toPath(jar).toFile(),
classLoaderCache = st.get(BasicKeys.classLoaderCache)
)
case _ =>
case Nil =>
ZincLmUtil.scalaCompiler(
scalaInstance = scalaInstance.value,
classpathOptions = classpathOptions.value,
@ -854,7 +866,6 @@ object Defaults extends BuildCommon {
classLoaderCache = st.get(BasicKeys.classLoaderCache),
log = streams.value.log
)
}
val compilers = ZincUtil.compilers(
instance = scalaInstance.value,
classpathOptions = classpathOptions.value,

View File

@ -145,6 +145,8 @@ object Keys {
// Path Keys
val baseDirectory = settingKey[File]("The base directory. Depending on the scope, this is the base directory for the build, project, configuration, or task.").withRank(AMinusSetting)
@transient
val target = settingKey[File]("Main directory for files generated by the build.").withRank(AMinusSetting)
val crossTarget = settingKey[File]("Main directory for files generated by the build that are cross-built.").withRank(BSetting)
@ -230,7 +232,7 @@ object Keys {
val crossSbtVersions = settingKey[Seq[String]]("The versions of Sbt used when cross-building an sbt plugin.")
val printWarnings = taskKey[Unit]("Shows warnings from compilation, including ones that weren't printed initially.").withRank(BPlusTask)
val fileInputOptions = settingKey[Seq[String]]("Options that take file input, which may invalidate the cache.").withRank(CSetting)
val scalaCompilerBridgeBinaryJar = taskKey[Option[File]]("Optionally, the jar of the compiler bridge. When not None, this takes precedence over scalaCompilerBridgeSource").withRank(CSetting)
val scalaCompilerBridgeBin = taskKey[Seq[HashedVirtualFileRef]]("Optionally, the jar of the compiler bridge. When not None, this takes precedence over scalaCompilerBridgeSource").withRank(DTask)
val scalaCompilerBridgeSource = settingKey[ModuleID]("Configures the module ID of the sources of the compiler bridge when scalaCompilerBridgeBinaryJar is None").withRank(CSetting)
val scalaCompilerBridgeScope = taskKey[Unit]("The compiler bridge scope.").withRank(DTask)
val scalaArtifacts = settingKey[Seq[String]]("Configures the list of artifacts which should match the Scala binary version").withRank(CSetting)
@ -519,8 +521,12 @@ object Keys {
val unmanagedBase = settingKey[File]("The default directory for manually managed libraries.").withRank(ASetting)
val updateConfiguration = settingKey[UpdateConfiguration]("Configuration for resolving and retrieving managed dependencies.").withRank(DSetting)
val updateOptions = settingKey[UpdateOptions]("Options for resolving managed dependencies.").withRank(DSetting)
@transient
val unresolvedWarningConfiguration = taskKey[UnresolvedWarningConfiguration]("Configuration for unresolved dependency warning.").withRank(DTask)
val dependencyPositions = taskKey[Map[ModuleID, SourcePosition]]("Source positions where the dependencies are defined.").withRank(DTask)
@transient
val dependencyResolution = taskKey[DependencyResolution]("Provides the sbt interface to dependency resolution.").withRank(CTask)
val publisher = taskKey[Publisher]("Provides the sbt interface to publisher")
val ivySbt = taskKey[IvySbt]("Provides the sbt interface to Ivy.").withRank(CTask)

View File

@ -27,8 +27,9 @@ object ConsoleProject {
val unit = extracted.currentUnit
val (state1, dependencyResolution) =
extracted.runTask(Keys.dependencyResolution, state)
val (_, scalaCompilerBridgeBinaryJar) =
extracted.runTask(Keys.consoleProject / Keys.scalaCompilerBridgeBinaryJar, state1)
val (_, scalaCompilerBridgeBinaryBin) =
extracted.runTask(Keys.consoleProject / Keys.scalaCompilerBridgeBin, state1)
val conv = extracted.get(Keys.fileConverter)
val scalaInstance = {
val scalaProvider = state.configuration.provider.scalaProvider
ScalaInstance(scalaProvider.version, scalaProvider)
@ -37,15 +38,15 @@ object ConsoleProject {
val zincDir = BuildPaths.getZincDirectory(state, g)
val app = state.configuration
val launcher = app.provider.scalaProvider.launcher
val compiler = scalaCompilerBridgeBinaryJar match {
case Some(jar) =>
val compiler = scalaCompilerBridgeBinaryBin.toList match
case jar :: xs =>
AlternativeZincUtil.scalaCompiler(
scalaInstance = scalaInstance,
classpathOptions = ClasspathOptionsUtil.repl,
compilerBridgeJar = jar,
compilerBridgeJar = conv.toPath(jar).toFile(),
classLoaderCache = state1.get(BasicKeys.classLoaderCache)
)
case None =>
case Nil =>
ZincLmUtil.scalaCompiler(
scalaInstance = scalaInstance,
classpathOptions = ClasspathOptionsUtil.repl,
@ -59,7 +60,6 @@ object ConsoleProject {
classLoaderCache = state1.get(BasicKeys.classLoaderCache),
log = log
)
}
val imports = BuildUtil.getImports(unit.unit) ++ BuildUtil.importAll(bindings.map(_._1))
val importString = imports.mkString("", ";\n", ";\n\n")
val initCommands = importString + extra

View File

@ -3,7 +3,7 @@ ThisBuild / scalaVersion := "3.3.4"
lazy val check = taskKey[Unit]("")
check := Def.uncached {
val bridge = scalaCompilerBridgeBinaryJar.value
bridge.getOrElse(sys.error(s"bridge JAR is missing"))
()
val bridge = scalaCompilerBridgeBin.value
if bridge.isEmpty then sys.error(s"bridge JAR is missing")
else ()
}