diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/support/EditorConfigUtils.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/support/EditorConfigUtils.kt index 72052555..a415f1cb 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/support/EditorConfigUtils.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/support/EditorConfigUtils.kt @@ -3,9 +3,9 @@ package org.jmailen.gradle.kotlinter.support import com.pinterest.ktlint.core.KtLint import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties import com.pinterest.ktlint.core.api.EditorConfigOverride +import org.gradle.api.file.ConfigurableFileCollection import org.gradle.api.file.ProjectLayout import org.gradle.api.logging.Logger -import org.gradle.api.provider.Property import java.io.File internal fun editorConfigOverride(ktLintParams: KtLintParams): EditorConfigOverride { @@ -19,12 +19,13 @@ internal fun editorConfigOverride(ktLintParams: KtLintParams): EditorConfigOverr } internal fun resetEditorconfigCacheIfNeeded( - wasEditorConfigChanged: Property, + changedEditorconfigFiles: ConfigurableFileCollection, logger: Logger, ) { - if (wasEditorConfigChanged.get()) { + val changedFiles = changedEditorconfigFiles.files + if (changedFiles.any()) { logger.info("Editorconfig changed, resetting KtLint caches") - KtLint.trimMemory() // Calling trimMemory() will also reset internal loaded `.editorconfig` cache + changedFiles.map(File::toPath).forEach(KtLint::reloadEditorConfigFile) } } diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt index 898ac388..f8b46c35 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt @@ -13,6 +13,7 @@ import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.SourceTask import org.gradle.internal.exceptions.MultiCauseException +import org.gradle.work.FileChange import org.gradle.work.Incremental import org.gradle.work.InputChanges import org.jmailen.gradle.kotlinter.KotlinterExtension.Companion.DEFAULT_DISABLED_RULES @@ -44,8 +45,12 @@ abstract class ConfigurableKtLintTask( disabledRules = disabledRules.get(), ) - protected fun wasEditorconfigChanged(inputChanges: InputChanges) = - inputChanges.isIncremental && inputChanges.getFileChanges(editorconfigFiles).any() + protected fun getChangedEditorconfigFiles(inputChanges: InputChanges) = + if (inputChanges.isIncremental) { + inputChanges.getFileChanges(editorconfigFiles).map(FileChange::getFile) + } else { + emptyList() + } } internal inline fun ObjectFactory.property(default: T? = null): Property = diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt index d65c9375..53ba5c2d 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt @@ -40,7 +40,7 @@ open class FormatTask @Inject constructor( p.projectDirectory.set(projectLayout.projectDirectory.asFile) p.ktLintParams.set(getKtLintParams()) p.output.set(report) - p.wasEditorConfigChanged.set(wasEditorconfigChanged(inputChanges)) + p.changedEditorConfigFiles.from(getChangedEditorconfigFiles(inputChanges)) } runCatching { await() } } diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt index edd26171..ba03305b 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt @@ -52,7 +52,7 @@ open class LintTask @Inject constructor( p.projectDirectory.set(projectLayout.projectDirectory.asFile) p.reporters.putAll(reports) p.ktLintParams.set(getKtLintParams()) - p.wasEditorConfigChanged.set(wasEditorconfigChanged(inputChanges)) + p.changedEditorconfigFiles.from(getChangedEditorconfigFiles(inputChanges)) } runCatching { await() } } diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt index c5f62aee..5b411fa6 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt @@ -12,8 +12,8 @@ import org.jmailen.gradle.kotlinter.support.KotlinterError import org.jmailen.gradle.kotlinter.support.KtLintParams import org.jmailen.gradle.kotlinter.support.defaultRuleSetProviders import org.jmailen.gradle.kotlinter.support.editorConfigOverride -import org.jmailen.gradle.kotlinter.support.resolveRuleProviders import org.jmailen.gradle.kotlinter.support.resetEditorconfigCacheIfNeeded +import org.jmailen.gradle.kotlinter.support.resolveRuleProviders import org.jmailen.gradle.kotlinter.tasks.FormatTask import java.io.File @@ -27,7 +27,7 @@ abstract class FormatWorkerAction : WorkAction { override fun execute() { resetEditorconfigCacheIfNeeded( - wasEditorConfigChanged = parameters.wasEditorConfigChanged, + changedEditorconfigFiles = parameters.changedEditorConfigFiles, logger = logger, ) diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerParameters.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerParameters.kt index 3f02d1c3..d8a4b64b 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerParameters.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerParameters.kt @@ -8,7 +8,7 @@ import org.jmailen.gradle.kotlinter.support.KtLintParams interface FormatWorkerParameters : WorkParameters { val name: Property - val wasEditorConfigChanged: Property + val changedEditorConfigFiles: ConfigurableFileCollection val files: ConfigurableFileCollection val projectDirectory: RegularFileProperty val ktLintParams: Property diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerAction.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerAction.kt index b5781606..914792a0 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerAction.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerAction.kt @@ -15,8 +15,8 @@ import org.jmailen.gradle.kotlinter.support.defaultRuleSetProviders import org.jmailen.gradle.kotlinter.support.editorConfigOverride import org.jmailen.gradle.kotlinter.support.reporterFor import org.jmailen.gradle.kotlinter.support.reporterPathFor -import org.jmailen.gradle.kotlinter.support.resolveRuleProviders import org.jmailen.gradle.kotlinter.support.resetEditorconfigCacheIfNeeded +import org.jmailen.gradle.kotlinter.support.resolveRuleProviders import org.jmailen.gradle.kotlinter.tasks.LintTask import java.io.File @@ -32,7 +32,7 @@ abstract class LintWorkerAction : WorkAction { override fun execute() { resetEditorconfigCacheIfNeeded( - wasEditorConfigChanged = parameters.wasEditorConfigChanged, + changedEditorconfigFiles = parameters.changedEditorconfigFiles, logger = logger, ) var hasError = false diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerParameters.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerParameters.kt index 2fd74e94..82b375b8 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerParameters.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerParameters.kt @@ -10,7 +10,7 @@ import java.io.File interface LintWorkerParameters : WorkParameters { val name: Property - val wasEditorConfigChanged: Property + val changedEditorconfigFiles: ConfigurableFileCollection val files: ConfigurableFileCollection val projectDirectory: RegularFileProperty val reporters: MapProperty diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt index d1fb082e..0bac4dcf 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt @@ -57,9 +57,10 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { fun `plugin respects disabled_rules set in editorconfig`() { projectRoot.resolve(".editorconfig") { appendText( + // language=editorconfig """ [*.{kt,kts}] - disabled_rules=filename + ktlint_disabled_rules = filename """.trimIndent(), ) } @@ -76,6 +77,7 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { fun `plugin respects 'indent_size' set in editorconfig`() { projectRoot.resolve(".editorconfig") { appendText( + // language=editorconfig """ [*.{kt,kts}] indent_size = 6 @@ -106,9 +108,10 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { fun `editorconfig changes are taken into account on lint task re-runs`() { projectRoot.resolve(".editorconfig") { writeText( + // language=editorconfig """ [*.{kt,kts}] - disabled_rules=filename + ktlint_disabled_rules = filename """.trimIndent(), ) } @@ -158,9 +161,10 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { projectRoot.resolve(".editorconfig") { writeText( + // language=editorconfig """ [*.{kt,kts}] - disabled_rules=filename + ktlint_disabled_rules = filename """.trimIndent(), ) }