From c12484399fd4072133680c155dd4d5abf63c4316 Mon Sep 17 00:00:00 2001 From: Matt Dziuban Date: Thu, 2 Nov 2023 02:50:05 -0400 Subject: [PATCH] Add an option to disable incremental compilation with zinc (#2851) This change add an option to force a full recompile for a given module. Pull request: https://github.com/com-lihaoyi/mill/pull/2851 Co-authored-by: Tobias Roeser --- .../ROOT/pages/Scala_Module_Config.adoc | 19 +++++ .../src/mill/scalalib/api/ZincWorkerApi.scala | 79 ++++++++++++++++++- scalalib/src/mill/scalalib/JavaModule.scala | 7 +- scalalib/src/mill/scalalib/ScalaModule.scala | 6 +- .../mill/scalalib/SemanticDbJavaModule.scala | 4 +- .../mill/scalalib/worker/ZincWorkerImpl.scala | 26 +++--- 6 files changed, 126 insertions(+), 15 deletions(-) diff --git a/docs/modules/ROOT/pages/Scala_Module_Config.adoc b/docs/modules/ROOT/pages/Scala_Module_Config.adoc index f23a4d56567..b583d04d972 100644 --- a/docs/modules/ROOT/pages/Scala_Module_Config.adoc +++ b/docs/modules/ROOT/pages/Scala_Module_Config.adoc @@ -144,3 +144,22 @@ As a consequence, Ammonite needs full Scala version specific releases. The older your used Mill version or the newer the Scala version you want to use, the higher is the risk that the default Ammonite version will not match. -- + +== Disabling incremental compilation with Zinc + +By default all `ScalaModule`s use incremental compilation via https://github.com/sbt/zinc[Zinc] to +only recompile sources that have changed since the last compile, or ones that have been invalidated +by changes to upstream sources. + +If for any reason you want to disable incremental compilation for a module, you can override and set +`zincIncrementalCompilation` to `false` + +.`build.sc` +[source,scala,subs="attributes,verbatim"] +---- +import mill._, scalalib._ + +object foo extends ScalaModule { + def zincIncrementalCompilation = false +} +---- diff --git a/scalalib/api/src/mill/scalalib/api/ZincWorkerApi.scala b/scalalib/api/src/mill/scalalib/api/ZincWorkerApi.scala index 130d7fa8146..dc04fb91a15 100644 --- a/scalalib/api/src/mill/scalalib/api/ZincWorkerApi.scala +++ b/scalalib/api/src/mill/scalalib/api/ZincWorkerApi.scala @@ -3,12 +3,34 @@ package mill.scalalib.api import mill.api.{CompileProblemReporter, PathRef} import mill.api.Loose.Agg +import scala.annotation.nowarn + object ZincWorkerApi { type Ctx = mill.api.Ctx.Dest with mill.api.Ctx.Log with mill.api.Ctx.Home } trait ZincWorkerApi { /** Compile a Java-only project */ + def compileJava( + upstreamCompileOutput: Seq[CompilationResult], + sources: Agg[os.Path], + compileClasspath: Agg[os.Path], + javacOptions: Seq[String], + reporter: Option[CompileProblemReporter], + reportCachedProblems: Boolean, + incrementalCompilation: Boolean + )(implicit ctx: ZincWorkerApi.Ctx): mill.api.Result[CompilationResult] = + compileJava( + upstreamCompileOutput = upstreamCompileOutput, + sources = sources, + compileClasspath = compileClasspath, + javacOptions = javacOptions, + reporter = reporter, + reportCachedProblems = reportCachedProblems + ): @nowarn("cat=deprecation") + + /** Compile a Java-only project */ + @deprecated("Use override with `incrementalCompilation` parameter", "Mill 0.11.6") def compileJava( upstreamCompileOutput: Seq[CompilationResult], sources: Agg[os.Path], @@ -16,9 +38,48 @@ trait ZincWorkerApi { javacOptions: Seq[String], reporter: Option[CompileProblemReporter], reportCachedProblems: Boolean - )(implicit ctx: ZincWorkerApi.Ctx): mill.api.Result[CompilationResult] + )(implicit ctx: ZincWorkerApi.Ctx): mill.api.Result[CompilationResult] = + compileJava( + upstreamCompileOutput = upstreamCompileOutput, + sources = sources, + compileClasspath = compileClasspath, + javacOptions = javacOptions, + reporter = reporter, + reportCachedProblems = reportCachedProblems, + incrementalCompilation = true + ) + + /** Compile a mixed Scala/Java or Scala-only project */ + def compileMixed( + upstreamCompileOutput: Seq[CompilationResult], + sources: Agg[os.Path], + compileClasspath: Agg[os.Path], + javacOptions: Seq[String], + scalaVersion: String, + scalaOrganization: String, + scalacOptions: Seq[String], + compilerClasspath: Agg[PathRef], + scalacPluginClasspath: Agg[PathRef], + reporter: Option[CompileProblemReporter], + reportCachedProblems: Boolean, + incrementalCompilation: Boolean + )(implicit ctx: ZincWorkerApi.Ctx): mill.api.Result[CompilationResult] = + compileMixed( + upstreamCompileOutput = upstreamCompileOutput, + sources = sources, + compileClasspath = compileClasspath, + javacOptions = javacOptions, + scalaVersion = scalaVersion, + scalaOrganization = scalaOrganization, + scalacOptions = scalacOptions, + compilerClasspath = compilerClasspath, + scalacPluginClasspath = scalacPluginClasspath, + reporter = reporter, + reportCachedProblems = reportCachedProblems + ): @nowarn("cat=deprecation") /** Compile a mixed Scala/Java or Scala-only project */ + @deprecated("Use override with `incrementalCompilation` parameter", "Mill 0.11.6") def compileMixed( upstreamCompileOutput: Seq[CompilationResult], sources: Agg[os.Path], @@ -31,7 +92,21 @@ trait ZincWorkerApi { scalacPluginClasspath: Agg[PathRef], reporter: Option[CompileProblemReporter], reportCachedProblems: Boolean - )(implicit ctx: ZincWorkerApi.Ctx): mill.api.Result[CompilationResult] + )(implicit ctx: ZincWorkerApi.Ctx): mill.api.Result[CompilationResult] = + compileMixed( + upstreamCompileOutput = upstreamCompileOutput, + sources = sources, + compileClasspath = compileClasspath, + javacOptions = javacOptions, + scalaVersion = scalaVersion, + scalaOrganization = scalaOrganization, + scalacOptions = scalacOptions, + compilerClasspath = compilerClasspath, + scalacPluginClasspath = scalacPluginClasspath, + reporter = reporter, + reportCachedProblems = reportCachedProblems, + incrementalCompilation = true + ) def discoverMainClasses(compilationResult: CompilationResult): Seq[String] diff --git a/scalalib/src/mill/scalalib/JavaModule.scala b/scalalib/src/mill/scalalib/JavaModule.scala index 5c6fd4dd8d3..a26b5f19c31 100644 --- a/scalalib/src/mill/scalalib/JavaModule.scala +++ b/scalalib/src/mill/scalalib/JavaModule.scala @@ -355,6 +355,10 @@ trait JavaModule ).equalsIgnoreCase("true") } + def zincIncrementalCompilation: T[Boolean] = T { + true + } + /** * Compiles the current module to generate compiled classfiles/bytecode. * @@ -370,7 +374,8 @@ trait JavaModule compileClasspath = compileClasspath().map(_.path), javacOptions = javacOptions(), reporter = T.reporter.apply(hashCode), - reportCachedProblems = zincReportCachedProblems() + reportCachedProblems = zincReportCachedProblems(), + incrementalCompilation = zincIncrementalCompilation() ) } diff --git a/scalalib/src/mill/scalalib/ScalaModule.scala b/scalalib/src/mill/scalalib/ScalaModule.scala index 8670cfe5a6d..819eaa2e4b8 100644 --- a/scalalib/src/mill/scalalib/ScalaModule.scala +++ b/scalalib/src/mill/scalalib/ScalaModule.scala @@ -228,7 +228,8 @@ trait ScalaModule extends JavaModule with TestModule.ScalaModuleBase { outer => compilerClasspath = scalaCompilerClasspath(), scalacPluginClasspath = scalacPluginClasspath(), reporter = T.reporter.apply(hashCode), - reportCachedProblems = zincReportCachedProblems() + reportCachedProblems = zincReportCachedProblems(), + incrementalCompilation = zincIncrementalCompilation() ) } @@ -579,7 +580,8 @@ trait ScalaModule extends JavaModule with TestModule.ScalaModuleBase { outer => compilerClasspath = scalaCompilerClasspath(), scalacPluginClasspath = semanticDbPluginClasspath(), reporter = None, - reportCachedProblems = zincReportCachedProblems() + reportCachedProblems = zincReportCachedProblems(), + incrementalCompilation = zincIncrementalCompilation() ) .map(r => SemanticDbJavaModule.copySemanticdbFiles(r.classes.path, T.workspace, T.dest / "data") diff --git a/scalalib/src/mill/scalalib/SemanticDbJavaModule.scala b/scalalib/src/mill/scalalib/SemanticDbJavaModule.scala index 5750994994c..490579bc0b1 100644 --- a/scalalib/src/mill/scalalib/SemanticDbJavaModule.scala +++ b/scalalib/src/mill/scalalib/SemanticDbJavaModule.scala @@ -15,6 +15,7 @@ trait SemanticDbJavaModule extends CoursierModule { def zincWorker: ModuleRef[ZincWorkerModule] def upstreamCompileOutput: T[Seq[CompilationResult]] def zincReportCachedProblems: T[Boolean] + def zincIncrementalCompilation: T[Boolean] def allSourceFiles: T[Seq[PathRef]] def compile: T[mill.scalalib.api.CompilationResult] def bspBuildTarget: BspBuildTarget @@ -116,7 +117,8 @@ trait SemanticDbJavaModule extends CoursierModule { (compileClasspath() ++ resolvedSemanticDbJavaPluginIvyDeps()).map(_.path), javacOptions = javacOpts, reporter = None, - reportCachedProblems = zincReportCachedProblems() + reportCachedProblems = zincReportCachedProblems(), + incrementalCompilation = zincIncrementalCompilation() ).map(r => SemanticDbJavaModule.copySemanticdbFiles(r.classes.path, T.workspace, T.dest / "data") ) diff --git a/scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala b/scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala index c093da378f0..3a93433e86a 100644 --- a/scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala +++ b/scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala @@ -23,7 +23,6 @@ import xsbti.compile.{ ClasspathOptions, CompileAnalysis, CompileOrder, - CompileProgress, Compilers, IncOptions, JavaTools, @@ -34,7 +33,6 @@ import xsbti.{PathBasedFile, VirtualFile} import java.io.File import java.util.Optional -import java.util.concurrent.ConcurrentHashMap import scala.collection.mutable import scala.ref.SoftReference import scala.util.Properties.isWin @@ -289,13 +287,14 @@ class ZincWorkerImpl( .getOrElse(Seq.empty[String]) } - def compileJava( + override def compileJava( upstreamCompileOutput: Seq[CompilationResult], sources: Agg[os.Path], compileClasspath: Agg[os.Path], javacOptions: Seq[String], reporter: Option[CompileProblemReporter], - reportCachedProblems: Boolean + reportCachedProblems: Boolean, + incrementalCompilation: Boolean )(implicit ctx: ZincWorkerApi.Ctx): Result[CompilationResult] = { compileInternal( upstreamCompileOutput = upstreamCompileOutput, @@ -305,7 +304,8 @@ class ZincWorkerImpl( scalacOptions = Nil, compilers = javaOnlyCompilers(javacOptions), reporter = reporter, - reportCachedProblems = reportCachedProblems + reportCachedProblems = reportCachedProblems, + incrementalCompilation = incrementalCompilation ) } @@ -320,7 +320,8 @@ class ZincWorkerImpl( compilerClasspath: Agg[PathRef], scalacPluginClasspath: Agg[PathRef], reporter: Option[CompileProblemReporter], - reportCachedProblems: Boolean + reportCachedProblems: Boolean, + incrementalCompilation: Boolean )(implicit ctx: ZincWorkerApi.Ctx): Result[CompilationResult] = { withCompilers( scalaVersion = scalaVersion, @@ -337,7 +338,8 @@ class ZincWorkerImpl( scalacOptions = scalacOptions, compilers = compilers, reporter = reporter, - reportCachedProblems: Boolean + reportCachedProblems: Boolean, + incrementalCompilation ) } } @@ -419,7 +421,8 @@ class ZincWorkerImpl( scalacOptions: Seq[String], compilers: Compilers, reporter: Option[CompileProblemReporter], - reportCachedProblems: Boolean + reportCachedProblems: Boolean, + incrementalCompilation: Boolean )(implicit ctx: ZincWorkerApi.Ctx): Result[CompilationResult] = { os.makeDir.all(ctx.dest) @@ -514,12 +517,17 @@ class ZincWorkerImpl( earlyAnalysisStore = None, extra = Array() ), - pr = { + pr = if (incrementalCompilation) { val prev = store.get() PreviousResult.of( prev.map(_.getAnalysis): Optional[CompileAnalysis], prev.map(_.getMiniSetup): Optional[MiniSetup] ) + } else { + PreviousResult.of( + Optional.empty[CompileAnalysis], + Optional.empty[MiniSetup] + ) }, temporaryClassesDirectory = java.util.Optional.empty(), converter = converter,