Open sihutch opened 4 months ago
Thanks for the issue. Looks like some kind of classpath issue. It reminds me of a case in a real (non-test) project that uses Kotlin but wasn't applying any Kotlin plugin to the root project. I am not entirely sure how the classpath works in the context of a unit test like you have, but as I stare at the error and the build scan you shared, it occurs to me that my plugin uses Kotlin DSL, and that missing class is from the Kotlin DSL -- that is, the Gradle jar itself. Maybe try adding testRuntimeOnly(gradleApi())
to your build script. That may not work though because I'm not certain that dependencies also contains the Kotlin DSL classes. My IDE tells me that class comes from gradle-kotlin-dsl-8.5.jar
.
More generally, I'm not sure it makes a lot of sense to try to unit test this. I recommend writing functional tests instead. I have a plugin and companion libraries for that here if you're interested.
Build scan link https://scans.gradle.com/s/segppkfrzjmca/
Plugin version 1.29.0
Gradle version 7.6.1 (Same bug exhibits with Gradle 8.0)
JDK version 11.0.16 (Amazon.com Inc. 11.0.16+8-LTS)
Describe the bug
We have tests that build
Projects
usingorg.gradle.testfixtures.ProjectBuilder.
We apply thecom.autonomousapps.dependency-analysis
plugin on theProject
during our test. If the testProject
does not have a subProject
then plugin application works. However, if the testProject
does have a subProject
, then ajava.lang.NoClassDefFoundError: org/gradle/kotlin/dsl/PluginAwareExtensionsKt
exception is thrown when thecom.autonomousapps.subplugin.RootPlugin
attempts toconditionallyApplyToSubprojects
as shown in the snippet from the stack trace below (The full stack trace can be seen on the build scan [here](https://scans.gradle.com/s/segppkfrzjmca/tests/task/:test/details/org.example.ProjectBuilderTest/canApplyPluginOnProjectWithSubProject()?expanded-stacktrace=WyIwIiwiMC0xIl0&focused-exception-line=0-1-0&top-execution=1))To Reproduce Steps to reproduce the behavior:
org.gradle.api.Project
using theorg.gradle.testfixtures.ProjectBuilder
org.gradle.api.Project
using theorg.gradle.testfixtures.ProjectBuilder
setting theProject
from step 1 as its parentcom.autonomousapps.dependency-analysis
plugin to the root projectI created a Junit 5 test to help me diagnose and have included it below (note this test fails as the
NoClassDefFoundError
exception is thrown)Also note that if we remove the line
ProjectBuilder.builder().withParent(rootProject).build();
from the code, then the test passes.Expected behavior We expect the plugin application to work on a
Project
containing a subProject
in the same way as it does on aProject
without any subProject
.Additional context I'm not sure that is related, but I noticed this line [Fix] Don't leak Kotlin stdlib from shaded dependencies. from the 1.29.0 release notes.
I tested against the previous plugin version, i.e. 1.28.0. On this version, a Kotlin
java.lang.NoClassDefFoundError
is thrown when we apply the plugin on anyProject
in our test, i.e. even if theProject
does not have any subProject
.Notice from the stack trace below that for version 1.28.0, the exception was thrown by a call to
com.autonomousapps.DependencyAnalysisPlugin.applyForProject
as opposed to version 1.29.0 where the exception was thrown by a call tocom.autonomousapps.subplugin.RootPlugin$conditionallyApplyToSubprojects
I included this context as it appears that something related was fixed in the 1.29.0 release but that it somehow wasn't fixed for the sub
Project
case.