From 212603e668d2644b070a0d608eddc2accd1f49af Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 22 Nov 2026 13:35:19 +0000 Subject: [PATCH] Fix taskService type clash when Spotless is applied to sibling projects Since 8.3.0 (#2854) every copy of the plugin registered its build service under the same name, "SpotlessTaskService". Gradle loads a separate copy of the plugin per buildscript classloader, e.g. when sibling projects apply Spotless without a common parent. The second copy then got the first copy's service and failed with "Cannot set the value of task ... property 'taskService'". Each copy of the plugin now uses the first service name that is not taken by another copy. RegisterDependenciesTask uses the same lookup, so predeclared dependencies stay on the same service as the format tasks. Fixes #2877 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MTr23wtv6sQmXLnujeLnnP --- plugin-gradle/CHANGES.md | 1 + .../spotless/RegisterDependenciesTask.java | 4 +- .../gradle/spotless/SpotlessExtension.java | 2 +- .../gradle/spotless/SpotlessTaskService.java | 22 +++- .../spotless/SiblingClassloaderTest.java | 119 ++++++++++++++++++ 5 files changed, 142 insertions(+), 6 deletions(-) create mode 100644 plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SiblingClassloaderTest.java diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 1151abc9ae..03164b7e52 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Fixed - Fix npm/node executable discovery treating a directory (e.g. an `npm` folder inside `NODE_PATH`) as an executable. ([#3190](https://github.com/diffplug/spotless/issues/3190)) +- Fix `Cannot set the value of task ... property 'taskService'` when Spotless is applied to sibling projects but not to their parent, a regression since `8.3.0`. Applying it to the root project with `apply false` is no longer necessary. ([#2877](https://github.com/diffplug/spotless/issues/2877)) ## [8.12.0] - 2026-11-21 ### Added diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java index 96dc160bbe..e9ab1dc244 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java @@ -63,8 +63,8 @@ void hookSubprojectTask(SpotlessTask task) { void setup() { Preconditions.checkArgument(getProject().getRootProject() == getProject(), "Can only be used on the root project"); - String compositeBuildSuffix = getName().substring(TASK_NAME.length()); // see https://github.com/diffplug/spotless/pull/1001 - taskService = SpotlessTaskService.registerIfAbsent(getProject(), compositeBuildSuffix); + // the same service as the SpotlessExtension of this copy of the plugin, see https://github.com/diffplug/spotless/pull/1001 + taskService = SpotlessTaskService.registerIfAbsent(getProject()); usesService(taskService); getBuildEventsListenerRegistry().onTaskCompletion(taskService); // lazy, so a build directory set later in the buildscript still counts diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java index 83c317f7e7..93a57eba74 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java @@ -51,7 +51,7 @@ public abstract class SpotlessExtension { protected SpotlessExtension(Project project) { this.project = requireNonNull(project); - this.spotlessTaskService = SpotlessTaskService.registerIfAbsent(project, ""); + this.spotlessTaskService = SpotlessTaskService.registerIfAbsent(project); } Provider getSpotlessTaskService() { diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskService.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskService.java index c6fd993d20..e95994a776 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskService.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskService.java @@ -38,6 +38,7 @@ import org.gradle.api.provider.Provider; import org.gradle.api.services.BuildService; import org.gradle.api.services.BuildServiceParameters; +import org.gradle.api.services.BuildServiceRegistry; import org.gradle.api.tasks.Internal; import org.gradle.api.tasks.TaskProvider; import org.gradle.tooling.events.FinishEvent; @@ -154,9 +155,24 @@ public void hookSubprojectTask(Project project, SpotlessTask task) { } } - public static Provider registerIfAbsent(Project project, String suffix) { - return project.getGradle().getSharedServices() - .registerIfAbsent("SpotlessTaskService" + suffix, SpotlessTaskService.class, spec -> {}); + /** + * Returns the service of this copy of the plugin, registering it if needed. + *

+ * Gradle loads a separate copy of the plugin for each buildscript classloader, e.g. for sibling + * projects which apply Spotless without a common parent (#2877), or for a composite build (#1001). + * A service instance from another copy can't be assigned to our {@code Property}, + * so each copy uses the first service name which isn't taken by another copy. + */ + public static Provider registerIfAbsent(Project project) { + BuildServiceRegistry services = project.getGradle().getSharedServices(); + for (int copy = 0;; ++copy) { + String name = copy == 0 ? "SpotlessTaskService" : "SpotlessTaskService" + copy; + Provider service = services.registerIfAbsent(name, SpotlessTaskService.class, spec -> {}); + // raw access, to avoid a ClassCastException if the service is from another copy + if (((Provider) service).get() instanceof SpotlessTaskService) { + return service; + } + } } abstract static class ClientTask extends DefaultTask { diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SiblingClassloaderTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SiblingClassloaderTest.java new file mode 100644 index 0000000000..c541296491 --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SiblingClassloaderTest.java @@ -0,0 +1,119 @@ +/* + * Copyright 2026 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.gradle.spotless; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.stream.Collectors; + +import org.assertj.core.api.Assertions; +import org.gradle.testkit.runner.BuildResult; +import org.gradle.testkit.runner.GradleRunner; +import org.junit.jupiter.api.Test; + +/** + * When the plugin is applied to sibling projects but not to a common parent, then + * Gradle loads it in a separate buildscript classloader per project. + * #2877 + */ +class SiblingClassloaderTest extends GradleIntegrationHarness { + private static final List SUBPROJECTS = List.of("a", "b"); + + /** + * TestKit's injected plugin classpath is shared by every project of the build, + * so we put the plugin on each subproject's own buildscript classpath instead. + */ + @Override + public GradleRunner gradleRunner() throws IOException { + return super.gradleRunner().withPluginClasspath(List.of()); + } + + private void createSiblingsWithOwnClassloader() throws IOException { + String classpath = GradleRunner.create().withPluginClasspath().getPluginClasspath().stream() + .map(File::getAbsolutePath) + .map(path -> "'" + path.replace('\\', '/') + "'") + .collect(Collectors.joining(", ")); + setFile("settings.gradle").toLines(SUBPROJECTS.stream().map(name -> "include '" + name + "'").toArray(String[]::new)); + setFile("build.gradle").toLines(); + for (String name : SUBPROJECTS) { + // Gradle shares one classloader between identical buildscript classpaths, so give each + // project an extra entry, as happens when projects apply different sets of plugins + setFile(name + "/classpath-" + name + "/marker").toContent(name); + setFile(name + "/build.gradle").toLines( + "buildscript {", + " dependencies {", + " classpath files(" + classpath + ", 'classpath-" + name + "')", + " }", + "}", + "apply plugin: 'com.diffplug.spotless'", + "spotless {", + " format 'misc', {", + " target file('test.txt')", + " trimTrailingWhitespace()", + " }", + "}"); + setFile(name + "/test.txt").toContent("dirty \n"); + } + } + + @Test + void givenSiblingClassloaders_whenApplyThenCheck_thenBothProjectsAreFormatted() throws IOException { + // given + createSiblingsWithOwnClassloader(); + + // when + gradleRunner().withArguments("spotlessApply").build(); + gradleRunner().withArguments("spotlessCheck").build(); + + // then + for (String name : SUBPROJECTS) { + assertFile(name + "/test.txt").hasContent("dirty\n"); + } + } + + @Test + void givenSiblingClassloaders_whenCheckOnDirtyFiles_thenBothProjectsFail() throws IOException { + // given + createSiblingsWithOwnClassloader(); + + // when + BuildResult result = gradleRunner().withArguments("spotlessCheck", "--continue").buildAndFail(); + + // then + for (String name : SUBPROJECTS) { + Assertions.assertThat(result.getOutput()).contains("Execution failed for task ':" + name + ":spotlessMiscCheck'"); + } + } + + @Test + void givenSiblingClassloadersAndConfigurationCache_whenRunTwice_thenCacheIsStoredAndReused() throws IOException { + // given + createSiblingsWithOwnClassloader(); + setFile("gradle.properties").toContent("org.gradle.configuration-cache=true"); + + // when + BuildResult store = gradleRunner().withArguments("spotlessApply").build(); + setFile("a/test.txt").toContent("dirty again \n"); + BuildResult reuse = gradleRunner().withArguments("spotlessApply").build(); + + // then + Assertions.assertThat(store.getOutput()).contains("Configuration cache entry stored"); + Assertions.assertThat(reuse.getOutput()).contains("Configuration cache entry reused"); + assertFile("a/test.txt").hasContent("dirty again\n"); + assertFile("b/test.txt").hasContent("dirty\n"); + } +}