From e0f0550276f4691df1ba95357d3ecb5b9e860c60 Mon Sep 17 00:00:00 2001 From: Eugene Yokota Date: Thu, 15 Aug 2024 00:03:15 -0400 Subject: [PATCH] Migrate cachedCompileIncrementalTask to dir caching --- main/src/main/scala/sbt/Defaults.scala | 52 ++++++------------- main/src/main/scala/sbt/Keys.scala | 2 +- .../scala/sbt/internal/CompileInputs2.scala | 14 +++-- .../build.sbt | 1 + .../cross-strict-aggregation-scala-3/test | 28 +++++----- 5 files changed, 44 insertions(+), 53 deletions(-) diff --git a/main/src/main/scala/sbt/Defaults.scala b/main/src/main/scala/sbt/Defaults.scala index a741fa37c..d44a01bdd 100644 --- a/main/src/main/scala/sbt/Defaults.scala +++ b/main/src/main/scala/sbt/Defaults.scala @@ -923,9 +923,9 @@ object Defaults extends BuildCommon { compileOutputs := { import scala.jdk.CollectionConverters.* val c = fileConverter.value - val (_, jarFile) = compileIncremental.value + val (_, vfDir, _) = compileIncremental.value val classFiles = compile.value.readStamps.getAllProductStamps.keySet.asScala - classFiles.toSeq.map(c.toPath) :+ compileAnalysisFile.value.toPath :+ c.toPath(jarFile) + classFiles.toSeq.map(c.toPath) :+ compileAnalysisFile.value.toPath :+ c.toPath(vfDir) }, compileOutputs := compileOutputs.triggeredBy(compile).value, tastyFiles := Def.taskIf { @@ -2530,10 +2530,6 @@ object Defaults extends BuildCommon { val dir = c.toPath(backendOutput.value).toFile result match case Result.Value(res) => - val rawJarPath = c.toPath(res._2) - IO.delete(dir) - IO.unzip(rawJarPath.toFile, dir) - IO.delete(dir / "META-INF" / "MANIFEST.MF") val analysis = store.unsafeGet().getAnalysis() reporter.sendSuccessReport(analysis) bspTask.notifySuccess(analysis) @@ -2544,13 +2540,6 @@ object Defaults extends BuildCommon { bspTask.notifyFailure(compileFailed) throw cause }, - packagedArtifact := { - val (hasModified, out) = compileIncremental.value - artifact.value -> out - }, - artifact := artifactSetting.value, - artifactClassifier := Some("noresources"), - artifactPath := artifactPathSetting(artifact).value, ) ) @@ -2571,21 +2560,11 @@ object Defaults extends BuildCommon { val contents = AnalysisContents.create(analysisResult.analysis(), analysisResult.setup()) store.set(contents) Def.declareOutput(analysisOut) - val dir = ci.options.classesDirectory.toFile() - val mappings = Path - .allSubpaths(dir) - .filter(_._1.isFile()) - .map { case (p, path) => - val vf = c.toVirtualFile(p.toPath()) - (vf: HashedVirtualFileRef) -> path - } - .toSeq - // inlined to avoid caching mappings - val pkgConfig = Pkg.Configuration(mappings, artifactPath.value, packageOptions.value) - val out = Pkg(pkgConfig, c, s.log, Pkg.timeFromConfiguration(pkgConfig)) - s.log.debug(s"wrote $out") - Def.declareOutput(out) - analysisResult.hasModified() -> (out: HashedVirtualFileRef) + val dir = ci.options.classesDirectory + val vfDir = c.toVirtualFile(dir) + val packedDir = Def.declareOutputDirectory(vfDir) + s.log.debug(s"wrote $vfDir") + (analysisResult.hasModified(), vfDir: VirtualFileRef, packedDir: HashedVirtualFileRef) } .tag(Tags.Compile, Tags.CPU) @@ -2732,6 +2711,7 @@ object Defaults extends BuildCommon { inputs.options.sources.toVector, scalacOptions.value.toVector, javacOptions.value.toVector, + outputPath.value + prefix(configuration.value.name), ) }, bspCompileTask := @@ -4425,17 +4405,19 @@ object Classpaths { def makeProducts: Initialize[Task[Seq[File]]] = Def.task { val c = fileConverter.value val resources = copyResources.value.map(_._2).toSet - val dir = classDirectory.value - val rawJar = compileIncremental.value._2 - val rawJarPath = c.toPath(rawJar) + val classDir = classDirectory.value + val vfBackendDir = compileIncremental.value._2 + val backendDir = c.toPath(vfBackendDir) // delete outdated files Path - .allSubpaths(dir) + .allSubpaths(classDir) .collect { case (f, _) if f.isFile() && !resources.contains(f) => f } .foreach(IO.delete) - IO.unzip(rawJarPath.toFile, dir) - IO.delete(dir / "META-INF" / "MANIFEST.MF") - dir :: Nil + IO.copyDirectory( + source = backendDir.toFile(), + target = classDir, + ) + classDir :: Nil } private[sbt] def makePickleProducts: Initialize[Task[Seq[VirtualFile]]] = Def.task { diff --git a/main/src/main/scala/sbt/Keys.scala b/main/src/main/scala/sbt/Keys.scala index e2b885c70..e5c46c962 100644 --- a/main/src/main/scala/sbt/Keys.scala +++ b/main/src/main/scala/sbt/Keys.scala @@ -250,7 +250,7 @@ object Keys { val consoleProject = taskKey[Unit]("Starts the Scala interpreter with the sbt and the build definition on the classpath and useful imports.").withRank(AMinusTask) val compile = taskKey[CompileAnalysis]("Compiles sources.").withRank(APlusTask) val manipulateBytecode = taskKey[CompileResult]("Manipulates generated bytecode").withRank(BTask) - val compileIncremental = taskKey[(Boolean, HashedVirtualFileRef)]("Actually runs the incremental compilation").withRank(DTask) + val compileIncremental = taskKey[(Boolean, VirtualFileRef, HashedVirtualFileRef)]("Actually runs the incremental compilation").withRank(DTask) val previousCompile = taskKey[PreviousResult]("Read the incremental compiler analysis from disk").withRank(DTask) val tastyFiles = taskKey[Seq[File]]("Returns the TASTy files produced by compilation").withRank(DTask) private[sbt] val compileScalaBackend = taskKey[CompileResult]("Compiles only Scala sources if pipelining is enabled. Compiles both Scala and Java sources otherwise").withRank(Invisible) diff --git a/main/src/main/scala/sbt/internal/CompileInputs2.scala b/main/src/main/scala/sbt/internal/CompileInputs2.scala index 742fc8f04..199140f9d 100644 --- a/main/src/main/scala/sbt/internal/CompileInputs2.scala +++ b/main/src/main/scala/sbt/internal/CompileInputs2.scala @@ -10,6 +10,7 @@ case class CompileInputs2( sources: Vector[HashedVirtualFileRef], scalacOptions: Vector[String], javacOptions: Vector[String], + relativeOutputPath: String, ) object CompileInputs2: @@ -18,7 +19,7 @@ object CompileInputs2: given IsoLList.Aux[ CompileInputs2, Vector[HashedVirtualFileRef] :*: Vector[HashedVirtualFileRef] :*: Vector[String] :*: - Vector[String] :*: LNil + Vector[String] :*: String :*: LNil ] = LList.iso( { (v: CompileInputs2) => @@ -26,12 +27,19 @@ object CompileInputs2: ("sources", v.sources) :*: ("scalacOptions", v.scalacOptions) :*: ("javacOptions", v.javacOptions) :*: + ("relativeOutputPath", v.relativeOutputPath) :*: LNil }, { (in: Vector[HashedVirtualFileRef] :*: Vector[HashedVirtualFileRef] :*: Vector[String] :*: - Vector[String] :*: LNil) => - CompileInputs2(in.head, in.tail.head, in.tail.tail.head, in.tail.tail.tail.head) + Vector[String] :*: String :*: LNil) => + CompileInputs2( + in.head, + in.tail.head, + in.tail.tail.head, + in.tail.tail.tail.head, + in.tail.tail.tail.tail.head + ) } ) end CompileInputs2 diff --git a/sbt-app/src/sbt-test/actions/cross-strict-aggregation-scala-3/build.sbt b/sbt-app/src/sbt-test/actions/cross-strict-aggregation-scala-3/build.sbt index 38a5a26fe..c356d3d6a 100644 --- a/sbt-app/src/sbt-test/actions/cross-strict-aggregation-scala-3/build.sbt +++ b/sbt-app/src/sbt-test/actions/cross-strict-aggregation-scala-3/build.sbt @@ -1,4 +1,5 @@ scalaVersion := "2.12.19" +name := "root" lazy val core = project .settings( diff --git a/sbt-app/src/sbt-test/actions/cross-strict-aggregation-scala-3/test b/sbt-app/src/sbt-test/actions/cross-strict-aggregation-scala-3/test index 9afecfa1e..a2497b474 100644 --- a/sbt-app/src/sbt-test/actions/cross-strict-aggregation-scala-3/test +++ b/sbt-app/src/sbt-test/actions/cross-strict-aggregation-scala-3/test @@ -1,19 +1,19 @@ -> ++3.0.2 compile +> ++3.0.2 packageBin -$ exists target/out/jvm/scala-3.0.2/core/core_3-0.1.0-SNAPSHOT-noresources.jar --$ exists target/out/jvm/scala-3.1.2/core/core_3-0.1.0-SNAPSHOT-noresources.jar --$ exists target/out/jvm/scala-3.0.2/subproj/subproj_3-0.1.0-SNAPSHOT-noresources.jar --$ exists target/out/jvm/scala-3.1.2/subproj/subproj_3-0.1.0-SNAPSHOT-noresources.jar +$ exists target/out/jvm/scala-3.0.2/core/core_3-0.1.0-SNAPSHOT.jar +-$ exists target/out/jvm/scala-3.1.2/core/core_3-0.1.0-SNAPSHOT.jar +-$ exists target/out/jvm/scala-3.0.2/subproj/subproj_3-0.1.0-SNAPSHOT.jar +-$ exists target/out/jvm/scala-3.1.2/subproj/subproj_3-0.1.0-SNAPSHOT.jar > clean --$ exists target/out/jvm/scala-3.0.2/core/core_3-0.1.0-SNAPSHOT-noresources.jar --$ exists target/out/jvm/scala-3.1.2/core/core_3-0.1.0-SNAPSHOT-noresources.jar --$ exists target/out/jvm/scala-3.0.2/subproj/subproj_3-0.1.0-SNAPSHOT-noresources.jar --$ exists target/out/jvm/scala-3.1.2/subproj/subproj_3-0.1.0-SNAPSHOT-noresources.jar +-$ exists target/out/jvm/scala-3.0.2/core/core_3-0.1.0-SNAPSHOT.jar +-$ exists target/out/jvm/scala-3.1.2/core/core_3-0.1.0-SNAPSHOT.jar +-$ exists target/out/jvm/scala-3.0.2/subproj/subproj_3-0.1.0-SNAPSHOT.jar +-$ exists target/out/jvm/scala-3.1.2/subproj/subproj_3-0.1.0-SNAPSHOT.jar -> ++3.1.2 compile +> ++3.1.2 packageBin --$ exists target/out/jvm/scala-3.0.2/core/core_3-0.1.0-SNAPSHOT-noresources.jar -$ exists target/out/jvm/scala-3.1.2/core/core_3-0.1.0-SNAPSHOT-noresources.jar --$ exists target/out/jvm/scala-3.0.2/subproj/subproj_3-0.1.0-SNAPSHOT-noresources.jar -$ exists target/out/jvm/scala-3.1.2/subproj/subproj_3-0.1.0-SNAPSHOT-noresources.jar +-$ exists target/out/jvm/scala-3.0.2/core/core_3-0.1.0-SNAPSHOT.jar +$ exists target/out/jvm/scala-3.1.2/core/core_3-0.1.0-SNAPSHOT.jar +-$ exists target/out/jvm/scala-3.0.2/subproj/subproj_3-0.1.0-SNAPSHOT.jar +$ exists target/out/jvm/scala-3.1.2/subproj/subproj_3-0.1.0-SNAPSHOT.jar