cortinico / ktfmt-gradle

A Gradle plugin to apply ktfmt to your builds, and reformat you Kotlin source code like a glimpse 🧹🐘
MIT License
149 stars 20 forks source link

Plugin not compatible with Gradle configuration cache #290

Closed omarismail94 closed 1 month ago

omarismail94 commented 3 months ago

πŸ› Describe the bug

project.extensions.getByType(KtfmtExtension::class.java).toBean() in KtfmtBaseTask invocation is unsupported with Gradle configuration caching due to call to project

⚠️ Current behavior

This gets triggered when I register a new task:

tasks.register('formatKotlin', KtfmtFormatTask) {
    source = fileTree("../buildSrc") {
        subdirs.each { include("$it/src/**/*.kt") }
    }
}

and run my gradle task with Gradle configuration caching enabled. An error gets thrown:

1 problem was found storing the configuration cache.
- Task `:buildSrc-tests:formatKotlin` of type `com.ncorti.ktfmt.gradle.tasks.KtfmtFormatTask`: invocation of 'Task.project' at execution time is unsupported.
  See https://docs.gradle.org/8.8-rc-1/userguide/configuration_cache.html#config_cache:requirements:use_project_during_execution

Full stack trace: https://ge.androidx.dev/s/nkeb2gxeap5tg/failure?expanded-stacktrace=WyIwLTEiXQ#1

If you expand the stack trace, you can see it is coming from the line I linked to above

βœ… Expected behavior

The task is cacheable. I can see a TODO over the problematic line of code that should resolve the issue.

πŸ’£ Steps to reproduce

Register a new task and run it with Gradle configuration caching enabled

πŸ“· Screenshots

πŸ“± Tech info

cortinico commented 3 months ago

The task is cacheable. I can see a TODO over the problematic line of code that should resolve the issue.

Yes that's correct. We should look into refactoring this function:

https://github.com/cortinico/ktfmt-gradle/blob/e802f87fddf6755886270a0ea4b4ad6cd1c01fa3/plugin-build/plugin/src/main/java/com/ncorti/ktfmt/gradle/tasks/KtfmtBaseTask.kt#L71-L82

To support configuration caching. I'm happy to receive a PR to fix this up πŸ‘