Skip to content

Commit e891e27

Browse files
committed
fix: make sure to use separate cache-dirs for separate basedirs
without this, one spotless-cli call from one directory could re-use the directory of another spotless-cli call. This is a very probable concurrency issue which proved to be an issue in unit-tests (where multiple unit tests could be running on the same command line concurrently).
1 parent 16f91cc commit e891e27

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

app/src/main/java/com/diffplug/spotless/cli/core/ExecutionLayout.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@
1717

1818
import java.nio.file.Path;
1919
import java.nio.file.Paths;
20-
import java.util.List;
2120
import java.util.Objects;
2221
import java.util.Optional;
22+
import java.util.stream.Stream;
2323

2424
import org.jetbrains.annotations.NotNull;
2525
import org.jetbrains.annotations.Nullable;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
2628

2729
import com.diffplug.spotless.cli.steps.BuildDirGloballyReusable;
2830
import com.diffplug.spotless.cli.steps.SpotlessCLIFormatterStep;
2931

3032
public class ExecutionLayout {
3133

34+
private static final Logger LOGGER = LoggerFactory.getLogger(ExecutionLayout.class);
35+
3236
private final FileResolver fileResolver;
3337
private final SpotlessCommandLineStream commandLineStream;
3438
private final ChecksumCalculator checksumCalculator;
@@ -76,18 +80,23 @@ public Path baseDir() {
7680
}
7781

7882
public Path buildDir() {
79-
// gradle?
8083
if (isGradleDirectory()) {
81-
return gradleBuildDir();
84+
Path gradleBuildDir = gradleBuildDir();
85+
LOGGER.info("Using Gradle build directory as buildDir: {}", gradleBuildDir);
86+
return gradleBuildDir;
8287
}
8388
if (isMavenDirectory()) {
84-
return mavenBuildDir();
89+
Path mavenBuildDir = mavenBuildDir();
90+
LOGGER.info("Using Maven build directory as buildDir: {}", mavenBuildDir);
91+
return mavenBuildDir;
8592
}
86-
return tempBuildDir();
93+
Path tempBuildDir = tempBuildDir();
94+
LOGGER.info("Using temporary build directory as buildDir: {}", tempBuildDir);
95+
return tempBuildDir;
8796
}
8897

8998
private boolean isGradleDirectory() {
90-
return List.of("build.gradle", "build.gradle.kts", "settings.gradle", "settings.gradle.kts").stream()
99+
return Stream.of("build.gradle", "build.gradle.kts", "settings.gradle", "settings.gradle.kts")
91100
.map(Paths::get)
92101
.map(this::find)
93102
.anyMatch(Optional::isPresent);
@@ -98,7 +107,7 @@ private Path gradleBuildDir() {
98107
}
99108

100109
private boolean isMavenDirectory() {
101-
return List.of("pom.xml").stream().map(Paths::get).map(this::find).anyMatch(Optional::isPresent);
110+
return Stream.of("pom.xml").map(Paths::get).map(this::find).anyMatch(Optional::isPresent);
102111
}
103112

104113
private Path mavenBuildDir() {
@@ -107,7 +116,9 @@ private Path mavenBuildDir() {
107116

108117
private Path tempBuildDir() {
109118
String tmpDir = System.getProperty("java.io.tmpdir");
110-
return Path.of(tmpDir, "spotless-cli", String.valueOf(deriveId));
119+
String baseDirHash =
120+
checksumCalculator.calculateChecksum(baseDir().toAbsolutePath().toString());
121+
return Path.of(tmpDir, "spotless-cli", baseDirHash, String.valueOf(deriveId));
111122
}
112123

113124
public Path buildDirFor(@NotNull SpotlessCLIFormatterStep step) {

0 commit comments

Comments
 (0)