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
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
17 changes: 17 additions & 0 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)).

<a name="applying-to-java-source"></a>

## Java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ public void suppressLintsFor(Action<LintSuppression> 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.
* <p>
* See [this issue](https://github.com/diffplug/spotless/issues/3143).
*/
public void failOnNotConverging() {
failOnNotConverging = true;
}

/**
* Ignores errors in the given step.
*
Expand Down Expand Up @@ -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<FormatterStep> steps;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -135,6 +135,25 @@ public List<LintSuppression> 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.
* <p>
* See {@link FormatExtension#failOnNotConverging()}.
*/
@Input
public boolean getFailOnNotConverging() {
return failOnNotConverging;
}

public void setFailOnNotConverging(boolean failOnNotConverging) {
this.failOnNotConverging = failOnNotConverging;
}

protected FileCollection target;

@PathSensitive(PathSensitivity.RELATIVE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down