From 60e424b73ad000890a9db05c1f427e7200558c52 Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 11 Feb 2021 12:48:51 +0100 Subject: [PATCH 1/3] Let spotless plugin not break clean task with the configuration cache 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. --- .../gradle/spotless/SpotlessPlugin.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java index c62dd03418..b15aea5d4f 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java @@ -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) { From 52766178cccc6edba0024ac0e50b1071f91caaf7 Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 11 Feb 2021 12:58:26 +0100 Subject: [PATCH 2/3] Add fix to Gradle plugin changelog --- plugin-gradle/CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index e8a1a800d2..c02773cc52 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Fixed +* Fixed the `clean` task when Gradle's configuration cache is enabled ([#796](https://github.com/diffplug/spotless/issues/796)) ## [5.10.0] - 2021-02-09 ### Added From f92fbb8f73219c0069aa2feeeff89815729fda01 Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 11 Feb 2021 13:05:08 +0100 Subject: [PATCH 3/3] Make spotless happy :) --- .../java/com/diffplug/gradle/spotless/SpotlessPlugin.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java index b15aea5d4f..80025f3164 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 DiffPlug + * Copyright 2016-2021 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,9 +48,7 @@ public void apply(Project project) { // // 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)) - ); + project.getTasks().named(BasePlugin.CLEAN_TASK_NAME).configure(clean -> clean.doLast(unused -> SpotlessCache.clearOnce(cacheKey))); } static String capitalize(String input) {