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 @@ -4,6 +4,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]
### Fixed
- If a format task is excluded, e.g. `-x spotlessJava`, its `spotlessJavaApply` and `spotlessJavaCheck` tasks are skipped too, instead of using the output of an earlier build and overwriting the current sources with it. ([#2097](https://github.com/diffplug/spotless/issues/2097))
- 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))
### Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ protected void createFormatTasks(String name, FormatExtension formatExtension) {
});
rootCheckTask.configure(task -> task.dependsOn(checkTask));

// apply and check read the output of the format task, so if the format task was excluded
// (e.g. `-x spotlessJava`) that output is stale and they must not run either (#2097)
String pathPrefix = project.getPath().equals(":") ? ":" : project.getPath() + ":";
project.getGradle().getTaskGraph().whenReady(graph -> {
if (!graph.hasTask(pathPrefix + taskName)) {
if (graph.hasTask(pathPrefix + taskName + APPLY)) {
applyTask.get().setEnabled(false);
}
if (graph.hasTask(pathPrefix + taskName + CHECK)) {
checkTask.get().setEnabled(false);
}
}
});

// create the diagnose task
TaskProvider<SpotlessDiagnoseTask> diagnoseTask = tasks.register(taskName + DIAGNOSE, SpotlessDiagnoseTask.class, task -> {
task.source = spotlessTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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 static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;

import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.TaskOutcome;
import org.junit.jupiter.api.Test;

/** https://github.com/diffplug/spotless/issues/2097 */
class ExcludedSpotlessTaskTest extends GradleIntegrationHarness {
private void writeBuildFile() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"spotless {",
" format 'misc', {",
" target 'src/*.md'",
" custom 'lowercase', { str -> str.toLowerCase(Locale.ROOT) }",
" }",
"}");
}

/** Runs the format task once, so that its output holds a formatted copy of the file, then edits the file. */
private void formatThenEdit() throws IOException {
writeBuildFile();
setFile("src/test.md").toContent("ABC");
gradleRunner().withArguments("spotlessMisc").build();
assertFile("src/test.md").hasContent("ABC");
setFile("src/test.md").toContent("XYZ");
}

@Test
void applyDoesNotUseStaleOutputWhenFormatTaskIsExcluded() throws IOException {
formatThenEdit();
BuildResult result = gradleRunner().withArguments("spotlessApply", "-x", "spotlessMisc").build();
assertThat(result.task(":spotlessMiscApply").getOutcome()).isEqualTo(TaskOutcome.SKIPPED);
assertFile("src/test.md").hasContent("XYZ");
}

@Test
void checkDoesNotUseStaleOutputWhenFormatTaskIsExcluded() throws IOException {
formatThenEdit();
BuildResult result = gradleRunner().withArguments("spotlessCheck", "-x", "spotlessMisc").build();
assertThat(result.task(":spotlessMiscCheck").getOutcome()).isEqualTo(TaskOutcome.SKIPPED);
}

@Test
void applyDoesNotUseStaleOutputWhenFormatTaskIsExcludedWithConfigurationCache() throws IOException {
formatThenEdit();
for (int i = 0; i < 2; ++i) {
BuildResult result = gradleRunner().withArguments("--configuration-cache", "spotlessApply", "-x", "spotlessMisc").build();
assertThat(result.task(":spotlessMiscApply").getOutcome()).isEqualTo(TaskOutcome.SKIPPED);
assertFile("src/test.md").hasContent("XYZ");
}
}

@Test
void applyStillWorksWhenFormatTaskIsNotExcluded() throws IOException {
formatThenEdit();
gradleRunner().withArguments("spotlessApply").build();
assertFile("src/test.md").hasContent("xyz");
}
}