Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SpotlessTaskService> getSpotlessTaskService() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -154,9 +155,24 @@ public void hookSubprojectTask(Project project, SpotlessTask task) {
}
}

public static Provider<SpotlessTaskService> 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.
* <p>
* 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<SpotlessTaskService>},
* so each copy uses the first service name which isn't taken by another copy.
*/
public static Provider<SpotlessTaskService> registerIfAbsent(Project project) {
BuildServiceRegistry services = project.getGradle().getSharedServices();
for (int copy = 0;; ++copy) {
String name = copy == 0 ? "SpotlessTaskService" : "SpotlessTaskService" + copy;
Provider<SpotlessTaskService> 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
* <a href="https://github.com/diffplug/spotless/issues/2877">#2877</a>
*/
class SiblingClassloaderTest extends GradleIntegrationHarness {
private static final List<String> 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");
}
}
Loading