Skip to content

Commit

Permalink
Let spotless plugin not break clean task with the configuration cache
Browse files Browse the repository at this point in the history
Using the project at execution time is not allowed when Gradle's
configuration cache is enabled.

Before this commit, the spotless plugin registered a doLast task action
on the clean task that accessed the projet at execution time. The effect
of this is to break using the clean task on all builds that have the
spotless plugin applied when the configuration cache is enabled.

This commits change the way the spotless plugin configures the clean
task to not access a project in the added doLast task action by
computing the spotless cache key upfront at configuration time. The
effect is that builds with the spotless plugin applied can now run the
clean task with the configuration cache enabled.
  • Loading branch information
eskatos committed Feb 11, 2021
1 parent 95b7c5c commit 60e424b
Showing 1 changed file with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ public void apply(Project project) {
project.getExtensions().create(SpotlessExtension.class, SpotlessExtension.EXTENSION, SpotlessExtensionImpl.class, project);

// clear spotless' cache when the user does a clean
project.getTasks().named(BasePlugin.CLEAN_TASK_NAME).configure(clean -> {
clean.doLast(unused -> {
// resolution for: https://github.com/diffplug/spotless/issues/243#issuecomment-564323856
// project.getRootProject() is consistent across every project, so only of one the clears will
// actually happen (as desired)
//
// we use System.identityHashCode() to avoid a memory leak by hanging on to the reference directly
SpotlessCache.clearOnce(System.identityHashCode(project.getRootProject()));
});
});
// resolution for: https://github.com/diffplug/spotless/issues/243#issuecomment-564323856
// project.getRootProject() is consistent across every project, so only of one the clears will
// actually happen (as desired)
//
// we use System.identityHashCode() to avoid a memory leak by hanging on to the reference directly
int cacheKey = System.identityHashCode(project.getRootProject());
project.getTasks().named(BasePlugin.CLEAN_TASK_NAME).configure(clean ->
clean.doLast(unused -> SpotlessCache.clearOnce(cacheKey))
);
}

static String capitalize(String input) {
Expand Down

0 comments on commit 60e424b

Please sign in to comment.