com-lihaoyi / mill

Mill is a fast JVM build tool that supports Java and Scala. 2-3x faster than Gradle and 5-10x faster than Maven for common workflows, Mill aims to make your project’s build process performant, maintainable, and flexible
https://mill-build.org/
MIT License
2.03k stars 319 forks source link

Make `CompilationResult` more generic #2781

Open lefou opened 11 months ago

lefou commented 11 months ago

Curent Limitations

Current CompilationResult is very specific for the zinc compiler.

case class CompilationResult(analysisFile: os.Path, classes: PathRef)

It's hard to integrate other compilers easily into Mill:

There are also other shortcomings:

Solution

Mae ComilationResult extensible, so that additional compiler specific details can be attached.

lihaoyi commented 11 months ago

Do they even need to be the same type? It feels to me like those can be completely different case classes unless there's some reason we want to abstract over them, and it's not clear to me what that reason is

lefou commented 11 months ago

The reason is the shared parent module JavaModule, which allows a KotlinModule to integrate in BSP, IDEA and all other workflows seamlessly. After all, it's just another compiler but everything else is the same.

lefou commented 11 months ago

It is completely transparent for a ScalaModule, whether it's moduleDeps is a Java, Scala, Kotlin, or Something module. But we need to trigger compilation and we need to be able to consume the compilation output (compile.dest/classes). If we now also can extract the incremental info, we can also optimize compilation speed, which is the case when using zinc, but currently not when using other compilers.

lihaoyi commented 11 months ago

got it, that makes sense

lefou commented 11 months ago

We have various ways to implement this:

  1. Inheritance
  2. Composition

1. Inheritance

trait CompileResult {
  classes: PathRef
}

case class CompilationResult(analysisFile: os.Path, classes: PathRef) extends CompileResult

// ScalaModule
def compile: T[CompiationResult]

trait KotlinIncrDetails {
  // ...
}

class KotlinCompileResult extends CompileResult with KotlinIncrDetails

// KotlinModule
def compile: T[CompileResult with KotlinIncrDetails]
// or
def compile: T[KotlinCompileResult]

2. Composition

case class CompilationResult(
  analysisFile: os.Path, // for compatibility
  classes: PathRef,
  extras: Map[String, AnyRef] = Map()
)

// KotlinModule
def compile: T[CompilationResult] = {
  // ...
  CompilationResult(
    dummyFile, // what we do this currently
    T.dest / "classes",
    extras = Map("kotlinIncrDetails", details)
  )
}

I think I like 1. Inheritance more, as it might better play with upickle. But it requires a bit of discipline to use not the same def names for different Compilers, so we can better encode multiple compilers into one result (for what ever reason, e.g. an aspect compiler still able to provide incremental details).

lihaoyi commented 11 months ago

Inheritance with uPickle will only work if the trait is sealed. If you want jt to be extensible from downstream third party modules, you'll have to use some kind of composition

lefou commented 2 months ago

As part of the discussion of a PR experimenting with the new scalac/zinc pipelining feature (https://github.com/com-lihaoyi/mill/pull/3202), we had the idea of realizing compilation with a set of (co-working) targets and a worker, encapsulating some compiler internals. See comment https://github.com/com-lihaoyi/mill/pull/3202#issuecomment-2165293750 and following.