Skip to content

Fix hard-to-reproduce race condition in SpotlessCheck. #671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 25, 2020
Merged
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
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
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]
### Fixed
* 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).

## [5.1.2] - 2020-08-21
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
*/
package com.diffplug.gradle.spotless;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand All @@ -32,6 +36,7 @@

import com.diffplug.spotless.FileSignature;
import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.ThrowingEx;
import com.diffplug.spotless.extra.integration.DiffMessageFormatter;

public class SpotlessCheck extends DefaultTask {
Expand Down Expand Up @@ -76,10 +81,33 @@ public void visitDir(FileVisitDetails fileVisitDetails) {
public void visitFile(FileVisitDetails fileVisitDetails) {
String path = fileVisitDetails.getPath();
File originalSource = new File(getProject().getProjectDir(), path);
problemFiles.add(originalSource);
try {
// read the file on disk
byte[] userFile = Files.readAllBytes(originalSource.toPath());
// and the formatted version from spotlessOutDirectory
byte[] formatted;
{
ByteArrayOutputStream clean = new ByteArrayOutputStream();
fileVisitDetails.copyTo(clean);
formatted = clean.toByteArray();
}
// If these two are equal, it means that SpotlessTask left a file
// in its output directory which ought to have been removed. As
// best I can tell, this is a filesytem race which is very hard
// to trigger. GitRatchetGradleTest can *sometimes* reproduce it
// but it's very erratic, and that test writes both to gradle cache
// and git cache very quickly. Either of gradle or jgit might be
// caching something wrong because of the fast repeated writes.
if (!Arrays.equals(userFile, formatted)) {
// If the on-disk content is equal to the formatted content,
// just don't add it as a problem file. Easy!
problemFiles.add(originalSource);
}
} catch (IOException e) {
throw ThrowingEx.asRuntime(e);
}
}
});

if (!problemFiles.isEmpty()) {
try (Formatter formatter = source.buildFormatter()) {
Collections.sort(problemFiles);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.diffplug.spotless.kotlin;

import org.junit.Ignore;
import org.junit.Test;

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