+ * 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
+ * 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();