diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 67e6658fad..8cd587eda1 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -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 diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtensionImpl.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtensionImpl.java index 34dfa13fd4..7f1aca35fb 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtensionImpl.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtensionImpl.java @@ -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 diagnoseTask = tasks.register(taskName + DIAGNOSE, SpotlessDiagnoseTask.class, task -> { task.source = spotlessTask; diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ExcludedSpotlessTaskTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ExcludedSpotlessTaskTest.java new file mode 100644 index 0000000000..8b222d02ec --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ExcludedSpotlessTaskTest.java @@ -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"); + } +}