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 CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This document is intended for Spotless developers.
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Added
- Add `DiffMessageFormatter.Builder.showAllFailures(boolean)` to list every failing file instead of only counting them. ([#2418](https://github.com/diffplug/spotless/issues/2418))
### 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))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2025 DiffPlug
* 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.
Expand Down Expand Up @@ -118,6 +118,7 @@ private Builder() {}
private String runToFix;
private CleanProvider formatter;
private List<File> problemFiles;
private boolean showAllFailures;

/** "Run 'gradlew spotlessApply' to fix these violations." */
public Builder runToFix(String runToFix) {
Expand All @@ -141,13 +142,22 @@ public Builder problemFiles(List<File> problemFiles) {
return this;
}

/**
* If true, every problem file which didn't fit in the diff is listed by name, instead of
* only counting them once there are {@link DiffMessageFormatter#MAX_FILES_TO_LIST} or more. Defaults to false.
*/
public Builder showAllFailures(boolean showAllFailures) {
this.showAllFailures = showAllFailures;
return this;
}

/** Returns the error message. */
public String getMessage() {
try {
Objects.requireNonNull(runToFix, "runToFix");
Objects.requireNonNull(formatter, "formatter");
Objects.requireNonNull(problemFiles, "problemFiles");
DiffMessageFormatter diffFormater = new DiffMessageFormatter(formatter, problemFiles);
DiffMessageFormatter diffFormater = new DiffMessageFormatter(formatter, problemFiles, showAllFailures);
return "The following files had format violations:\n"
+ diffFormater.buffer
+ runToFix;
Expand All @@ -165,7 +175,7 @@ public String getMessage() {

private final CleanProvider formatter;

private DiffMessageFormatter(CleanProvider formatter, List<File> problemFiles) throws IOException {
private DiffMessageFormatter(CleanProvider formatter, List<File> problemFiles, boolean showAllFailures) throws IOException {
this.formatter = Objects.requireNonNull(formatter, "formatter");
ListIterator<File> problemIter = problemFiles.listIterator();
while (problemIter.hasNext() && numLines < MAX_CHECK_MESSAGE_LINES) {
Expand All @@ -174,7 +184,7 @@ private DiffMessageFormatter(CleanProvider formatter, List<File> problemFiles) t
}
if (problemIter.hasNext()) {
int remainingFiles = problemFiles.size() - problemIter.nextIndex();
if (remainingFiles >= MAX_FILES_TO_LIST) {
if (remainingFiles >= MAX_FILES_TO_LIST && !showAllFailures) {
buffer.append("Violations also present in ").append(remainingFiles).append(" other files.\n");
} else {
buffer.append("Violations also present in:\n");
Expand Down
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
- `spotlessCheck` lists every failing file instead of only counting them when run with `-PspotlessShowAllFailures`. ([#2418](https://github.com/diffplug/spotless/issues/2418))
### 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
8 changes: 8 additions & 0 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2235,6 +2235,14 @@ tasks.withType(com.diffplug.gradle.spotless.SpotlessApply).configureEach {

`changedFilesReport` is an output of the apply task, so another task can declare it as an input (e.g. `inputs.file(tasks.named('spotlessJavaApply').flatMap { it.changedFilesReport })`) and Gradle will run the apply task first, without an explicit `dependsOn`.

### Which files does `spotlessCheck` complain about?

When many files are misformatted, `spotlessCheck` shows a diff for the first few and then only says `Violations also present in N other files.` To list every one of them by name, pass `-PspotlessShowAllFailures` (or put `spotlessShowAllFailures=true` in `gradle.properties`):

```console
user@machine repo % ./gradlew spotlessCheck -PspotlessShowAllFailures
```

## Can I apply Spotless to specific files?

The Gradle plugin does not read a `spotlessFiles` property like the Maven plugin does (`-PspotlessFiles` was deprecated in `4.3.0` and removed in `5.0.0`). To format one or a few files, pass their absolute paths to the [IDE hook](IDE_HOOK.md) as a comma-separated list:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public abstract class SpotlessCheck extends SpotlessTaskService.ClientTask {
@Input
public abstract Property<String> getRunToFixMessage();

/** Lists every failing file instead of a count once there are too many, defaults to {@code -PspotlessShowAllFailures}. */
@Input
public abstract Property<Boolean> getShowAllFailures();

static final String SHOW_ALL_FAILURES = "spotlessShowAllFailures";

public void performActionTest() throws IOException {
performAction(true);
}
Expand Down Expand Up @@ -79,6 +85,7 @@ private void performAction(boolean isTest) throws IOException {
getSpotlessCleanDirectory().get().toPath(),
getEncoding().get())
.problemFiles(unformattedFiles)
.showAllFailures(getShowAllFailures().get())
.getMessage());
} else if (!lintsFiles.isEmpty()) {
// We only show lints if there are no unformatted files.
Expand Down Expand Up @@ -146,6 +153,8 @@ void init(TaskProvider<SpotlessTaskImpl> impl) {
getEncoding().set(impl.map(SpotlessTask::getEncoding));
getRunToFixMessage().convention(
"Run '" + calculateGradleCommand() + " " + spotlessApplySelector(getProject()) + "' to fix all violations.");
String showAllFailures = GradleCompat.findOptionalProperty(getProject(), SHOW_ALL_FAILURES);
getShowAllFailures().convention(showAllFailures != null && !showAllFailures.equalsIgnoreCase("false"));
}

private static String calculateGradleCommand() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2025 DiffPlug
* 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.
Expand Down Expand Up @@ -317,6 +317,33 @@ void manyManyFiles() throws Exception {
"Violations also present in " + DiffMessageFormatter.MAX_FILES_TO_LIST + " other files.");
}

@Test
void manyManyFilesShowAllFailures() throws Exception {
List<File> testFiles = new ArrayList<>();
for (int i = 0; i < 9 + DiffMessageFormatter.MAX_FILES_TO_LIST; i++) {
String fileName = "%02d".formatted(i) + ".txt";
testFiles.add(setFile(fileName).toContent("1\r\n2\r\n"));
}
Bundle spotless = create(testFiles);
spotless.check.get().getShowAllFailures().set(true);
String msg = spotless.checkFailureMsg();
Assertions.assertThat(msg)
.doesNotContain("other files.")
.contains(StringPrinter.buildStringFromLines(
" ... (2 more lines that didn't fit)",
"Violations also present in:",
" 09.txt",
" 10.txt",
" 11.txt",
" 12.txt",
" 13.txt",
" 14.txt",
" 15.txt",
" 16.txt",
" 17.txt",
" 18.txt"));
}

@Test
void longFile() throws Exception {
StringBuilder builder = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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 java.io.IOException;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class ShowAllFailuresTest extends GradleIntegrationHarness {
private static final int FILE_COUNT = 30;

@BeforeEach
void createProjectWithManyBadFiles() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"spotless {",
" format 'misc', {",
" target '*.txt'",
" leadingTabsToSpaces(2)",
" }",
"}");
for (int i = 0; i < FILE_COUNT; i++) {
setFile("%02d.txt".formatted(i)).toContent("\thello\n");
}
}

@Test
void summaryByDefault() throws IOException {
String output = gradleRunner().withArguments("spotlessCheck").buildAndFail().getOutput();
Assertions.assertThat(output)
.contains(" other files.")
.doesNotContain(" %02d.txt".formatted(FILE_COUNT - 1));
}

@Test
void listsEveryFileWithProperty() throws IOException {
String output = gradleRunner().withArguments("spotlessCheck", "-PspotlessShowAllFailures").buildAndFail().getOutput();
Assertions.assertThat(output)
.doesNotContain(" other files.")
.contains("Violations also present in:");
for (int i = 0; i < FILE_COUNT; i++) {
Assertions.assertThat(output).contains(" %02d.txt".formatted(i));
}
}

@Test
void listsEveryFileWithGradleProperties() throws IOException {
setFile("gradle.properties").toContent("spotlessShowAllFailures=true");
String output = gradleRunner().withArguments("spotlessCheck").buildAndFail().getOutput();
Assertions.assertThat(output)
.doesNotContain(" other files.")
.contains(" %02d.txt".formatted(FILE_COUNT - 1));
}

@Test
void summaryWhenPropertyIsFalse() throws IOException {
String output = gradleRunner().withArguments("spotlessCheck", "-PspotlessShowAllFailures=false").buildAndFail().getOutput();
Assertions.assertThat(output).contains(" other files.");
}
}
Loading