cortinico / ktfmt-gradle

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

Task caching not working properly for multiple sourceSets #288

Closed simonhauck closed 5 months ago

simonhauck commented 5 months ago

πŸ› Describe the bug

When a project has multiple sourceSets (e.g. main, test) the task caching is not working properly because the task output file changes.

Running multiple format checks together with the --info flag shows this

Caching disabled for task ':plugin:git-plugin:ktfmtCheckTest' because:
  Gradle does not know how file 'build/ktfmt/output.txt' was created (output property 'output'). Task output caching requires exclusive access to output paths to guarantee correctness (i.e. multiple tasks are not allowed to produce output in the same location).

Task ':plugin:git-plugin:ktfmtCheckTest' is not up-to-date because:
  Output property 'output' file /<project-dir>/plugin/git-plugin/build/ktfmt/output.txt has changed.

From a quick look at the code, the issue seems to orginate from the KtfmtCheckTask class. The output value is set to layout.buildDirectory.file("ktfmt/output.txt"). If multiple sourcesets exist, they share the output file. Therefore the file changes between tasks and the task is marked as Not-Up-To-Date.

⚠️ Current behavior

The checkFormat task is executed every time, even though none of the inputs have changed.

βœ… Expected behavior

Ideally the task would be marked as Up-to-date.

πŸ’£ Steps to reproduce

You simply need a repository with two or more sourceSets and execute the ktfmtCheck task twice. If required, I can provide an example repository

Proposed solution

The output file could have a suffix/prefix of the current sourceSet. This can either be provided as additional input or you could specify the output file directly in the KtfmtPluginUtils class.

I think this could already be enough.

πŸ“· Screenshots

πŸ“± Tech info

If you are fine with this plan, I could draft a PR.

cortinico commented 5 months ago

Gradle does not know how file 'build/ktfmt/output.txt' was created (output property 'output'). Task output caching requires exclusive access to output paths to guarantee correctness (i.e. multiple tasks are not allowed to produce output in the same location).

Yup I believe you're right. I think the problem is that we should be using paths that are per-sourceSet (like build/ktfmt/check/output.txt)