|
15 | 15 | */
|
16 | 16 | package com.diffplug.gradle.spotless;
|
17 | 17 |
|
| 18 | +import java.io.ByteArrayOutputStream; |
18 | 19 | import java.io.File;
|
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.file.Files; |
19 | 22 | import java.util.ArrayList;
|
| 23 | +import java.util.Arrays; |
20 | 24 | import java.util.Collections;
|
21 | 25 | import java.util.List;
|
22 | 26 |
|
|
32 | 36 |
|
33 | 37 | import com.diffplug.spotless.FileSignature;
|
34 | 38 | import com.diffplug.spotless.Formatter;
|
| 39 | +import com.diffplug.spotless.ThrowingEx; |
35 | 40 | import com.diffplug.spotless.extra.integration.DiffMessageFormatter;
|
36 | 41 |
|
37 | 42 | public class SpotlessCheck extends DefaultTask {
|
@@ -76,10 +81,33 @@ public void visitDir(FileVisitDetails fileVisitDetails) {
|
76 | 81 | public void visitFile(FileVisitDetails fileVisitDetails) {
|
77 | 82 | String path = fileVisitDetails.getPath();
|
78 | 83 | File originalSource = new File(getProject().getProjectDir(), path);
|
79 |
| - problemFiles.add(originalSource); |
| 84 | + try { |
| 85 | + // read the file on disk |
| 86 | + byte[] userFile = Files.readAllBytes(originalSource.toPath()); |
| 87 | + // and the formatted version from spotlessOutDirectory |
| 88 | + byte[] formatted; |
| 89 | + { |
| 90 | + ByteArrayOutputStream clean = new ByteArrayOutputStream(); |
| 91 | + fileVisitDetails.copyTo(clean); |
| 92 | + formatted = clean.toByteArray(); |
| 93 | + } |
| 94 | + // If these two are equal, it means that SpotlessTask left a file |
| 95 | + // in its output directory which ought to have been removed. As |
| 96 | + // best I can tell, this is a filesytem race which is very hard |
| 97 | + // to trigger. GitRatchetGradleTest can *sometimes* reproduce it |
| 98 | + // but it's very erratic, and that test writes both to gradle cache |
| 99 | + // and git cache very quickly. Either of gradle or jgit might be |
| 100 | + // caching something wrong because of the fast repeated writes. |
| 101 | + if (!Arrays.equals(userFile, formatted)) { |
| 102 | + // If the on-disk content is equal to the formatted content, |
| 103 | + // just don't add it as a problem file. Easy! |
| 104 | + problemFiles.add(originalSource); |
| 105 | + } |
| 106 | + } catch (IOException e) { |
| 107 | + throw ThrowingEx.asRuntime(e); |
| 108 | + } |
80 | 109 | }
|
81 | 110 | });
|
82 |
| - |
83 | 111 | if (!problemFiles.isEmpty()) {
|
84 | 112 | try (Formatter formatter = source.buildFormatter()) {
|
85 | 113 | Collections.sort(problemFiles);
|
|
0 commit comments