Kotlin-dsl-samples: kotlin-compiler-embeddable version clash when applying kotlin-gradle-plugin via buildSrc

Created on 25 Oct 2018  Â·  7Comments  Â·  Source: gradle/kotlin-dsl-samples

There seem to be a clash between dependencies of the kotlin-compiler-embeddable used to compile the buildSrc module and the kotlin-gradle-plugin used in the rest of the build when I try to apply or configure the plugin from another plugin placed in buildSrc. As I have to add kotlin-gradle-plugin to the classpath of buildSrc in this case (instead of applying in from the plugins block of the root project), transitive dependency resolution seems to select the incorrect version of kotlin-compiler-embeddable (1.2.61 instead of 1.2.71), leading to a build failure due to class incompatibility.

Expected Behavior

When adding kotlin-gradle-plugin as a dependency in the buildSrc module, the appropriate Kotlin compiler should be present in the classpath of the rest of the build (as if kotlin-gradle-plugin was applied as a plugin in the root project, with explicitly specified version).

Current Behavior

When trying to write a plugin inside buildSrc that applies kotlin-gradle-plugin:1.2.71, the build fails with the following exception:

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':jani-model:compileKotlin'.
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:110)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:77)
        at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
        at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:59)
        at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
        at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:59)
        at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:101)
        at org.gradle.api.internal.tasks.execution.FinalizeInputFilePropertiesTaskExecuter.execute(FinalizeInputFilePropertiesTaskExecuter.java:44)
        at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:91)
        at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:62)
        at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:59)
        at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
        at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
        at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
        at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.run(EventFiringTaskExecuter.java:51)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:300)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:292)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:174)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:90)
        at org.gradle.internal.operations.DelegatingBuildOperationExecutor.run(DelegatingBuildOperationExecutor.java:31)
        at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter.execute(EventFiringTaskExecuter.java:46)
        at org.gradle.execution.taskgraph.LocalTaskInfoExecutor.execute(LocalTaskInfoExecutor.java:42)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareWorkItemExecutor.execute(DefaultTaskExecutionGraph.java:277)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareWorkItemExecutor.execute(DefaultTaskExecutionGraph.java:262)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$ExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:135)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$ExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:130)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$ExecutorWorker.execute(DefaultTaskPlanExecutor.java:200)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$ExecutorWorker.executeWithWork(DefaultTaskPlanExecutor.java:191)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$ExecutorWorker.run(DefaultTaskPlanExecutor.java:130)
        at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
        at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
        at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
Caused by: java.lang.NoSuchMethodError: org.jetbrains.kotlin.incremental.BuildUtilKt.makeModuleFile(Ljava/lang/String;ZLjava/io/File;Ljava/lang/Iterable;Ljava/lang/Iterable;Ljava/lang/Iterable;Ljava/lang/Iterable;Ljava/lang/Iterable;)Ljava/io/File;
        at org.jetbrains.kotlin.compilerRunner.GradleCompilerRunner.runJvmCompiler(GradleKotlinCompilerRunner.kt:102)
        at org.jetbrains.kotlin.gradle.tasks.KotlinCompile.callCompiler$kotlin_gradle_plugin(Tasks.kt:393)
        at org.jetbrains.kotlin.gradle.tasks.KotlinCompile.callCompiler$kotlin_gradle_plugin(Tasks.kt:297)
        at org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile.execute(Tasks.kt:259)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
        at org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:50)
        at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)
        at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:131)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:300)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:292)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:174)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:90)
        at org.gradle.internal.operations.DelegatingBuildOperationExecutor.run(DelegatingBuildOperationExecutor.java:31)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:120)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:99)
        ... 31 more

The reason for this seems to be the incorrect version of kotlin-compiler-embeddable:

$ ../gradlew dependencyInsight --dependency org.jetbrains.kotlin:kotlin-compiler-embeddable 
> Task :dependencyInsight
org.jetbrains.kotlin:kotlin-compiler-embeddable:1.2.61 (selected by rule)
   variant "runtime" [
      org.gradle.status = release (not requested)
      Requested attributes not found in the selected variant:
         org.gradle.usage  = java-api
   ]

org.jetbrains.kotlin:kotlin-compiler-embeddable:1.2.71 -> 1.2.61
+--- org.jetbrains.kotlin:kotlin-android-extensions:1.2.71
|    \--- org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.71
|         \--- compileClasspath
+--- org.jetbrains.kotlin:kotlin-annotation-processing-gradle:1.2.71
|    \--- org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.71 (*)
\--- org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.71 (*)

(*) - dependencies omitted (listed previously)

It seems that the dependency used by the embedded Kotlin version (1.2.61) "leaks" from the kotlinCompiler configuration of buildSrc to the rest of the configurations.

