ktlint-gradle icon indicating copy to clipboard operation
ktlint-gradle copied to clipboard

Setting filter patterns on KtlintExtension breaks configuration cache

Open tadfisher opened this issue 2 years ago • 5 comments

Passing PatternFilterable from KtlintExtension into BaseKtLintCheckTask breaks configuration caching with the following:

️cannot serialize Gradle script object references as these are not supported with the configuration cache.

The code that breaks is as follows:

ktlint {
    filter {
        exclude("src/commonMain/kotlin/Models.kt")
    }
}

Adding the filters directly to the tasks makes it work:

tasks {
    named<BaseKtLintCheckTask>("runKtlintCheckOverCommonMainSourceSet") {
        exclude("src/commonMain/kotlin/Models.kt")
    }

    named<BaseKtLintCheckTask>("runKtlintFormatOverCommonMainSourceSet") {
        exclude("src/commonMain/kotlin/Models.kt")
    }
}

I think all that needs to be done is to wrap the PatternFilterable object in a Provider.

tadfisher avatar Mar 28 '23 02:03 tadfisher

😩 yea, this makes sense. Thank you for the report.

I don't know if putting the pattern filterable into a provider will solve the problem though.

JLLeitschuh avatar Mar 28 '23 11:03 JLLeitschuh

I am having trouble reproducing this message. is it a warning? does it occur on first build or when the configuration cache is attempted to be used?

wakingrufus avatar Mar 28 '23 13:03 wakingrufus

The configuration cache has to be enabled, with --configuration-cache or org.gradle.unsafe.configuration-cache=true.

I also noticed we have enableFeaturePreview("STABLE_CONFIGURATION_CACHE") in our settings script, which might make the cache more strict in the types it will accept.

tadfisher avatar Mar 28 '23 17:03 tadfisher

I encountered the same error while filtering using Spec<FileTreeElement> like:

 ktlint {
    filter {
      exclude { it.file.toString().contains("$buildDir/bob/") }     } // Gradle configuration cache - error cannot serialize Gradle script
}

It does indeed work while using a regular expression like:

ktlint {
    filter {
        exclude("**/style-violations.kt")
    }
}

Running Gradle with configuration cache won't work when using filter excludes with Spec and project variables. As Gradle resolves project variables (like $buildDir) during running task the error message suggests that the issue is in the plugin task method body.

A workaround for this is to force the variable resolution during the configuration phase:

ktlint {
   val buildDirCopy: File = buildDir
    filter {
        exclude { it.file.path.contains("$buildDirCopy/generated/") }
    }
}

It's not perfect, but other solutions would have to include some more complex support for early variable resolutions.

IgorDomagala avatar Aug 22 '23 13:08 IgorDomagala

I was looking for a solution for exclude auto generated files from GraphQL in Kotlin Multi Platform project and finally resolved this problem thanks your response :)

awochniak avatar Dec 21 '23 11:12 awochniak