From 56f8fd5ca37dc1a0c5e4fa8613b1f298224dbbb9 Mon Sep 17 00:00:00 2001 From: Sergey Shanshin Date: Thu, 15 Feb 2024 17:36:08 +0300 Subject: [PATCH] Fix breaking configuration cache Usage of DependencyResolutionListener is incompatible with the configuration cache, therefore, a quick solution is to remove the listener. In the future, checking for apply of the Kover Gradle Plugin in dependencies must be done in a different way. See #537 Fixes #513 PR #538 --- .../functional/cases/NoDependencyTests.kt | 4 ++ .../gradle/plugin/DependencyCheckListener.kt | 62 ------------------- .../kover/gradle/plugin/KoverGradlePlugin.kt | 15 ----- 3 files changed, 4 insertions(+), 77 deletions(-) delete mode 100644 kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/DependencyCheckListener.kt diff --git a/kover-gradle-plugin/src/functionalTest/kotlin/kotlinx/kover/gradle/plugin/test/functional/cases/NoDependencyTests.kt b/kover-gradle-plugin/src/functionalTest/kotlin/kotlinx/kover/gradle/plugin/test/functional/cases/NoDependencyTests.kt index d7729524..1a13630d 100644 --- a/kover-gradle-plugin/src/functionalTest/kotlin/kotlinx/kover/gradle/plugin/test/functional/cases/NoDependencyTests.kt +++ b/kover-gradle-plugin/src/functionalTest/kotlin/kotlinx/kover/gradle/plugin/test/functional/cases/NoDependencyTests.kt @@ -3,13 +3,17 @@ package kotlinx.kover.gradle.plugin.test.functional.cases import kotlinx.kover.gradle.plugin.test.functional.framework.checker.checkNoAndroidSdk import kotlinx.kover.gradle.plugin.test.functional.framework.runner.buildFromTemplate import kotlinx.kover.gradle.plugin.test.functional.framework.runner.runWithParams +import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test import kotlin.test.assertContains import kotlin.test.assertFalse /** * Tests on dependency check https://github.com/Kotlin/kotlinx-kover/issues/478. + * + * Temporary disabled */ +@Disabled class NoDependencyTests { @Test fun testJvmNotApplied() { diff --git a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/DependencyCheckListener.kt b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/DependencyCheckListener.kt deleted file mode 100644 index b922f0c2..00000000 --- a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/DependencyCheckListener.kt +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package kotlinx.kover.gradle.plugin - -import kotlinx.kover.gradle.plugin.commons.* -import kotlinx.kover.gradle.plugin.commons.KoverIllegalConfigException -import kotlinx.kover.gradle.plugin.commons.KoverMarkerAttr -import org.gradle.api.artifacts.DependencyResolutionListener -import org.gradle.api.artifacts.ResolvableDependencies -import org.gradle.api.artifacts.component.ComponentIdentifier -import org.gradle.api.artifacts.component.ProjectComponentIdentifier - - -internal class DependencyCheckListener : DependencyResolutionListener { - - override fun beforeResolve(dependencies: ResolvableDependencies) { - // no-op before - } - - override fun afterResolve(dependencies: ResolvableDependencies) { - // Only Kover dependency consumers requests KoverMarkerAttr attribute - dependencies.attributes.getAttribute(KoverMarkerAttr.ATTRIBUTE) ?: return - - /* - Gradle can resolve a random artifact from the dependency if the desired option was not found, see: - https://github.com/Kotlin/kotlinx-kover/issues/478 - https://github.com/gradle/gradle/issues/27019 - https://docs.gradle.org/current/userguide/variant_model.html#sec:variant-aware-matching - */ - dependencies.artifacts.resolvedArtifacts.get().forEach { artifact -> - - val marker = artifact.variant.attributes.getAttribute(KoverMarkerAttr.ATTRIBUTE) - if (marker == null) { - // no Kover marker - Kover plugin don't applied in the dependency - val dependencyPath = artifact.id.componentIdentifier.projectPath - val projectPath = dependencies.resolutionResult.rootComponent.get().id.projectPath - - val message = "Kover plugin is not applied in dependency '$dependencyPath' of project '$projectPath'. Apply Kover plugin in the '$dependencyPath' project." - throw KoverIllegalConfigException(message) - } - - val requestedBuildType = dependencies.attributes.getAttribute(BuildTypeAttr.ATTRIBUTE) - val givenBuildType = artifact.variant.attributes.getAttribute(BuildTypeAttr.ATTRIBUTE) - if (requestedBuildType != null && givenBuildType == null) { - // consumer expects android variant but default variant were selected - invalid selection - val dependencyPath = artifact.id.componentIdentifier.projectPath - val projectPath = dependencies.resolutionResult.rootComponent.get().id.projectPath - - val suffix = dependencies.name.substringAfter("koverExternalArtifacts") - val variantName = if (suffix.isNotEmpty()) suffix.replaceFirstChar { it.lowercase() } else "" - val message = - "Kover android variant '$variantName' was not matched with any variant from dependency '$dependencyPath' of project '$projectPath'. Check that the Kover plugin is applied in the '$dependencyPath' project and there is a variant compatible with '$variantName' in it." - throw KoverIllegalConfigException(message) - } - } - } - - private val ComponentIdentifier.projectPath: String - get() = if (this is ProjectComponentIdentifier) projectPath else displayName -} \ No newline at end of file diff --git a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/KoverGradlePlugin.kt b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/KoverGradlePlugin.kt index 037f66a1..ab7056df 100644 --- a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/KoverGradlePlugin.kt +++ b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/KoverGradlePlugin.kt @@ -32,7 +32,6 @@ class KoverGradlePlugin : Plugin { val applier = ProjectApplier(target) applier.onApply() - target.addDependencyListener() target.addDeprecations() } @@ -55,18 +54,4 @@ class KoverGradlePlugin : Plugin { this.extensions.create("kover") } } - - private fun Project.addDependencyListener() { - /* - The plugin is applied for each project, but different projects in the same build have the same `gradle` object - In order not to add the listener again, it is necessary to check whether we added it earlier. - - The most reliable way is to use the extra properties extension, - because it is always present and tied to a specific instance of the `Gradle`. - */ - if (gradle.extraProperties.properties[LISTENER_ADDED_PROPERTY_NAME] == null) { - gradle.extraProperties.properties[LISTENER_ADDED_PROPERTY_NAME] = true - gradle.addListener(DependencyCheckListener()) - } - } }