I'm progressing in building a compiler plugin. Im ...
# compiler
w
I'm progressing in building a compiler plugin. Im looking for tools to debug and test my work. Find it hard to debug en get an overview of the tree. What are best practices to write unit tests with as input a sample Kotlin file? Is it possible to run the compiler and place a breakpoint to inspect the output?
👌 1
h
Use the official kotlin-compiler-test-framework and this template https://github.com/Kotlin/compiler-plugin-template
2
You can debug the tests as usual. the box tests compiles your code, while diagnostic tests are used to test error messages.
m
I've also used
Copy code
kotlin.compiler.execution.strategy=in-process
IIRC, this allows to put regular breakpoints too
h
That’s only needed if you want to debug the compiler when not using the test framework. For tests, it is not needed, and the framework is incredible nice to use.
m
Indeed 👍
I kind of like debugging "in real life conditions" because I get IDE support (which I don't think you get in the
box()
functions?)
FWIW, I started abstracting the test framework Gradle code in a separate plugin there: https://github.com/GradleUp/kctf/
It's far from perfect but it's working for simple cases
h
m
Ahah nice!
I like my name better because it doesn't clash with https://github.com/zacsweers/kotlin-compile-testing
h
Yeah, you don’t have ide support in the box test files, but you should create tests anyway
👍 1
m
I wish the Kotlin team would separate the test generator from the test runtime. It's a bit convoluted at the moment
j
Check the diagnostic tests with the official framework to see something similar to what you can get on the IDE
For example:
Copy code
<!NON_DATA_CLASS_KOPY_ANNOTATED!>@Kopy<!> class Baz(val qux: Qux, val text: String)
https://github.com/JavierSegoviaCordoba/kopy/blob/c694e633711bd92235ea77a917250a7d[…]-data/diagnostics/missing-data-class/simple-1-level-deep-set.kt
Copy code
fun diagnostics() {
    val foo1 = <!INVISIBLE_REFERENCE!>Foo<!>(number = 7, letter = 'W')

    val foo21 = foo1 <!INVISIBLE_REFERENCE!>copy<!> {
        <!INVISIBLE_REFERENCE!>number<!> = 42
    }
}

@Kopy
data class Foo <!DATA_CLASS_COPY_VISIBILITY_WILL_BE_CHANGED_WARNING!>private<!> constructor(val number: Int, val letter: Char)
https://github.com/JavierSegoviaCordoba/kopy/blob/main/kopy-compiler/test-data/diagnostics/visibility/private.kt
w
Tnx I will have a look at the options