Skip to content
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
42 changes: 35 additions & 7 deletions packages/build/src/esbuild.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from "fs"
import * as path from "path"
import { execSync } from "child_process"

import { ViewsContainer, Views, Menus, Configuration, contributesSchema } from "./types.js"

Expand All @@ -22,23 +23,50 @@
return count
}

function rmDir(dirPath: string, maxRetries: number = 3): void {
function rmDir(dirPath: string, maxRetries: number = 5): void {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
fs.rmSync(dirPath, { recursive: true, force: true })
return
} catch (error) {
const isLastAttempt = attempt === maxRetries

const isEnotemptyError =
error instanceof Error && "code" in error && (error.code === "ENOTEMPTY" || error.code === "EBUSY")
const isRetryableError =
error instanceof Error &&
"code" in error &&
(error.code === "ENOTEMPTY" ||
error.code === "EBUSY" ||
error.code === "EPERM" ||
error.code === "EACCES")

if (isLastAttempt) {
// On the last attempt, try alternative cleanup methods.
try {
console.warn(`[rmDir] Final attempt using alternative cleanup for ${dirPath}`)

// Try to clear readonly flags on Windows.
if (process.platform === "win32") {
try {
execSync(`attrib -R "${dirPath}\\*.*" /S /D`, { stdio: "ignore" })
} catch {
// Ignore attrib errors.
}
}
fs.rmSync(dirPath, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 })
return
} catch (finalError) {
console.error(`[rmDir] Failed to remove ${dirPath} after ${maxRetries} attempts:`, finalError)
throw finalError
}
}

if (isLastAttempt || !isEnotemptyError) {
throw error // Re-throw if it's the last attempt or not a locking error.
if (!isRetryableError) {
throw error // Re-throw if it's not a retryable error.
}

// Wait with exponential backoff before retrying.
const delay = Math.min(100 * Math.pow(2, attempt - 1), 1000) // Cap at 1s.
// Wait with exponential backoff before retrying, with longer delays for Windows.
const baseDelay = process.platform === "win32" ? 200 : 100
const delay = Math.min(baseDelay * Math.pow(2, attempt - 1), 2000) // Cap at 2s
console.warn(`[rmDir] Attempt ${attempt} failed for ${dirPath}, retrying in ${delay}ms...`)

// Synchronous sleep for simplicity in build scripts.
Expand All @@ -62,7 +90,7 @@

if (stats.isDirectory()) {
if (fs.existsSync(path.join(dstDir, dstRelPath))) {
rmDir(path.join(dstDir, dstRelPath))

Check warning

Code scanning / CodeQL

Unsafe shell command constructed from library input Medium

This path concatenation which depends on
library input
is later used in a
shell command
.

Check warning

Code scanning / CodeQL

Unsafe shell command constructed from library input Medium

This path concatenation which depends on
library input
is later used in a
shell command
.
}

fs.mkdirSync(path.join(dstDir, dstRelPath), { recursive: true })
Expand Down
4 changes: 3 additions & 1 deletion src/esbuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ async function main() {
build.onEnd((result) => {
result.errors.forEach(({ text, location }) => {
console.error(`✘ [ERROR] ${text}`)
console.error(` ${location.file}:${location.line}:${location.column}:`)
if (location && location.file) {
console.error(` ${location.file}:${location.line}:${location.column}:`)
}
})

console.log("[esbuild-problem-matcher#onEnd]")
Expand Down
Loading