From a65cb947d6fad4af13ecaf62bf9907f77909b22d Mon Sep 17 00:00:00 2001 From: Nikita Bobko Date: Mon, 8 Aug 2022 12:08:59 +0200 Subject: [PATCH] Check that kotlin-reflect has a correct version during resolution Review: https://jetbrains.team/p/kt/reviews/6753 After we migrated to the binary reflect in previous commits we want to make sure that people won't accidentaly depend on wrong reflect in the future. --- build.gradle.kts | 53 ++++++++++++++++++++++++++++ compiler/ir/ir.tree/build.gradle.kts | 4 ++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 5ffc02b6442..2277f9b3973 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -390,10 +390,63 @@ val gradlePluginProjects = listOf( val ignoreTestFailures by extra(project.kotlinBuildProperties.ignoreTestFailures) +val dependencyOnSnapshotReflectWhitelist = setOf( + ":kotlin-compiler", + ":kotlin-reflect", + ":tools:binary-compatibility-validator", + ":tools:kotlin-stdlib-gen", +) + allprojects { if (!project.path.startsWith(":kotlin-ide.")) { pluginManager.apply("common-configuration") } + configurations.all { + val configuration = this + if (name != "compileClasspath") { + return@all + } + resolutionStrategy.eachDependency { + if (requested.group != "org.jetbrains.kotlin") { + return@eachDependency + } + val isReflect = requested.name == "kotlin-reflect" || requested.name == "kotlin-reflect-api" + // More strict check for "compilerModules". We can't apply this check for all modules because it would force to + // exclude kotlin-reflect from transitive dependencies of kotlin-poet, ktor, com.android.tools.build:gradle, etc + if (project.path in (rootProject.extra["compilerModules"] as Array)) { + val expectedReflectVersion = commonDependencyVersion("org.jetbrains.kotlin", "kotlin-reflect") + if (isReflect) { + check(requested.version == expectedReflectVersion) { + """ + $configuration: 'kotlin-reflect' should have '$expectedReflectVersion' version. But it was '${requested.version}' + Suggestions: + 1. Use 'commonDependency("org.jetbrains.kotlin:kotlin-reflect") { isTransitive = false }' + 2. Avoid 'kotlin-reflect' leakage from transitive dependencies with 'exclude("org.jetbrains.kotlin")' + """.trimIndent() + } + } + if (requested.name.startsWith("kotlin-stdlib")) { + check(requested.version != expectedReflectVersion) { + """ + $configuration: '${requested.name}' has a wrong version. It's not allowed to be '$expectedReflectVersion' + Suggestions: + 1. Most likely, it leaked from 'kotlin-reflect' transitive dependencies. Use 'isTransitive = false' for + 'kotlin-reflect' dependencies + 2. Avoid '${requested.name}' leakage from other transitive dependencies with 'exclude("org.jetbrains.kotlin")' + """.trimIndent() + } + } + } + if (isReflect && project.path !in dependencyOnSnapshotReflectWhitelist) { + check(requested.version != kotlinVersion) { + """ + $configuration: 'kotlin-reflect' is not allowed to have '$kotlinVersion' version. + Suggestion: Use 'commonDependency("org.jetbrains.kotlin:kotlin-reflect") { isTransitive = false }' + """.trimIndent() + } + } + } + } val mirrorRepo: String? = findProperty("maven.repository.mirror")?.toString() repositories { diff --git a/compiler/ir/ir.tree/build.gradle.kts b/compiler/ir/ir.tree/build.gradle.kts index 40ffe7f1729..dbd8857e7c5 100644 --- a/compiler/ir/ir.tree/build.gradle.kts +++ b/compiler/ir/ir.tree/build.gradle.kts @@ -12,7 +12,9 @@ dependencies { implementation(project(":compiler:util")) implementation(project(":compiler:config")) - compileOnly(project("tree-generator")) // Provided, so that IDEA can recognize references to this module in KDoc. + if (kotlinBuildProperties.isInIdeaSync) { + compileOnly(project("tree-generator")) // Provided, so that IDEA can recognize references to this module in KDoc. + } compileOnly(intellijCore()) }