Skip to content

Commit 927b920

Browse files
committed
windows - wait to file to unlock for removal
while removing dir or file, it happens that EBUSY error could happen with file being used and lock. This is a simple way to wait for file to be unlocked, with a fail safe of max try to avoid infinite loop.
1 parent 0008520 commit 927b920

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/core/path.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import * as ld from "./lodash.ts";
2626

2727
import { getenv } from "./env.ts";
2828
import { execProcess } from "./process.ts";
29+
import { isWindows } from "./platform.ts";
2930

3031
export const kSkipHidden = /[/\\][\.]/;
3132

@@ -50,8 +51,25 @@ export function safeRemoveSync(
5051
try {
5152
Deno.removeSync(file, options);
5253
} catch (e) {
54+
let lastError = e;
55+
// WINDOWS ONLY: Retry on windows to let time to file to unlock
56+
if (isWindows() && e.code === "EBUSY") {
57+
let nTry: number = 1;
58+
// high number to prevent infinite loop
59+
const maxTry: number = 500;
60+
let eCode = e.code;
61+
while (eCode === "EBUSY" && nTry <= maxTry) {
62+
try {
63+
Deno.removeSync(file, options);
64+
} catch (e) {
65+
lastError = e;
66+
eCode = e.code;
67+
nTry++;
68+
}
69+
}
70+
}
5371
if (existsSync(file)) {
54-
throw e;
72+
throw lastError;
5573
}
5674
}
5775
}

0 commit comments

Comments
 (0)