Hey everyone! Ran into a catch-22 with compiler pl...
# compiler
e
Hey everyone! Ran into a catch-22 with compiler plugin dependencies and PSI imports: • Without (using direct
com.intellij.psi.PsiElement
): IDE error highlighting works fine, but the Gradle build crashes with
NoClassDefFoundError: PsiElement
inside FIR. • With (using shaded
org.jetbrains.kotlin.com.intellij.psi.PsiElement
): The Gradle build succeeds, but IDE in-editor highlighting completely breaks. (Toggling Build and run between Gradle and IntelliJ IDEA didn't make any difference). To break the loop, I came up with this dynamic fallback hack:
Copy code
object LapisDiagnostics : KtDiagnosticsContainer() {

    //    val FACTORY by error1org.jetbrains.kotlin.com.intellij.psi.PsiElement, String(SourceElementPositioningStrategies.DECLARATION_NAME)
    val FACTORY = KtDiagnosticFactory1String(
        name = simpleNameOfLapisDiagnostics(),
        severity = Severity.ERROR,
        defaultPositioningStrategy = SourceElementPositioningStrategies.DECLARATION_NAME,
        psiType = runCatching { com.intellij.psi.PsiElement::class }
            .getOrNull() ?: org.jetbrains.kotlin.com.intellij.psi.PsiElement::class,
        rendererFactory = getRendererFactory(),
    )
}
Combined with adding both as
compileOnly
so the IDE can resolve both paths:
Copy code
dependencies {
    compileOnly(libs.kotlin.compiler)
    compileOnly(libs.kotlin.compiler.embeddable)
}
Now both the IDE and the build are happy, but it feels sketchy. Is this dynamic resolution safe to keep as a workaround, or am I setting myself up for hidden K2 pipeline issues down the road? 🤔 P.S. I looked at how other plugins handle this, and most people simply import the shaded
org.jetbrains.kotlin.com.intellij.psi.PsiElement
without overthinking it. Now I'm left wondering: do they just live with broken IDE highlighting, or does it actually work for them and something is uniquely broken in my setup? 😅
d
Use KtElement instead of PsiElement everywhere, it's not getting shaded.
e
@dmitriy.novozhilov That makes so much sense, thanks! Switched to
error1KtElement, String
, dropped the extra embeddable dependency, and everything works flawlessly now, both in the IDE and during the build. Appreciate the lightning-fast help!
👍 1
y
You could theoretically use
compileOnly(kotlin("compiler-embeddable"))
and remove the unshaded compiler, but I'd stick with
KtElement
, especially because
internal-test-framework
is unshaded IIUC, so it may not like that