Skip to content

Fix taskService type clash when Spotless is applied to sibling projects (#2877) - #3202

Open
Pankraz76 wants to merge 1 commit into
diffplug:mainfrom
Pankraz76:fix/2877-sibling-classloader-task-service
Open

Pankraz76 wants to merge 1 commit into
diffplug:mainfrom
Pankraz76:fix/2877-sibling-classloader-task-service

Conversation

@Pankraz76

Copy link
Copy Markdown

Fixes #2877

Summary

Since 8.3.0, a multi-project build fails at configuration time when Spotless is applied to sibling projects but not to their parent:

Cannot set the value of task ':b:spotlessMisc' property 'taskService' of type com.diffplug.gradle.spotless.SpotlessTaskService
using a provider of type com.diffplug.gradle.spotless.SpotlessTaskService.

Each copy of the plugin (one per buildscript classloader) now gets its own SpotlessTaskService registration. The apply false workaround on the root project is no longer needed.

Root cause

# Finding Evidence
1 Gradle loads a separate copy of the plugin for each buildscript classloader. This happens when sibling projects apply Spotless with different plugin classpaths and there is no common parent that applies it. Stack trace in #2877: project-integTest vs project-incap classloaders. Reproduced locally, see "Tests" below.
2 Since #2854 (commit f7829b5, isolated projects), SpotlessExtension calls SpotlessTaskService.registerIfAbsent(project, ""), so every copy registers the build service under the same name, SpotlessTaskService. SpotlessExtension.java
3 BuildServiceRegistry.registerIfAbsent returns the existing registration by name and does not check its type. The second copy therefore gets a provider for the first copy's SpotlessTaskService class, and Property<SpotlessTaskService>.set(...) rejects it. Error message above: same class name, different classloaders
4 Before 8.3.0 this worked by accident. The root-project RegisterDependenciesTask lookup failed with a cast error for the second copy, and its fallback (#1001) registered the task and the service with an identityHashCode suffix. #2854 removed that path from non-predeclared builds. git show f7829b55c
5 @ServiceReference (suggested in the issue and in Gradle's hint) would not fix this. The injected service would still be an instance of another classloader's class. Analysis
6 Since #2854, RegisterDependenciesTask ("SpotlessTaskService" + compositeBuildSuffix) and SpotlessExtension ("SpotlessTaskService") could also end up on different services in the composite-build fallback. RegisterDependenciesTask.setup()

Fix

SpotlessTaskService.registerIfAbsent(Project) registers under the first name not taken by another copy of the plugin: SpotlessTaskService, then SpotlessTaskService1, and so on.

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;
	}
}

Design notes:

  • The common case is unchanged. With a single classloader the name is still SpotlessTaskService.
  • Safe under parallel configuration. It registers first and then checks the type, rather than checking and then registering. A concurrent registration by another copy is detected, and the loop moves on to the next name.
  • Isolated projects stay supported. It does not access the root project and uses only public Gradle API (getSharedServices(), registerIfAbsent).
  • Predeclared dependencies. RegisterDependenciesTask now uses the same lookup, so it always uses the same service as the SpotlessExtension of its own plugin copy. This removes the divergence described in finding 6. The identityHashCode task-name fallback for composite builds (fix: #860 composite gradle build #1001) is unchanged.
  • Trade-off. The service is now created when the spotless extension is created, during configuration, instead of when the first step resolves its provisioner. Configuration already did this for almost every real build (FormatExtension.provisioner(), SpotlessExtensionPredeclare). The full test suite stays green.

Tests

A new SiblingClassloaderTest puts the plugin on each subproject's own buildscript classpath and does not apply it to the root project.

Two pitfalls the test has to work around:

  • TestKit's withPluginClasspath() injects one classloader shared by all projects, which hides the bug.
  • Gradle shares one classloader between subprojects whose buildscript classpaths are identical. Each subproject therefore gets an extra, existing classpath directory, which is what happens in real builds when projects apply different plugin sets. A path that doesn't exist is ignored and doesn't separate the classloaders. I verified this manually by printing SpotlessTaskService.classLoader per project.
Scenario Before fix After fix
Sibling classloaders: spotlessApply, then spotlessCheck. Both projects are formatted and check passes ❌ Cannot set the value of task ':b:spotlessMisc' property 'taskService' ✅
Sibling classloaders, dirty files: spotlessCheck --continue fails in both :a:spotlessMiscCheck and :b:spotlessMiscCheck ❌ same error ✅
Sibling classloaders + org.gradle.configuration-cache=true: spotlessApply twice. The cache entry is stored, then reused, and changes are applied ❌ same error ✅

Checklist

🤖 Generated with Claude Code

https://claude.ai/code/session_01MTr23wtv6sQmXLnujeLnnP

Since 8.3.0 (diffplug#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 diffplug#2877

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MTr23wtv6sQmXLnujeLnnP

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression in Gradle plugin v8.3.0: Cannot set the value of task ':integTest:spotlessJava' property 'taskService'

2 participants