tschuchortdev / kotlin-compile-testing

A library for testing Kotlin and Java annotation processors, compiler plugins and code generation
Mozilla Public License 2.0
659 stars 63 forks source link

Make it easier to use the output of a compilation as an input for another compilation #216

Closed vRallev closed 2 years ago

vRallev commented 2 years ago

Feeding a new compilation with the output from a previous compilation isn't very hard, although I believe there could be a nicer API:

kotlinCompilation.classpaths += previousCompilationResult.outputDirectory

The tricky part is the result of the 2nd compilation where the class loader doesn't respect the output from the previous compilation. The workaround is to create a new class loader that uses all compilation results as input:

URLClassLoader(
  allCompilationResults.map { it.outputDirectory.toURI().toURL() }.toTypedArray(),
  this::class.java.classLoader
)

This is quite annoying, because one needs to be aware that result.classLoader gives misleading results.

I wish the library would provide a better solution for this. If you're open for that, then I'm more than happy to create a PR.

tschuchortdev commented 2 years ago

Sounds like a good idea. If you want to make a PR it would be very appreciated.

The tricky part is the result of the 2nd compilation where the class loader doesn't respect the output from the previous compilation

Can you elaborate on that? Perhaps it would be better if the result.classLoader included kotlinCompilation.classpaths by default.

vRallev commented 2 years ago

Perhaps it would be better if the result.classLoader included kotlinCompilation.classpaths by default.

Ah, that's even better than what I suggested above. I've created a PR.