Skip to content

Commit f2ad5f9

Browse files
committed
Try to fix build on Windows
1 parent 8670bcc commit f2ad5f9

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

packages/build/src/esbuild.ts

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,56 @@ import * as path from "path"
33

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

6+
function copyDir(srcDir: string, dstDir: string, count: number): number {
7+
const entries = fs.readdirSync(srcDir, { withFileTypes: true })
8+
9+
for (const entry of entries) {
10+
const srcPath = path.join(srcDir, entry.name)
11+
const dstPath = path.join(dstDir, entry.name)
12+
13+
if (entry.isDirectory()) {
14+
fs.mkdirSync(dstPath, { recursive: true })
15+
count = copyDir(srcPath, dstPath, count)
16+
} else {
17+
count = count + 1
18+
fs.copyFileSync(srcPath, dstPath)
19+
}
20+
}
21+
22+
return count
23+
}
24+
25+
function rmDir(dirPath: string, maxRetries: number = 3): void {
26+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
27+
try {
28+
fs.rmSync(dirPath, { recursive: true, force: true })
29+
return
30+
} catch (error) {
31+
const isLastAttempt = attempt === maxRetries
32+
const isEnotemptyError = error instanceof Error && "code" in error && (error.code === 'ENOTEMPTY' || error.code === 'EBUSY')
33+
34+
if (isLastAttempt || !isEnotemptyError) {
35+
throw error // Re-throw if it's the last attempt or not a locking error.
36+
}
37+
38+
// Wait with exponential backoff before retrying.
39+
const delay = Math.min(100 * Math.pow(2, attempt - 1), 1000) // Cap at 1s.
40+
console.warn(`[rmDir] Attempt ${attempt} failed for ${dirPath}, retrying in ${delay}ms...`)
41+
42+
// Synchronous sleep for simplicity in build scripts.
43+
const start = Date.now()
44+
while (Date.now() - start < delay) { /* Busy wait */ }
45+
}
46+
}
47+
}
48+
649
export function copyPaths(copyPaths: [string, string][], srcDir: string, dstDir: string) {
750
copyPaths.forEach(([srcRelPath, dstRelPath]) => {
851
const stats = fs.lstatSync(path.join(srcDir, srcRelPath))
952

1053
if (stats.isDirectory()) {
1154
if (fs.existsSync(path.join(dstDir, dstRelPath))) {
12-
fs.rmSync(path.join(dstDir, dstRelPath), { recursive: true, force: true })
55+
rmDir(path.join(dstDir, dstRelPath))
1356
}
1457

1558
fs.mkdirSync(path.join(dstDir, dstRelPath), { recursive: true })
@@ -23,25 +66,6 @@ export function copyPaths(copyPaths: [string, string][], srcDir: string, dstDir:
2366
})
2467
}
2568

26-
export function copyDir(srcDir: string, dstDir: string, count: number): number {
27-
const entries = fs.readdirSync(srcDir, { withFileTypes: true })
28-
29-
for (const entry of entries) {
30-
const srcPath = path.join(srcDir, entry.name)
31-
const dstPath = path.join(dstDir, entry.name)
32-
33-
if (entry.isDirectory()) {
34-
fs.mkdirSync(dstPath, { recursive: true })
35-
count = copyDir(srcPath, dstPath, count)
36-
} else {
37-
count = count + 1
38-
fs.copyFileSync(srcPath, dstPath)
39-
}
40-
}
41-
42-
return count
43-
}
44-
4569
export function copyWasms(srcDir: string, distDir: string): void {
4670
const nodeModulesDir = path.join(srcDir, "node_modules")
4771

packages/build/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { getGitSha } from "./git.js"
2-
export { copyPaths, copyDir, copyWasms, copyLocales, setupLocaleWatcher, generatePackageJson } from "./esbuild.js"
2+
export { copyPaths, copyWasms, copyLocales, setupLocaleWatcher, generatePackageJson } from "./esbuild.js"

src/esbuild.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async function main() {
4444
*/
4545
const plugins = [
4646
{
47-
name: "copy-files",
47+
name: "copyFiles",
4848
setup(build) {
4949
build.onEnd(() => {
5050
copyPaths(
@@ -62,13 +62,13 @@ async function main() {
6262
},
6363
},
6464
{
65-
name: "copy-wasms",
65+
name: "copyWasms",
6666
setup(build) {
6767
build.onEnd(() => copyWasms(srcDir, distDir))
6868
},
6969
},
7070
{
71-
name: "copy-locales",
71+
name: "copyLocales",
7272
setup(build) {
7373
build.onEnd(() => copyLocales(srcDir, distDir))
7474
},

0 commit comments

Comments
 (0)