From 405c8aa4d4b8bbebb55f1ce80e9142e39635787c Mon Sep 17 00:00:00 2001 From: Tobias Roeser Date: Fri, 1 Nov 2024 20:30:10 +0100 Subject: [PATCH] Drop incremental compilation state for Mill modules when version changes (#3884) Mill used various tricks to map build scripts written by users to generated enhanced versions, including package objects and other stuff. Since some of these techniques may result in Zinc undercompilation, we want avoid downstream issues, especally in the `methodCodeHashSignatures` task, which is sensitive to inconsistent bytecode. We trait potentially longer compilation against incorrect or too coarse code change detection, which may invalidate a lot more downstream tasks than needed due to invalid data. Fix https://github.com/com-lihaoyi/mill/issues/3874 Pull request: https://github.com/com-lihaoyi/mill/pull/3884 --- .../src/mill/runner/MillBuildRootModule.scala | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/runner/src/mill/runner/MillBuildRootModule.scala b/runner/src/mill/runner/MillBuildRootModule.scala index 2a193463f59..855520b6087 100644 --- a/runner/src/mill/runner/MillBuildRootModule.scala +++ b/runner/src/mill/runner/MillBuildRootModule.scala @@ -7,7 +7,7 @@ import mill.define.{Discover, Task} import mill.scalalib.{BoundDep, Dep, DepSyntax, Lib, ScalaModule} import mill.util.CoursierSupport import mill.util.Util.millProjectModule -import mill.scalalib.api.Versions +import mill.scalalib.api.{CompilationResult, Versions} import mill.main.client.OutFiles._ import mill.main.client.CodeGenConstants.buildFileExtensions import mill.main.{BuildInfo, RootModule} @@ -261,6 +261,49 @@ abstract class MillBuildRootModule()(implicit /** Used in BSP IntelliJ, which can only work with directories */ def dummySources: Sources = Task.Sources(T.dest) + + def millVersion = T.input { BuildInfo.millVersion } + + override def compile: T[CompilationResult] = Task(persistent = true) { + val mv = millVersion() + + val prevMillVersionFile = T.dest / s"mill-version" + val prevMillVersion = Option(prevMillVersionFile) + .filter(os.exists) + .map(os.read(_).trim) + .getOrElse("?") + + if (prevMillVersion != mv) { + // Mill version changed, drop all previous incremental state + // see https://github.com/com-lihaoyi/mill/issues/3874 + T.log.debug( + s"Detected Mill version change ${prevMillVersion} -> ${mv}. Dropping previous incremental compilation state" + ) + os.remove.all(T.dest) + os.makeDir(T.dest) + os.write(prevMillVersionFile, mv) + } + + // copied from `ScalaModule` + zincWorker() + .worker() + .compileMixed( + upstreamCompileOutput = upstreamCompileOutput(), + sources = Agg.from(allSourceFiles().map(_.path)), + compileClasspath = compileClasspath().map(_.path), + javacOptions = javacOptions() ++ mandatoryJavacOptions(), + scalaVersion = scalaVersion(), + scalaOrganization = scalaOrganization(), + scalacOptions = allScalacOptions(), + compilerClasspath = scalaCompilerClasspath(), + scalacPluginClasspath = scalacPluginClasspath(), + reporter = T.reporter.apply(hashCode), + reportCachedProblems = zincReportCachedProblems(), + incrementalCompilation = zincIncrementalCompilation(), + auxiliaryClassFileExtensions = zincAuxiliaryClassFileExtensions() + ) + } + } object MillBuildRootModule {