diff --git a/README.md b/README.md index 67ae536..9f4b2f3 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ ktlint { ktlintVersion = "1.0.0-SNAPSHOT" chunkSize = 50 baselineFile.set(file("config/ktlint_baseline.xml")) - ignoreKspGeneratedSources = true + ignoreFilesUnderBuildDir = true } ``` @@ -84,7 +84,7 @@ ktlint { - `chunkSize` - defines how many files will be processed by a single gradle worker in parallel - `baselineFile` - points at location of baseline file containing _known_ offenses that will be ignored during `lintKotlin` task execution -- `ignoreKspGeneratedSources` - indicates if the plugin should automatically register tasks for KSP generated sources (hence triggering compilation tasks). This is an incubating, rather primitive workaround for the issue reported: https://github.com/google/ksp/issues/1261. +- `ignoreFilesUnderBuildDir` - This allows to ignore generated sources. This is a workaround for https://youtrack.jetbrains.com/issue/KT-45161. Setting the value to `false` restores default behavior and will run ktlint against all sources returned by KGP ### Customizing Tasks diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 2e1a829..db8f8e7 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -11,7 +11,8 @@ maven-commons = "2.15.0" [libraries] agp-gradle = { module = "com.android.tools.build:gradle", version.ref = "google-agp" } -kotlin-gradle = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin" } +kotlin-gradle-api = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin-api" } +kotlin-gradle-impl = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin" } ktlint-rule-engine = { module = "com.pinterest.ktlint:ktlint-rule-engine", version.ref = "maven-ktlint" } ktlint-cli-ruleset-core = { module = "com.pinterest.ktlint:ktlint-cli-ruleset-core", version.ref = "maven-ktlint" } ktlint-cli-reporter-core = { module = "com.pinterest.ktlint:ktlint-cli-reporter-core", version.ref = "maven-ktlint" } diff --git a/ktlint-gradle-plugin/build.gradle b/ktlint-gradle-plugin/build.gradle index fe21c6b..76229e7 100644 --- a/ktlint-gradle-plugin/build.gradle +++ b/ktlint-gradle-plugin/build.gradle @@ -26,7 +26,7 @@ jacoco { } dependencies { - compileOnly(libs.kotlin.gradle) + compileOnly(libs.kotlin.gradle.api) compileOnly(libs.agp.gradle) compileOnly(libs.ktlint.rule.engine) compileOnly(libs.ktlint.cli.ruleset.core) @@ -39,7 +39,7 @@ dependencies { testImplementation(libs.commons.io) testImplementation(libs.assertj.core) - testRuntimeDependencies(libs.kotlin.gradle) + testRuntimeDependencies(libs.kotlin.gradle.impl) testRuntimeDependencies(libs.agp.gradle) testRuntimeDependencies(libs.google.ksp.gradle) } @@ -66,8 +66,8 @@ tasks.named("processResources") { tasks.withType(KotlinCompile).configureEach { kotlinOptions { - apiVersion = "1.4" - languageVersion = "1.4" + apiVersion = "1.8" + languageVersion = "1.8" } } tasks.withType(Test).configureEach { diff --git a/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/KtlintGradleExtension.kt b/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/KtlintGradleExtension.kt index 57bd26f..0ba148d 100644 --- a/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/KtlintGradleExtension.kt +++ b/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/KtlintGradleExtension.kt @@ -37,5 +37,9 @@ public open class KtlintGradleExtension internal constructor( public val baselineFile: RegularFileProperty = objectFactory.fileProperty() @Incubating - public val ignoreKspGeneratedSources: Property = objectFactory.property(default = true) + public val ignoreFilesUnderBuildDir: Property = objectFactory.property(default = true) + + @Deprecated(message = "Will be removed in the next version", replaceWith = ReplaceWith(expression = "ignoreFilesUnderBuildDir")) + @Incubating + public val ignoreKspGeneratedSources: Property = ignoreFilesUnderBuildDir } diff --git a/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/KtlintGradlePlugin.kt b/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/KtlintGradlePlugin.kt index 9b44189..8e053fb 100644 --- a/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/KtlintGradlePlugin.kt +++ b/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/KtlintGradlePlugin.kt @@ -47,7 +47,7 @@ public class KtlintGradlePlugin : Plugin { sourceResolver.applyToAll(this, pluginExtension) { id, resolvedSources -> val checkWorker = tasks.register( - "lintKotlin${id.capitalize()}", + "lintKotlin${id.replaceFirstChar(Char::titlecase)}", LintTask::class.java, ) { task -> task.source(resolvedSources) @@ -67,7 +67,7 @@ public class KtlintGradlePlugin : Plugin { lintKotlin.configure { it.dependsOn(checkWorker) } val formatWorker = tasks.register( - "formatKotlin${id.capitalize()}", + "formatKotlin${id.replaceFirstChar(Char::titlecase)}", FormatTask::class.java, ) { task -> task.source(resolvedSources) diff --git a/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/SourceSetApplier.kt b/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/SourceSetApplier.kt index 3f3d799..f55b961 100644 --- a/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/SourceSetApplier.kt +++ b/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/SourceSetApplier.kt @@ -1,10 +1,8 @@ package io.github.usefulness import org.gradle.api.Project -import org.gradle.api.file.FileTree -import org.gradle.api.provider.Provider -internal typealias SourceSetAction = (String, Provider) -> Unit +internal typealias SourceSetAction = (String, Any) -> Unit internal interface SourceSetApplier { fun applyToAll(project: Project, extension: KtlintGradleExtension, action: SourceSetAction) diff --git a/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/pluginapplier/AndroidSourceSetApplier.kt b/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/pluginapplier/AndroidSourceSetApplier.kt index 5967b8c..67530ca 100644 --- a/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/pluginapplier/AndroidSourceSetApplier.kt +++ b/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/pluginapplier/AndroidSourceSetApplier.kt @@ -17,12 +17,12 @@ internal object AndroidSourceSetApplier : SourceSetApplier { android.finalizeDsl { commonExtension -> commonExtension.sourceSets.configureEach { sourceSet -> val id = sourceSet.name.id - action(id, project.provider { getKotlinFiles(project, sourceSet) }) + action(id, getKotlinFiles(project, sourceSet)) } } } - private fun getKotlinFiles(project: Project, sourceSet: AndroidSourceSet): FileTree? { + private fun getKotlinFiles(project: Project, sourceSet: AndroidSourceSet): FileTree { val javaSources = sourceSet.java.srcDirs val kotlinSources = sourceSet.kotlin.srcDirs diff --git a/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/pluginapplier/KotlinSourceSetApplier.kt b/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/pluginapplier/KotlinSourceSetApplier.kt index fefac95..a86fbec 100644 --- a/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/pluginapplier/KotlinSourceSetApplier.kt +++ b/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/pluginapplier/KotlinSourceSetApplier.kt @@ -5,15 +5,26 @@ import io.github.usefulness.SourceSetAction import io.github.usefulness.SourceSetApplier import io.github.usefulness.id import org.gradle.api.Project -import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension +import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetContainer +import java.io.File internal object KotlinSourceSetApplier : SourceSetApplier { override fun applyToAll(project: Project, extension: KtlintGradleExtension, action: SourceSetAction) { - project.extensions.getByType(KotlinProjectExtension::class.java).sourceSets.configureEach { sourceSet -> - if (!sourceSet.name.startsWith("generatedByKsp") || !extension.ignoreKspGeneratedSources.get()) { - action(sourceSet.kotlin.name.id, project.provider { sourceSet.kotlin }) + project.extensions.getByType(KotlinSourceSetContainer::class.java).sourceSets.configureEach { sourceSet -> + val directories = project.provider { + if (extension.ignoreFilesUnderBuildDir.get()) { + sourceSet.kotlin.srcDirTrees.mapNotNull { dirTree -> + dirTree.dir.takeUnless { it.isUnder(project.layout.buildDirectory.asFile.get()) } + } + } else { + sourceSet.kotlin.sourceDirectories + } } + + action(sourceSet.kotlin.name.id, directories) } } + + private fun File.isUnder(rootDirectory: File) = canonicalPath.startsWith(rootDirectory.canonicalPath) } diff --git a/ktlint-gradle-plugin/src/test/kotlin/io/github/usefulness/functional/ThirdPartyPlugins.kt b/ktlint-gradle-plugin/src/test/kotlin/io/github/usefulness/functional/ThirdPartyPlugins.kt index 915f453..46203d3 100644 --- a/ktlint-gradle-plugin/src/test/kotlin/io/github/usefulness/functional/ThirdPartyPlugins.kt +++ b/ktlint-gradle-plugin/src/test/kotlin/io/github/usefulness/functional/ThirdPartyPlugins.kt @@ -5,12 +5,10 @@ import io.github.usefulness.functional.utils.kotlinClass import io.github.usefulness.functional.utils.resolve import io.github.usefulness.functional.utils.settingsFile import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test class ThirdPartyPlugins : WithGradleTest.Android() { - @Disabled("This doesn't work with Kotlin 1.9.20 :/ https://github.com/google/ksp/pull/1558") @Test fun kspKotlin() { testProjectDir.apply { @@ -32,7 +30,9 @@ class ThirdPartyPlugins : WithGradleTest.Android() { repositories.mavenCentral() dependencies { + implementation "com.google.dagger:dagger:2.48.1" ksp "com.google.dagger:dagger-compiler:2.48.1" + } kotlin { @@ -53,7 +53,24 @@ class ThirdPartyPlugins : WithGradleTest.Android() { resolve("src/main/kotlin/KotlinClass.kt") { writeText(kotlinClass("KotlinClass")) } + resolve("src/test/kotlin/KotlinTestClass.kt") { + writeText(kotlinClass("KotlinTestClass")) + } + resolve("src/main/kotlin/FooModule.kt") { + writeText( + // language=kotlin + """ + @dagger.Module + object FooModule { + @dagger.Provides + fun foo(): String = "" + } + + """.trimIndent(), + ) + } } + build("assemble") // generate files under `build` directory val result = build("lintKotlin") @@ -79,9 +96,9 @@ class ThirdPartyPlugins : WithGradleTest.Android() { assertThat(onlyMain.tasks.map { it.path }).containsAll( listOf( + ":kspKotlin", ":compileKotlin", ":compileJava", - ":lintKotlinGeneratedByKspTestKotlin", ":lintKotlinTest", ":lintKotlinMain", ":lintKotlin", @@ -116,7 +133,7 @@ class ThirdPartyPlugins : WithGradleTest.Android() { repositories.mavenCentral() dependencies { - ksp "com.google.dagger:dagger-compiler:2.48" + ksp "com.google.dagger:dagger-compiler:2.48.1" } """.trimIndent(),