From 76ce3caf3d3f65ba5c6997b85ad77f761d510948 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Wed, 28 Aug 2024 11:56:22 +0200 Subject: [PATCH] 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. --- src/core/path.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/core/path.ts b/src/core/path.ts index 0c691ae031..f7c847d7e5 100644 --- a/src/core/path.ts +++ b/src/core/path.ts @@ -26,6 +26,7 @@ import * as ld from "./lodash.ts"; import { getenv } from "./env.ts"; import { execProcess } from "./process.ts"; +import { isWindows } from "./platform.ts"; export const kSkipHidden = /[/\\][\.]/; @@ -50,8 +51,25 @@ export function safeRemoveSync( try { Deno.removeSync(file, options); } catch (e) { + let lastError = e; + // WINDOWS ONLY: Retry on windows to let time to file to unlock + if (isWindows() && e.code === "EBUSY") { + let nTry: number = 1; + // high number to prevent infinite loop + const maxTry: number = 500; + let eCode = e.code; + while (eCode === "EBUSY" && nTry <= maxTry) { + try { + Deno.removeSync(file, options); + } catch (e) { + lastError = e; + eCode = e.code; + nTry++; + } + } + } if (existsSync(file)) { - throw e; + throw lastError; } } }