Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to return supported Kotlin version #6472

Merged
merged 17 commits into from
Oct 4, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import io.gitlab.arturbosch.detekt.internal.DetektAndroid
import io.gitlab.arturbosch.detekt.internal.DetektJvm
import io.gitlab.arturbosch.detekt.internal.DetektMultiplatform
import io.gitlab.arturbosch.detekt.internal.DetektPlain
import org.gradle.api.Incubating
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.ReportingBasePlugin
import org.gradle.api.reporting.ReportingExtension
import java.net.URL
import java.util.jar.Manifest

class DetektPlugin : Plugin<Project> {

Expand Down Expand Up @@ -176,3 +179,16 @@ class DetektPlugin : Plugin<Project> {
const val CONFIGURATION_DETEKT = "detekt"
const val CONFIGURATION_DETEKT_PLUGINS = "detektPlugins"
const val USE_WORKER_API = "detekt.use.worker.api"

@Incubating
fun getSupportedKotlinVersion(): String {
kkocel marked this conversation as resolved.
Show resolved Hide resolved
return DetektPlugin::class.java.classLoader.getResources("META-INF/MANIFEST.MF")
.asSequence()
.mapNotNull { runCatching { readVersion(it) }.getOrNull() }
.first()
}

private fun readVersion(resource: URL): String? = resource.openConnection()
.apply { useCaches = false }
.getInputStream()
.use { Manifest(it).mainAttributes.getValue("KotlinImplementationVersion") }
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,11 @@ class DetektPlainSpec {
assertThat(argumentString).doesNotContain("--classpath")
}
}

@Test
fun `resolves kotlin version from manifest`() {
val version = getSupportedKotlinVersion()

assertThat(version).isNotBlank()
}
}
4 changes: 2 additions & 2 deletions website/docs/gettingstarted/gradle.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -513,13 +513,13 @@ detekt was compiled with Kotlin 1.8.0 but is currently running with 1.7.0

This happens when the version of `kotlin-compiler-embeddable` is overridden on detekt's classpath (in the `detekt`
dependency configuration). This can happen when build scripts use things like this which override all dependency
configurations:
configurations (e.g. by [Spring Dependency Management Plugin](https://docs.spring.io/dependency-management-plugin/docs/current/reference/html/):

```kotlin
configurations.all {
resolutionStrategy.eachDependency {
if (requested.group == "org.jetbrains.kotlin") {
useVersion("1.7.0")
useVersion(io.gitlab.arturbosch.detekt.getSupportedKotlinVersion())
}
}
}
Expand Down