diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 67e6658fad..a7bca5279b 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Added +- Add `failOnNotConverging()` to every format, which makes the format task fail on a file whose formatting does not converge, instead of skipping it with a warning while `spotlessCheck` passes. ([#3143](https://github.com/diffplug/spotless/issues/3143)) ### 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)) diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index 82c9dc377c..b97e22d430 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -78,6 +78,7 @@ Spotless supports all of Gradle's built-in performance features (incremental bui - [Requirements](#requirements) - [Git hook (optional)](#git-hook) - [Linting](#linting) + - [Non-converging files](#non-converging-files) - **Languages** - [Java](#java) ([removeUnusedImports](#removeunusedimports), [forbidWildcardImports](#forbidwildcardimports), [expandWildcardImports](#expandwildcardimports), [forbidModuleImports](#forbidmoduleimports), [shortenFullyQualifiedTypes](#shortenfullyqualifiedtypes), [replaceStaticImports](#replacestaticimports), [google-java-format](#google-java-format), [eclipse jdt](#eclipse-jdt), [clang-format](#clang-format), [prettier](#prettier), [palantir-java-format](#palantir-java-format), [prince-of-space](#prince-of-space), [formatAnnotations](#formatAnnotations), [cleanthat](#cleanthat), [tabletest-formatter](#tabletest-formatter), [IntelliJ IDEA](#intellij-idea)) - [Groovy](#groovy) ([eclipse groovy](#eclipse-groovy)) @@ -212,6 +213,22 @@ spotless { Spotless is primarily a formatter, _not_ a linter. In our opinion, a linter is just a broken formatter. But formatters do break sometimes, and representing these failures as lints that can be suppressed is more useful than just giving up. +### Non-converging files + +If a formatter cannot converge on a file (formatting it repeatedly never settles on a stable result), Spotless skips that file with a warning and `spotlessCheck` still passes, even though the file was never checked. `spotlessApply` cannot fix such a file either. To make a non-converging file fail the build instead, use `failOnNotConverging()`, available in every format: + +```gradle +spotless { + markdown { + target '*.md' + flexmark() + failOnNotConverging() // a green spotlessCheck means every target file was actually checked + } +} +``` + +Default is the old behavior (skip with a warning), so adding `failOnNotConverging()` cannot break an existing build unless it actually has a non-converging file. If you only want to investigate such a file, run `./gradlew spotlessDiagnose` ([PADDEDCELL.md](https://github.com/diffplug/spotless/blob/main/PADDEDCELL.md)). + ## Java diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java index 565fd22315..6d85e0525d 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java @@ -196,6 +196,21 @@ public void suppressLintsFor(Action lintSuppression) { lintSuppressions.add(suppression); } + private boolean failOnNotConverging = false; + + /** + * If a formatter cannot converge on a file (formatting it repeatedly never settles on a stable + * result), then by default the file is skipped with a warning, and {@code spotlessCheck} passes + * even though the file was never checked ({@code spotlessApply} cannot fix it either). Calling + * this method makes the format task fail on such a file instead, so a green {@code spotlessCheck} + * means that every target file was actually checked. + *

+ * See [this issue](https://github.com/diffplug/spotless/issues/3143). + */ + public void failOnNotConverging() { + failOnNotConverging = true; + } + /** * Ignores errors in the given step. * @@ -1162,6 +1177,7 @@ public void toggleOffOnDisable() { protected void setupTask(SpotlessTask task) { task.setEncoding(getEncoding().name()); task.setLintSuppressions(lintSuppressions); + task.setFailOnNotConverging(failOnNotConverging); FileCollection totalTarget = targetExclude == null ? target : excludeFrom(target); task.setTarget(totalTarget); List steps; diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java index 713c9a2e86..b2d045701e 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2025 DiffPlug + * Copyright 2020-2026 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -135,6 +135,25 @@ public List getLintSuppressions() { return lintSuppressions; } + protected boolean failOnNotConverging = false; + + /** + * If {@code false} (the default), a file which the formatter cannot converge on is skipped with + * a warning, and the check task passes even though the file was never checked. If {@code true}, + * the format task fails on such a file instead, so a green check task means that every target + * file was actually checked. + *

+ * See {@link FormatExtension#failOnNotConverging()}. + */ + @Input + public boolean getFailOnNotConverging() { + return failOnNotConverging; + } + + public void setFailOnNotConverging(boolean failOnNotConverging) { + this.failOnNotConverging = failOnNotConverging; + } + protected FileCollection target; @PathSensitive(PathSensitivity.RELATIVE) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java index 88bc15e628..ed355e67cb 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java @@ -142,6 +142,10 @@ void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter, File in // Remove previous output if it exists Files.deleteIfExists(cleanFile.toPath()); } else if (lintState.getDirtyState().didNotConverge()) { + if (getFailOnNotConverging()) { + throw new GradleException("Spotless error! '" + relativePath + + "' does not converge. Run {@code spotlessDiagnose} to understand why, or remove {@code failOnNotConverging()} to skip this file with a warning instead."); + } getLogger().warn("Skipping '{}' because it does not converge. Run {@code spotlessDiagnose} to understand why", relativePath); } else { Path parentDir = cleanFile.toPath().getParent(); diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FailOnNotConvergingTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FailOnNotConvergingTest.java new file mode 100644 index 0000000000..ccd6f6a55e --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FailOnNotConvergingTest.java @@ -0,0 +1,64 @@ +/* + * Copyright 2016-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.IOException; + +import org.assertj.core.api.Assertions; +import org.gradle.testkit.runner.BuildResult; +import org.junit.jupiter.api.Test; + +/** + * A file which the formatter cannot converge on is skipped with a warning, and {@code spotlessCheck} + * passes even though the file was never checked. {@code failOnNotConverging()} makes such a file fail + * the build instead. See https://github.com/diffplug/spotless/issues/3143 + */ +class FailOnNotConvergingTest extends GradleIntegrationHarness { + private void writeBuild(boolean failOnNotConverging) throws IOException { + String flagLine = failOnNotConverging ? " failOnNotConverging()" : ""; + setFile("build.gradle").toContent(String.join("\n", + "plugins {", + " id 'com.diffplug.spotless'", + "}", + "spotless {", + " format 'misc', {", + " lineEndings 'UNIX'", + " target file('README.md')", + flagLine, + " custom 'divergeStep', {", + " it + ' more'", + " }", + " } // format", + "} // spotless")); + } + + @Test + void skipsByDefault() throws Exception { + writeBuild(false); + setFile("README.md").toContent("unstable"); + // the file does not converge, but the check still passes (warning only) + gradleRunner().withArguments("spotlessCheck").build(); + assertFile("README.md").hasContent("unstable"); + } + + @Test + void failsWhenOptIn() throws Exception { + writeBuild(true); + setFile("README.md").toContent("unstable"); + BuildResult result = gradleRunner().withArguments("spotlessCheck").buildAndFail(); + Assertions.assertThat(result.getOutput()).contains("does not converge"); + } +} diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java index fb061078f8..7c60d3e3f9 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java @@ -240,6 +240,17 @@ void paddedCellCheckConvergeFailureMsg() throws IOException { DiffMessageFormatterTest.EXPECTED_RUN_SPOTLESS_APPLY_SUGGESTION); } + @Test + void failOnNotConvergingFailsCheck() throws Exception { + Bundle diverge = diverge(); + // without the flag, the file is skipped with a warning and the check passes (see paddedCellApplyCheck) + diverge.source.get().setFailOnNotConverging(true); + // relativePath is always unix-style (LintSuppression.relativizeAsUnix), so the message is platform-independent + Assertions.assertEquals( + "Spotless error! 'src/test.diverge' does not converge. Run {@code spotlessDiagnose} to understand why, or remove {@code failOnNotConverging()} to skip this file with a warning instead.", + diverge.checkFailureMsg()); + } + private void assertFailureMessage(Bundle bundle, String... expectedOutput) { String msg = bundle.checkFailureMsg(); String expected = StringPrinter.buildStringFromLines(expectedOutput).trim();