Skip to content

Commit

Permalink
Drop incremental compilation state for Mill modules when version chan…
Browse files Browse the repository at this point in the history
…ges (#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 #3874

Pull request: #3884
  • Loading branch information
lefou authored Nov 1, 2024
1 parent dc0f678 commit 405c8aa
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion runner/src/mill/runner/MillBuildRootModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 405c8aa

Please sign in to comment.