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 @@ -6,6 +6,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
### 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))
- Defer clearing `SpotlessCache` from `clean` to `SpotlessTaskService.close()` at the end of the build, fixing concurrent `ClassNotFoundException` / `NoClassDefFoundError` failures when `clean` runs in parallel with Spotless tasks in another subproject and ensuring `clean` still clears the cache when reusing Gradle's configuration cache. ([#3067](https://github.com/diffplug/spotless/issues/3067))
### Changes
- README: example for formatting Dockerfiles with prettier and `prettier-plugin-sh`. ([#3158](https://github.com/diffplug/spotless/issues/3158))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
*/
package com.diffplug.gradle.spotless;

import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.JavaVersion;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.provider.Provider;
import org.gradle.util.GradleVersion;

import com.diffplug.spotless.Jvm;
import com.diffplug.spotless.SpotlessCache;

public class SpotlessPlugin implements Plugin<Project> {
static final String SPOTLESS_MODERN = "spotlessModern";
Expand All @@ -49,19 +51,32 @@ public void apply(Project project) {
project.getPlugins().apply(BasePlugin.class);

// setup the extension
project.getExtensions().create(SpotlessExtension.class, SpotlessExtension.EXTENSION, SpotlessExtensionImpl.class, project);
SpotlessExtension extension = project.getExtensions().create(SpotlessExtension.class, SpotlessExtension.EXTENSION, SpotlessExtensionImpl.class, project);
if (project.getRootProject() == project) {
project.getExtensions().create(SpotlessExtensionPredeclare.class, SpotlessExtension.EXTENSION_PREDECLARE, SpotlessExtensionPredeclare.class, project);
}

// clear spotless' cache when the user does a clean
// clear spotless' cache at the end of the build when the user does a clean, so parallel
// clean tasks in other subprojects cannot close FeatureClassLoaders still used by Spotless tasks (#3067)
// 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)));
Provider<SpotlessTaskService> taskService = extension.getSpotlessTaskService();
project.getTasks().named(BasePlugin.CLEAN_TASK_NAME).configure(clean -> {
SpotlessTaskService.usesServiceTolerateTestFailure(clean, taskService);
clean.doLast(new ClearSpotlessCacheOnCloseAction(taskService));
});
}

private static final class ClearSpotlessCacheOnCloseAction implements Action<Task> {
private final Provider<SpotlessTaskService> taskService;

ClearSpotlessCacheOnCloseAction(Provider<SpotlessTaskService> taskService) {
this.taskService = taskService;
}

@Override
public void execute(Task task) {
taskService.get().clearCacheOnClose();
}
}

static String capitalize(String input) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.Nullable;
import javax.inject.Inject;

import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.file.ConfigurableFileTree;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileVisitDetails;
Expand All @@ -48,6 +50,7 @@
import com.diffplug.common.base.Unhandled;
import com.diffplug.spotless.Lint;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.SpotlessCache;
import com.diffplug.spotless.extra.P2Provisioner;

/**
Expand All @@ -60,6 +63,7 @@ public abstract class SpotlessTaskService implements BuildService<BuildServicePa
private final Map<String, SpotlessApply> apply = Collections.synchronizedMap(new HashMap<>());
private final Map<String, SpotlessTask> source = Collections.synchronizedMap(new HashMap<>());
private final Map<String, Provisioner> provisioner = Collections.synchronizedMap(new HashMap<>());
private final AtomicBoolean clearCacheOnClose = new AtomicBoolean();

@Nullable GradleProvisioner.DedupingProvisioner predeclaredProvisioner;
@Nullable GradleProvisioner.DedupingP2Provisioner predeclaredP2Provisioner;
Expand Down Expand Up @@ -131,15 +135,27 @@ public void onFinish(FinishEvent var1) {
// NOOP
}

void clearCacheOnClose() {
clearCacheOnClose.set(true);
}

@Override
public void close() throws Exception {
ratchet.close();
try {
ratchet.close();
} finally {
if (clearCacheOnClose.getAndSet(false)) {
// Pass null so every build that runs clean clears the cache
// and SpotlessCache.lastClear does not retain this closed service.
SpotlessCache.clearOnce(null);
}
}
}
// </GitRatchet>

static String INDEPENDENT_HELPER = "Helper";

static void usesServiceTolerateTestFailure(DefaultTask task, Provider<SpotlessTaskService> serviceProvider) {
static void usesServiceTolerateTestFailure(Task task, Provider<SpotlessTaskService> serviceProvider) {
try {
task.usesService(serviceProvider);
} catch (ClassCastException e) {
Expand Down
Loading