Skip to content

Commit

Permalink
Pass auxiliary class files to zinc so they are deleted together
Browse files Browse the repository at this point in the history
Fixes #3068
`*.tasty`, `*.nir` and `*.sjsir` files were not cleaned up
by zinc when the companion class files were deleted.
This passes the right `AuxiliaryClassFiles` to zinc
  • Loading branch information
lolgab committed Mar 9, 2024
1 parent 8b46150 commit 793ad6a
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
object foo
20 changes: 20 additions & 0 deletions integration/feature/auxiliary-class-files/repo/build.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Issue https://github.com/com-lihaoyi/mill/issues/3068
import mill._
import mill.scalalib._
import mill.scalajslib._
import mill.scalanativelib._

object app extends Module {
trait Common extends ScalaModule {
def millSourcePath = super.millSourcePath / os.up
def scalaVersion = "3.4.0"
}

object jvm extends Common
object js extends Common with ScalaJSModule {
def scalaJSVersion = "1.15.0"
}
object native extends Common with ScalaNativeModule {
def scalaNativeVersion = "0.4.17"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package mill.integration

import utest._

// Regress test for issue https://github.com/com-lihaoyi/mill/issues/1901
object AuxiliaryClassFilesTests extends IntegrationTestSuite {
val tests: Tests = Tests {
test("tasty files are deleted together with companion class files") {
initWorkspace()
eval("app.jvm.compile")

val classes = wd / "out" / "app" / "jvm" / "compile.dest" / "classes"
val firstRun = os.list(classes).map(_.last)

os.remove(wd / "app" / "src" / "foo.scala")

eval("app.jvm.compile")

val secondRun = os.list(classes).map(_.last)

assert(firstRun == Seq("foo$.class", "foo.class", "foo.tasty"))
assert(secondRun == Seq.empty)
}

test("nir files are deleted together with companion class files") {
initWorkspace()
eval("app.native.compile")

val classes = wd / "out" / "app" / "native" / "compile.dest" / "classes"
val firstRun = os.list(classes).map(_.last)

os.remove(wd / "app" / "src" / "foo.scala")

eval("app.native.compile")

val secondRun = os.list(classes).map(_.last)

assert(firstRun == Seq("foo$.class", "foo$.nir", "foo.class", "foo.nir", "foo.tasty"))
assert(secondRun == Seq.empty)
}

test("sjsir files are deleted together with companion class files") {
initWorkspace()
eval("app.js.compile")

val classes = wd / "out" / "app" / "js" / "compile.dest" / "classes"
val firstRun = os.list(classes).map(_.last)

os.remove(wd / "app" / "src" / "foo.scala")

eval("app.js.compile")

val secondRun = os.list(classes).map(_.last)

assert(firstRun == Seq("foo$.class", "foo$.sjsir", "foo.class", "foo.tasty"))
assert(secondRun == Seq.empty)
}
}
}
42 changes: 39 additions & 3 deletions scalalib/api/src/mill/scalalib/api/ZincWorkerApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,30 @@ trait ZincWorkerApi {
compileClasspath: Agg[os.Path],
javacOptions: Seq[String],
scalaVersion: String,
platformSuffix: String,
scalaOrganization: String,
scalacOptions: Seq[String],
compilerClasspath: Agg[PathRef],
scalacPluginClasspath: Agg[PathRef],
reporter: Option[CompileProblemReporter],
reportCachedProblems: Boolean,
incrementalCompilation: Boolean
incrementalCompilation: Boolean,
)(implicit ctx: ZincWorkerApi.Ctx): mill.api.Result[CompilationResult] =
compileMixed(
upstreamCompileOutput = upstreamCompileOutput,
sources = sources,
compileClasspath = compileClasspath,
javacOptions = javacOptions,
scalaVersion = scalaVersion,
platformSuffix = platformSuffix,
scalaOrganization = scalaOrganization,
scalacOptions = scalacOptions,
compilerClasspath = compilerClasspath,
scalacPluginClasspath = scalacPluginClasspath,
reporter = reporter,
reportCachedProblems = reportCachedProblems
): @nowarn("cat=deprecation")
reportCachedProblems = reportCachedProblems,
incrementalCompilation = incrementalCompilation
)

/** Compile a mixed Scala/Java or Scala-only project */
@deprecated("Use override with `incrementalCompilation` parameter", "Mill 0.11.6")
Expand All @@ -99,6 +102,7 @@ trait ZincWorkerApi {
compileClasspath = compileClasspath,
javacOptions = javacOptions,
scalaVersion = scalaVersion,
platformSuffix = "",
scalaOrganization = scalaOrganization,
scalacOptions = scalacOptions,
compilerClasspath = compilerClasspath,
Expand All @@ -108,6 +112,38 @@ trait ZincWorkerApi {
incrementalCompilation = true
)

/** Compile a mixed Scala/Java or Scala-only project */
@deprecated("Use override with `platformSuffix` parameter", "Mill 0.11.8")
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,
platformSuffix = "",
scalaOrganization = scalaOrganization,
scalacOptions = scalacOptions,
compilerClasspath = compilerClasspath,
scalacPluginClasspath = scalacPluginClasspath,
reporter = reporter,
reportCachedProblems = reportCachedProblems,
incrementalCompilation = incrementalCompilation
)

def discoverMainClasses(compilationResult: CompilationResult): Seq[String]

def docJar(
Expand Down
2 changes: 2 additions & 0 deletions scalalib/src/mill/scalalib/ScalaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ trait ScalaModule extends JavaModule with TestModule.ScalaModuleBase { outer =>
compileClasspath = compileClasspath().map(_.path),
javacOptions = javacOptions(),
scalaVersion = sv,
platformSuffix = platformSuffix(),
scalaOrganization = scalaOrganization(),
scalacOptions = allScalacOptions(),
compilerClasspath = scalaCompilerClasspath(),
Expand Down Expand Up @@ -659,6 +660,7 @@ trait ScalaModule extends JavaModule with TestModule.ScalaModuleBase { outer =>
(compileClasspath() ++ resolvedSemanticDbJavaPluginIvyDeps()).map(_.path),
javacOptions = javacOpts,
scalaVersion = sv,
platformSuffix = platformSuffix(),
scalaOrganization = scalaOrganization(),
scalacOptions = scalacOptions,
compilerClasspath = scalaCompilerClasspath(),
Expand Down
30 changes: 28 additions & 2 deletions scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ import sbt.internal.util.{ConsoleAppender, ConsoleOut}
import sbt.mill.SbtLoggerUtils
import xsbti.compile.{
AnalysisContents,
AuxiliaryClassFiles,
ClasspathOptions,
CompileAnalysis,
CompileOrder,
Compilers,
IncOptions,
JavaTools,
MiniSetup,
PreviousResult
PreviousResult,
ScalaJSFiles,
ScalaNativeFiles,
TastyFiles
}
import xsbti.{PathBasedFile, VirtualFile}

Expand Down Expand Up @@ -303,6 +307,7 @@ class ZincWorkerImpl(
compileClasspath = compileClasspath,
javacOptions = javacOptions,
scalacOptions = Nil,
platformSuffix = "",
compilers = javaOnlyCompilers(javacOptions),
reporter = reporter,
reportCachedProblems = reportCachedProblems,
Expand All @@ -316,6 +321,7 @@ class ZincWorkerImpl(
compileClasspath: Agg[os.Path],
javacOptions: Seq[String],
scalaVersion: String,
platformSuffix: String,
scalaOrganization: String,
scalacOptions: Seq[String],
compilerClasspath: Agg[PathRef],
Expand All @@ -337,6 +343,7 @@ class ZincWorkerImpl(
compileClasspath = compileClasspath,
javacOptions = javacOptions,
scalacOptions = scalacOptions,
platformSuffix = platformSuffix,
compilers = compilers,
reporter = reporter,
reportCachedProblems: Boolean,
Expand Down Expand Up @@ -420,6 +427,7 @@ class ZincWorkerImpl(
compileClasspath: Agg[os.Path],
javacOptions: Seq[String],
scalacOptions: Seq[String],
platformSuffix: String,
compilers: Compilers,
reporter: Option[CompileProblemReporter],
reportCachedProblems: Boolean,
Expand Down Expand Up @@ -496,6 +504,24 @@ class ZincWorkerImpl(
.map(path => converter.toVirtualFile(path.toNIO))
.toArray

val incOptions = {
val platformSpecificFiles: Array[AuxiliaryClassFiles] = platformSuffix match {
case s"_sjs$_" => Array(ScalaJSFiles.instance())
case s"_native$_" => Array(ScalaNativeFiles.instance())
case _ => Array.empty[AuxiliaryClassFiles]
}

val scalaVersionSpecificFiles: Array[AuxiliaryClassFiles] =
if(ZincWorkerUtil.isDottyOrScala3(compilers.scalac().scalaInstance().version()))
Array(TastyFiles.instance())
else
Array.empty[AuxiliaryClassFiles]

val auxiliaryClassFiles: Array[AuxiliaryClassFiles] = platformSpecificFiles ++ scalaVersionSpecificFiles

IncOptions.of().withAuxiliaryClassFiles(auxiliaryClassFiles)
}

val inputs = ic.inputs(
classpath = classpath,
sources = virtualSources,
Expand All @@ -512,7 +538,7 @@ class ZincWorkerImpl(
skip = false,
cacheFile = zincFile.toNIO,
cache = new FreshCompilerCache,
incOptions = IncOptions.of(),
incOptions = incOptions,
reporter = newReporter,
progress = None,
earlyAnalysisStore = None,
Expand Down

0 comments on commit 793ad6a

Please sign in to comment.