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 {