kotlin icon indicating copy to clipboard operation
kotlin copied to clipboard

Load plugins using DependencyShadingPluginClassLoader

Open jimgoog opened this issue 2 years ago • 6 comments

DependencyShadingPluginClassLoader will load a plugin jar, applying the necessary shading as required by the variant of the Kotlin Compiler which is consuming the plugin. This allows shaded and unshaded plugins to be used interchangably, thus resolivng issues like KT-25596

jimgoog avatar Jun 09 '22 10:06 jimgoog

This leads to the following warning on compilation of any file:

$ kotlinc main.kt
warning: error on loading script definition org.jetbrains.kotlin.mainKts.MainKtsScript: bytecodeLoader.getResourceAsStream(path) must not be null

udalov avatar Jun 09 '22 22:06 udalov

Pushed a new version with a fix for the null warning.

jimgoog avatar Jun 10 '22 12:06 jimgoog

And I believe that we need some tests for this feature, it exposes quite a complicated behavior, so it will be nice to have it tested. I think that it shouldn't be difficult to use -embeddable compiler and try to load unshaded plugin into it, using existing plugins tests.

ligee avatar Jun 13 '22 14:06 ligee

Pushed updates to address code review comments, so those should be good now.

With regards to unit tests, maybe I'm just not familiar enough with this repository setup. Can you point me at a unit test which uses an embedded jar and a built embedded plugin which I can copy? Most unit tests seem to use the main implementation classpath, I'm not sure how to get the built artifacts for use in a test.

jimgoog avatar Jun 14 '22 09:06 jimgoog

About the test: I managed to reproduce the following behavior: This compiles successfully:

java -cp dist/kotlinc/lib/kotlin-compiler.jar:dist/kotlinc/lib/kotlin-stdlib.jar:dist/kotlinc/lib/kotlin-reflect.jar:dist/kotlinc/lib/trove4j.jar:dist/kotlinc/lib/annotations-13.0.jar:<substitute-path-to-jna>/jna-5.6.0.jar org.jetbrains.kotlin.cli.jvm.K2JVMCompiler -no-stdlib -cp dist/kotlinc/lib/kotlin-stdlib.jar:<substitute-path-to-lombok>/lombok-1.18.16.jar -Xplugin=dist/kotlinc/lib/lombok-compiler-plugin.jar -d tmp tmp/GetterSetterExample.java tmp/test.kt

where contents of the sources tmp/GetterSetterExample.java and tmp/test.kt are taken from plugins/lombok/testData/box/simple.kt (and path to jna in fact needed only for embeddable compiler below)

while the same command with embeddable compiler now fails with exception:

java -cp prepare/compiler-embeddable/build/libs/kotlin-compiler-embeddable-1.7.255-SNAPSHOT.jar:dist/kotlinc/lib/kotlin-stdlib.jar:dist/kotlinc/lib/kotlin-reflect.jar:dist/kotlinc/lib/trove4j.jar:dist/kotlinc/lib/annotations-13.0.jar:<substitute-path-to-jna>/jna-5.6.0.jar org.jetbrains.kotlin.cli.jvm.K2JVMCompiler -no-stdlib -cp dist/kotlinc/lib/kotlin-stdlib.jar:<substitute-path-to-lombok>/lombok-1.18.16.jar -Xplugin=dist/kotlinc/lib/lombok-compiler-plugin.jar -d tmp tmp/GetterSetterExample.java tmp/test.kt

:

Caused by: java.lang.AbstractMethodError: Receiver class org.jetbrains.kotlin.lombok.LombokComponentRegistrar does not define or inherit an implementation of the resolved method 'abstract void registerProjectComponents(org.jetbrains.kotlin.com.intellij.mock.MockProject, org.jetbrains.kotlin.config.CompilerConfiguration)' of interface org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar.

exactly because embeddable compiler is used with unshaded plugin.

So, the idea is to add a test (to prepare/compiler-embeddable/tests/kotlin/org/jetbrains/kotlin/compiler/embeddable/CompilerEmbeddableSmokeTests.kt) that runs it, and with your changes it should compile successfully.

ligee avatar Jun 15 '22 14:06 ligee

I was able to reproduce the exception. I had forgotten that the shading technique used in this repository appears to just blindly replace character sequences including string constants, so it was rewriting my if check. That rewrite behavior consistently shocks me every time, I eagerly await/hope we can remove it someday. Anyway, I pushed a fix.

I think I have a sense of what you're looking for with CompilerEmbeddableSmokeTests, still working on adding such a test case.

jimgoog avatar Jun 16 '22 08:06 jimgoog