tschuchortdev / kotlin-compile-testing

A library for testing Kotlin and Java annotation processors, compiler plugins and code generation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I test incremental compilation?

nbirillo opened this issue · comments

Hi! Can I imitate the incremental compilation process? I mean I have the following test case: I should compile a project that uses my compiler plugin, check results, save them, modify several files in the project, recompile the project and check new results. The problem is that I don't want to recompile the full project since it is important to check if clean (old, not dirty) files were analyzed by my compiler plugin.

KCT currently uses the same interface to the compiler as kotlinc. If you can do it with kotlinc then you should be able to do it in the same way with KCT. I'm afraid that incremental compilation is probably not supported by kotlinc and instead implemented in the kotlin gradle plugin in conjunction with new APIs in the compiler. That stuff is currently out of scope for me because the maintainance burden of exactly reproducing the logic from the gradle plugin would be huge, though I'm open to adding incremental compilation support if you're willing to do the ground work. I suppose in order to enable incremental compilation you'd have to look at the compiler source code to see what K2JVMCompiler (which is the class that represents the kotlinc cli) does to set up the compilation (creating the KotlinCoreEnvironment) and reimplement that in KCT. Then you have to look at the kotlin gradle plugin to find out how it keeps track of the intermediate compilation state (GenerationState maybe?) and triggers re-compilation on file changes. If you figure out how it works let me know.

Oh, thank you for your response! I researched the source code of Incremental compilation in the Kotlin repository and discussed some points with its developers and decided, for me, it would be easier to implement these tests directly.
I use makeIncrementally function from the org.jetbrains.kotlin.incremental package. I created a list of the necessary arguments (e.g. path to the classpath and to my plugin jar). It has K2JVMCompilerArguments type.
The main pipeline is the following:
I compiled my project and got the compiler files and the compiler cache (in the folders, that I sent). After that, I apply some modifications to files and compile it again (with the old cache)/ In this case, the compiler knows the previous results and incremental compilation works.
To get the program output, I just run the compiled Main class.

I can share my tests classes if you want. Also, I can research how the incremental compilation process can be adopted to KCT later

I would be interested in your test classes. If you can find out how it can be integrated with KCT, that's even better.

You can find them here

I can search for a way how it can be integrated into KCT after several days since currently, I am very busy at work