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

Setting filter patterns on KtlintExtension breaks configuration cache #657

Open
tadfisher opened this issue Mar 28, 2023 · 5 comments
Open
Assignees

Comments

@tadfisher
Copy link
Contributor

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.

@JLLeitschuh
Copy link
Owner

😩 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.

@wakingrufus
Copy link
Collaborator

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 added a commit that referenced this issue Mar 28, 2023
@tadfisher
Copy link
Contributor Author

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.

@wakingrufus wakingrufus self-assigned this Apr 11, 2023
@IgorDomagala
Copy link

I encountered the same error while filtering using Spec 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.

@awochniak
Copy link

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants