I'm looking to understand the compiler plugin temp...
# compiler
m
I'm looking to understand the compiler plugin template (https://github.com/Kotlin/compiler-plugin-template) but im not really sure how to test the plugin. I see there are theses tests in testData but I don't really understand what I'm looking at. Is there any material I can be pointed towards to help me grasp what these tests are doing? What is the difference between box and diagnostics tests?
r
https://github.com/Mr3zee/compiler-plugin-template/commit/f82c2ee585f28cf6342d3a7a5de7914dc49b4b04 This commit with a guide can help you I don't remember where I found a docs explaining the compiler test framework
m
Thanks, I'll read through this
c
testData has two types of tests: box tests and diagnostic tests. The "black-box tests" are just to make sure some input kt file generates the right FIR/IR; you put standard
.kt
files in box and it diffs the generated IRs against some fir/ir dump for that kt, or creates the files if they don't exist already The "diagnostics" are for custom error reporting. You make special kt files that have notation for how some code should error and where that error should be placed, like if it should appear around the function name. You can spoof different modules and files and other things too, so it's a really robust way to test weird edge cases in general
👍 1
m
I feel a little dumb, but how do I build the compiler plugin and test locally in a project that uses it?
c
You build it with each project's respective
:build
task, and you'll just want to make a subproject that takes the gradle plugin as a plugin dependency. I took the extra step to publish them all to mavenLocal and import them from there, so I could make absolutely sure every dependency resolved correctly. I'm not sure how you'd do it without doing that, actually.
m
so you're saying I can make another subproject in the compiler plugin project that depends on the plugin? and then make use of it to confirm functionality? I almost never do multi-module projects so its a bit new to me.
also if I publish to mavenlocal, which project to I build so I can add it as a dependency for an external project? Thank you for offering help/insight
c
Here, take a look at mine: https://github.com/cbrandt77/Kotlin-OverloadableSetters, and the test project is
/test-project/build.gradle.kts
. You need to publish not just the compiler plugin, but the gradle plugin and annotations projects too. You'll also need to make sure the gradle plugin is set up for your compiler plugin's path instead of the template's. But once that's done, the gradle plugin's configured by default to automatically add the other two as dependencies to whatever project it's applied to.
❤️ 1
In that project, the publishing is standardized through the convention plugin
/buildSrc/src/main/kotlin/module.publication.gradle.kts
, which you'll see imported as
id("module-publication"
in the three main projects. (If you don't know, convention plugins are basically a complicated version of pasting that file into your buildscript.)
Thank @Youssef Shoaib [MOD] for all that stuff
❤️ 1
If you want me to explain anything in the project, let me know
m
I'll give it some real effort and if I'm stuck I might take you up on that
I did some arrow meta stuff many years back allowed interacting with the IR before k2 came out, so a lot of the actual code gen/alteration stuff its familiar. Just this project structure and how it wires in is a bit confusing initially
y
I'm happy to help as well! Here's 3 compiler plugins I've written recently. They all use the same publishing plugin, but the
testData
might be helpful!
d
The "black-box tests" are just to make sure some input kt file generates the right FIR/IR; you put standard .kt files in box and it diffs the generated IRs against some fir/ir dump for that kt, or creates the files if they don't exist already,
Actually them also test the runtime behavior. After the code from testdata is compiled, the test framework loads compiled classes, finds and executes the
fun box(): String
function and checks if the result is
"OK"
.
☝️ 1
m
@Caleb B looking at your project, how do I build the sub projects fore the test-project?
I get this error: * What went wrong: Plugin [id: 'cb77.lang.plugins.kt.overloadablesetters', version: '1.0-SNAPSHOT'] was not found in any of the following sources:
if i comment out the dependency in the test project build file I can get the project to build, is there a more sane way to do that?
I've seemingly gotten the projects to build but I cannot seem to publish to maven local, I run that task and is complains about a signature. I'm sure its user error on my end
Execution failed for task 'gradle pluginsignOverloadableSettersPluginPluginMarkerMavenPublication'.
> Cannot perform signing task 'gradle pluginsignOverloadableSettersPluginPluginMarkerMavenPublication' because it has no configured signatory
c
You'll need to look up how the signing plugin works. Once you have it configured, you'll stop getting that error.
m
okay, was able to publish to local and import it in the test-project, doesn't seem like its working though? Only following up on your plugin in the test projects just to have a baseline of a project that works when published.
c
Oh, my plugin doesn't work in the IDE atm due to an IDEA bug. Nothing to do with your build setup. If you're able to publish and import, you're good to go.
👍 1