Skip to content

Commit 02338b9

Browse files
committed
Homogeneize gradle and maven behavior around Formatter issues on a specific file
1 parent 6789a1d commit 02338b9

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -94,26 +94,34 @@ public void performAction(InputChanges inputs) throws Exception {
9494

9595
private void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter, File input) throws IOException {
9696
File output = getOutputFile(input);
97-
getLogger().debug("Applying format to " + input + " and writing to " + output);
97+
getLogger().debug("Applying format to {} and writing to {}", input, output);
9898
PaddedCell.DirtyState dirtyState;
9999
if (ratchet != null && ratchet.isClean(getProjectDir().get().getAsFile(), getRootTreeSha(), input)) {
100100
dirtyState = PaddedCell.isClean();
101101
} else {
102-
dirtyState = PaddedCell.calculateDirtyState(formatter, input);
102+
try {
103+
dirtyState = PaddedCell.calculateDirtyState(formatter, input);
104+
} catch (IOException e) {
105+
throw new IOException("Issue processing file: " + input, e);
106+
} catch (RuntimeException e) {
107+
throw new IllegalArgumentException("Issue processing file: " + input, e);
108+
}
103109
}
104110
if (dirtyState.isClean()) {
105111
// Remove previous output if it exists
106112
Files.deleteIfExists(output.toPath());
107113
} else if (dirtyState.didNotConverge()) {
108-
getLogger().warn("Skipping '" + input + "' because it does not converge. Run {@code spotlessDiagnose} to understand why");
114+
getLogger().warn("Skipping '{}}' because it does not converge. Run {@code spotlessDiagnose} to understand why", input);
109115
} else {
110116
Path parentDir = output.toPath().getParent();
111117
if (parentDir == null) {
112-
throw new IllegalStateException("Every file has a parent folder.");
118+
throw new IllegalStateException("Every file has a parent folder. But not: " + output);
113119
}
114120
Files.createDirectories(parentDir);
115121
// Need to copy the original file to the tmp location just to remember the file attributes
116122
Files.copy(input.toPath(), output.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
123+
124+
getLogger().info(String.format("Writing clean file: %s", output));
117125
dirtyState.writeCanonicalTo(output);
118126
}
119127
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
5454
} else {
5555
counter.checkedButAlreadyClean();
5656
}
57-
} catch (IOException e) {
57+
} catch (IOException | RuntimeException e) {
5858
throw new MojoExecutionException("Unable to format file " + file, e);
5959
}
6060

plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,9 +38,12 @@ public class SpotlessCheckMojo extends AbstractSpotlessMojo {
3838

3939
@Override
4040
protected void process(Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException {
41+
ImpactedFilesTracker counter = new ImpactedFilesTracker();
42+
4143
List<File> problemFiles = new ArrayList<>();
4244
for (File file : files) {
4345
if (upToDateChecker.isUpToDate(file.toPath())) {
46+
counter.skippedAsCleanCache();
4447
if (getLog().isDebugEnabled()) {
4548
getLog().debug("Spotless will not check an up-to-date file: " + file);
4649
}
@@ -51,14 +54,24 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
5154
PaddedCell.DirtyState dirtyState = PaddedCell.calculateDirtyState(formatter, file);
5255
if (!dirtyState.isClean() && !dirtyState.didNotConverge()) {
5356
problemFiles.add(file);
57+
counter.cleaned();
5458
} else {
59+
counter.checkedButAlreadyClean();
5560
upToDateChecker.setUpToDate(file.toPath());
5661
}
57-
} catch (IOException e) {
62+
} catch (IOException | RuntimeException e) {
5863
throw new MojoExecutionException("Unable to format file " + file, e);
5964
}
6065
}
6166

67+
// We print the number of considered files which is useful when ratchetFrom is setup
68+
if (counter.getTotal() > 0) {
69+
getLog().info(String.format("Spotless.%s is keeping %s files clean - %s needs changes to be clean, %s were already clean, %s were skipped because caching determined they were already clean",
70+
formatter.getName(), counter.getTotal(), counter.getCleaned(), counter.getCheckedButAlreadyClean(), counter.getSkippedAsCleanCache()));
71+
} else {
72+
getLog().debug(String.format("Spotless.%s has no target files. Examine your `<includes>`: https://github.com/diffplug/spotless/tree/main/plugin-maven#quickstart", formatter.getName()));
73+
}
74+
6275
if (!problemFiles.isEmpty()) {
6376
throw new MojoExecutionException(DiffMessageFormatter.builder()
6477
.runToFix("Run 'mvn spotless:apply' to fix these violations.")

0 commit comments

Comments
 (0)