Skip to content

Commit 94e5671

Browse files
authored
fix: Handle cross-volume renames of intermediate files (#1264)
1 parent 64f39ae commit 94e5671

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed

src/bundle.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { rename } from 'node:fs/promises';
21
import { dirname, basename, resolve } from 'node:path';
32
import { fileURLToPath } from 'node:url';
43
import { build, type Plugin } from 'esbuild';
54

5+
import { moveFile } from './files.js';
66
import { swallowTopLevelExportsPlugin } from './swallowTopLevelExportsPlugin.js';
77

88
const __filename = fileURLToPath(import.meta.url);
@@ -197,8 +197,8 @@ export async function bundle(
197197
inject,
198198
});
199199

200-
await rename(bundle, outfile);
200+
await moveFile(bundle, outfile);
201201
if (enableStackTraces) {
202-
await rename(bundle + '.map', outfile + '.map');
202+
await moveFile(bundle + '.map', outfile + '.map');
203203
}
204204
}

src/compileApplicationToWasm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { rmSync } from 'node:fs';
1515
import weval from '@bytecodealliance/weval';
1616
import wizer from '@bytecodealliance/wizer';
1717

18-
import { isDirectory, isFile } from './isFile.js';
18+
import { isDirectory, isFile } from './files.js';
1919
import { postbundle } from './postbundle.js';
2020
import { bundle } from './bundle.js';
2121
import { composeSourcemaps, ExcludePattern } from './composeSourcemaps.js';

src/files.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { stat, rename, copyFile, unlink } from 'node:fs/promises';
2+
3+
export async function isFile(path: string) {
4+
const stats = await stat(path);
5+
return stats.isFile();
6+
}
7+
8+
export async function isDirectory(path: string) {
9+
const stats = await stat(path);
10+
return stats.isDirectory();
11+
}
12+
13+
export async function moveFile(src: string, dest: string): Promise<void> {
14+
try {
15+
await rename(src, dest);
16+
} catch (err: unknown) {
17+
if (!isErrnoException(err) || err.code !== 'EXDEV') {
18+
throw err;
19+
}
20+
21+
// Cross-device move: copy + delete
22+
await copyFile(src, dest);
23+
await unlink(src);
24+
}
25+
}
26+
27+
function isErrnoException(err: unknown): err is NodeJS.ErrnoException {
28+
return (
29+
typeof err === 'object' &&
30+
err !== null &&
31+
'code' in err &&
32+
typeof err.code === 'string'
33+
);
34+
}

src/isFile.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)