There is no version clash if I apply the kotlin-gradle-plugin to the root project (with explicitly specified version). Of course, in this case it is impossible to add it to the buildSrc dependencies, since no version can be specified for plugins already on the build classpath. This prevents me from writing a custom plugin in buildSrc that configures the Kotlin compiler.

There is also no version clash if I downgrade to Kotlin 1.2.61 in the project.

As a workaround I can force the embeddable Kotlin compiler to 1.2.71 in the buildSrc as follows:

configurations.all {
    val isKotlinCompiler = name == "embeddedKotlin" || name.startsWith("kotlin") || name.startsWith("kapt")
    if (!isKotlinCompiler) {
        resolutionStrategy.eachDependency {
            if (requested.group == "org.jetbrains.kotlin" && requested.module.name == "kotlin-compiler-embeddable") {
                useVersion("1.2.71")
            }
        }
    }
}

Simply using force did not help, because the dependency version is apparently _selected by rule_. I presume I have to write another rule to override in this case.

Context

I'm trying to store common configuration across modules using plugins defined in buildSrc in a project where the modules, the buildSrc plugins, and the build scripts are all written in Kotlin.

As an example of such configuration, see https://github.com/FTSRG/theta-jani-interchange/blob/84cbe35d2e7b9661319fb450694099c770b9a656/buildSrc/src/main/kotlin/hu/bme/mit/inf/theta/interchange/jani/buildsrc/KotlinConventions.kt

Steps to Reproduce (for bugs)

  1. Use kotlin-dsl in buildSrc.
  2. Add compile(kotlin("gradle-plugin", "1.2.71")) to the buildSrc module dependencies
  3. Create a plugin in buildSrc that runs apply<KotlinPlatformJvmPlugin>() in its apply method
  4. Apply the plugin in a subproject containing Kotlin code
  5. Try to ./gradlew compileKotlin

Your Environment

Relevant versions:

  • Gradle 4.10.2
  • kotlin-dsl using Kotlin 1.2.61
  • the rest of the sources using Kotlin 1.2.71

Note that the build also contains the io.gitlab.arturbosch.detekt plugin, which is applied from the plugins block of the root project as usual. Even though it is written in Kotlin 1.2.61, it does not disturb the build when kotlin-gradle-plugin is also applied from the plugins block (and not buildSrc dependencies). This highlights that there is no clash of differing Kotlin versions, unless the clash involves buildSrc.

bug kotlin-dsl-plugins

All 7 comments

This is a breaking change in the kotlin-gradle-plugin between 1.2.6x (embedded in Gradle 4.10) and 1.2.7x. Can you try with Gradle 5.0-rc-1 that embedds Kotlin 1.3.0?

I there any way I can try forcing a specific Kotlin version on Gradle 5.0-rc-1? I mean, it already embeds 1.3.0, so I can't set anything newer (yet). Is building 1.2.7x projects with Gradle 5.0 supported, despite the embedded 1.3.0 compiler?

Hi. I'm trying to update Kotlin version to 1.4.0-rc but it's not working. I have the same error as @kris7t.
It seems that BuildScr forces kotlin-compiler-embeddable to 1.3.72, so I can see the errors like:

e: java.lang.NoSuchMethodError: org.jetbrains.kotlin.cli.common.messages.MessageCollector$DefaultImpls.report$default(Lorg/jetbrains/kotlin/cli/common/messages/MessageCollector;Lorg/jetbrains/kotlin/cli/common/messages/CompilerMessageSeverity;Ljava/lang/String;Lorg/jetbrains/kotlin/cli/common/messages/CompilerMessageSourceLocation;ILjava/lang/Object;)V

or

e: java.lang.IncompatibleClassChangeError: Found class org.jetbrains.kotlin.backend.common.extensions.IrPluginContext, but interface was expected

I've tried Gradle 6.5 and 6.6-rc-3.

@TomasKuhn were you able to find a solution to this?

I also tried with gradle 6.6-rc-4 and nothing :/

There is no solution yet. It's defined in Gradle, so we have to wait on new Gradle version which use Kotlin 1.4 for kotlin-dsl

This Repo is only about samples. Maybe the is already an issue with that at
gradle/gradle? Maybe worth to check it out...

On Fri, Jul 31, 2020, 9:15 AM Tomáš Kuhn notifications@github.com wrote:

There is no solution yet. It's defined in Gradle, so we have to wait on
new Gradle version which use Kotlin 1.4 for kotlin-dsl

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/gradle/kotlin-dsl-samples/issues/1207#issuecomment-666972012,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ACOBQ6562IK3CJWW4NZQWELR6JVRFANCNFSM4F7I4S3A
.

Was this page helpful?
0 / 5 - 0 ratings