[Gradle MPP] Added duplicate source set name diagnostic

#KT-62601 Fixed
This commit is contained in:
Dmitrii Krasnov
2023-10-25 15:08:53 +02:00
committed by Space Team
parent 290adda8fc
commit 0c46cfd761
5 changed files with 96 additions and 0 deletions
@@ -665,6 +665,19 @@ object KotlinToolingDiagnostics {
""".trimIndent()
)
}
object DuplicateSourceSetsError : ToolingDiagnosticFactory(FATAL) {
operator fun invoke(duplicatedSourceSets: Map<String, List<String>>): ToolingDiagnostic {
val duplicatesGroupsString = duplicatedSourceSets
.map { entry -> entry.value.joinToString(", ") }
.joinToString("], [", "[", "]")
return build(
"Duplicate Kotlin source sets have been detected: $duplicatesGroupsString." +
" Keep in mind that source set names are case-insensitive," +
" which means that `srcMain` and `sRcMain` are considered the same source set."
)
}
}
}
private fun String.indentLines(nSpaces: Int = 4, skipFirstLine: Boolean = true): String {
@@ -0,0 +1,33 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.plugin.diagnostics.checkers
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtensionOrNull
import org.jetbrains.kotlin.gradle.plugin.diagnostics.*
import org.jetbrains.kotlin.gradle.plugin.diagnostics.KotlinGradleProjectChecker
import org.jetbrains.kotlin.gradle.plugin.diagnostics.KotlinGradleProjectCheckerContext
import org.jetbrains.kotlin.gradle.plugin.diagnostics.KotlinToolingDiagnosticsCollector
/**
* This checker is used to ensure that there are no duplicate source sets in MPP project.
*
* Examples of duplicated sourceSets: [jvmMain and jVmMain] or [macosMain and macOSMain]
*
*/
// TODO(Dmitrii Krasnov): Remove this checker once IDEA-317606 is resolved
internal object DuplicateSourceSetChecker : KotlinGradleProjectChecker {
override suspend fun KotlinGradleProjectCheckerContext.runChecks(collector: KotlinToolingDiagnosticsCollector) {
val kotlin = project.multiplatformExtensionOrNull ?: return
val sourceSetNames = kotlin.awaitSourceSets().map { it.name }
val duplicateSourceSets = sourceSetNames
.groupBy { it.toLowerCase() }
.filter { (_, values) -> values.size > 1 }
if (duplicateSourceSets.isNotEmpty()) {
project.reportDiagnostic(KotlinToolingDiagnostics.DuplicateSourceSetsError(duplicateSourceSets))
}
}
}
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.gradle.plugin.diagnostics.checkers.CommonMainOrTestW
import org.jetbrains.kotlin.gradle.plugin.diagnostics.checkers.DeprecatedKotlinNativeTargetsChecker
import org.jetbrains.kotlin.gradle.plugin.diagnostics.checkers.DisabledCinteropCommonizationInHmppProjectChecker
import org.jetbrains.kotlin.gradle.plugin.diagnostics.checkers.DisabledNativeTargetsChecker
import org.jetbrains.kotlin.gradle.plugin.diagnostics.checkers.DuplicateSourceSetChecker
import org.jetbrains.kotlin.gradle.plugin.diagnostics.checkers.ExperimentalK2UsageChecker
import org.jetbrains.kotlin.gradle.plugin.diagnostics.checkers.InternalGradlePropertiesUsageChecker
import org.jetbrains.kotlin.gradle.plugin.diagnostics.checkers.JsEnvironmentChecker
@@ -148,6 +149,7 @@ internal fun Project.registerKotlinPluginExtensions() {
register(project, KotlinTargetAlreadyDeclaredChecker)
register(project, InternalGradlePropertiesUsageChecker)
register(project, WasmSourceSetsNotFoundChecker)
register(project, DuplicateSourceSetChecker)
if (isMultiplatform) {
register(project, KotlinMultiplatformAndroidGradlePluginCompatibilityChecker)
@@ -0,0 +1,47 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.unitTests.diagnosticsTests
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtension
import org.jetbrains.kotlin.gradle.util.assertNoDiagnostics
import org.jetbrains.kotlin.gradle.util.buildProjectWithMPP
import org.jetbrains.kotlin.gradle.util.checkDiagnostics
import kotlin.test.Test
import kotlin.test.assertFails
class DuplicateSourceSetCheckerTest {
@Test
fun `target with custom name duplicates defualt name failes build`() {
val project = buildProjectWithMPP {
project.multiplatformExtension.applyDefaultHierarchyTemplate()
project.multiplatformExtension.linuxX64("linUX")
}
assertFails { project.evaluate() }
project.checkDiagnostics("DuplicateSourceSetChecker")
}
@Test
fun `target with custom name does not produce any diagnostics`() {
val project = buildProjectWithMPP {
project.multiplatformExtension.applyDefaultHierarchyTemplate()
project.multiplatformExtension.linuxX64("custom")
}
project.evaluate()
project.assertNoDiagnostics()
}
@Test
fun `several targets without custom name don't produce any diagnostics`() {
val project = buildProjectWithMPP {
project.multiplatformExtension.applyDefaultHierarchyTemplate()
project.multiplatformExtension.linuxX64()
project.multiplatformExtension.mingwX64()
}
project.evaluate()
project.assertNoDiagnostics()
}
}
@@ -0,0 +1 @@
[DuplicateSourceSetsError | FATAL] Duplicate Kotlin source sets have been detected: [linUXMain, linuxMain], [linUXTest, linuxTest]. Keep in mind that source set names are case-insensitive, which means that `srcMain` and `sRcMain` are considered the same source set.