Skip to content

Commit f3dc480

Browse files
committed
fix: increase robustness by recreating cache dir
1 parent c35888a commit f3dc480

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

lib/src/main/java/com/diffplug/spotless/npm/NodeModulesCachingNpmProcessFactory.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 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,11 +38,11 @@ public class NodeModulesCachingNpmProcessFactory implements NpmProcessFactory {
3838

3939
private NodeModulesCachingNpmProcessFactory(@Nonnull File cacheDir) {
4040
this.cacheDir = Objects.requireNonNull(cacheDir);
41-
assertDir(cacheDir);
42-
this.shadowCopy = new ShadowCopy(cacheDir);
41+
assertDir(); // throws if cacheDir is not a directory
42+
this.shadowCopy = new ShadowCopy(this::assertDir);
4343
}
4444

45-
private void assertDir(File cacheDir) {
45+
private synchronized File assertDir() {
4646
if (cacheDir.exists() && !cacheDir.isDirectory()) {
4747
throw new IllegalArgumentException("Cache dir must be a directory");
4848
}
@@ -51,6 +51,7 @@ private void assertDir(File cacheDir) {
5151
throw new IllegalArgumentException("Cache dir could not be created.");
5252
}
5353
}
54+
return cacheDir;
5455
}
5556

5657
public static NodeModulesCachingNpmProcessFactory create(@Nonnull File cacheDir) {

lib/src/main/java/com/diffplug/spotless/npm/ShadowCopy.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 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.
@@ -27,6 +27,7 @@
2727
import java.nio.file.attribute.BasicFileAttributes;
2828
import java.time.Duration;
2929
import java.util.concurrent.TimeoutException;
30+
import java.util.function.Supplier;
3031

3132
import javax.annotation.Nonnull;
3233

@@ -39,13 +40,18 @@ class ShadowCopy {
3940

4041
private static final Logger logger = LoggerFactory.getLogger(ShadowCopy.class);
4142

42-
private final File shadowCopyRoot;
43+
private final Supplier<File> shadowCopyRootSupplier;
4344

44-
public ShadowCopy(@Nonnull File shadowCopyRoot) {
45-
this.shadowCopyRoot = shadowCopyRoot;
45+
public ShadowCopy(@Nonnull Supplier<File> shadowCopyRootSupplier) {
46+
this.shadowCopyRootSupplier = shadowCopyRootSupplier;
47+
}
48+
49+
private File shadowCopyRoot() {
50+
File shadowCopyRoot = shadowCopyRootSupplier.get();
4651
if (!shadowCopyRoot.isDirectory()) {
47-
throw new IllegalArgumentException("Shadow copy root must be a directory: " + shadowCopyRoot);
52+
throw new IllegalStateException("Shadow copy root must be a directory: " + shadowCopyRoot);
4853
}
54+
return shadowCopyRoot;
4955
}
5056

5157
public void addEntry(String key, File orig) {
@@ -86,17 +92,17 @@ private void cleanupReservation(String key) {
8692
}
8793

8894
private Path markerFilePath(String key) {
89-
return Paths.get(shadowCopyRoot.getAbsolutePath(), key + ".marker");
95+
return Paths.get(shadowCopyRoot().getAbsolutePath(), key + ".marker");
9096
}
9197

9298
private File entry(String key, String origName) {
93-
return Paths.get(shadowCopyRoot.getAbsolutePath(), key, origName).toFile();
99+
return Paths.get(shadowCopyRoot().getAbsolutePath(), key, origName).toFile();
94100
}
95101

96102
private boolean reserveSubFolder(String key) {
97103
// put a marker file named "key".marker in "shadowCopyRoot" to make sure no other process is using it or return false if it already exists
98104
try {
99-
Files.createFile(Paths.get(shadowCopyRoot.getAbsolutePath(), key + ".marker"));
105+
Files.createFile(Paths.get(shadowCopyRoot().getAbsolutePath(), key + ".marker"));
100106
return true;
101107
} catch (FileAlreadyExistsException e) {
102108
return false;

testlib/src/test/java/com/diffplug/spotless/npm/ShadowCopyTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 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.
@@ -29,6 +29,7 @@
2929
import org.junit.jupiter.api.BeforeEach;
3030
import org.junit.jupiter.api.Test;
3131

32+
import com.diffplug.common.base.Suppliers;
3233
import com.diffplug.spotless.ResourceHarness;
3334

3435
class ShadowCopyTest extends ResourceHarness {
@@ -43,7 +44,7 @@ class ShadowCopyTest extends ResourceHarness {
4344
@BeforeEach
4445
void setUp() throws IOException {
4546
shadowCopyRoot = newFolder("shadowCopyRoot");
46-
shadowCopy = new ShadowCopy(shadowCopyRoot);
47+
shadowCopy = new ShadowCopy(Suppliers.ofInstance(shadowCopyRoot));
4748
}
4849

4950
@Test

0 commit comments

Comments
 (0)