fix(mod_compiler): flush directory entries before unpack to avoid XFS delayed-log causing partial readdir results

This commit is contained in:
2026-03-05 13:21:03 +00:00
parent e1198c0a78
commit 920095d008
3 changed files with 37 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
import type { CompilerOptions, Diagnostic, Program } from 'typescript';
import typescript from 'typescript';
import * as fs from 'fs';
import * as smartdelay from '@push.rocks/smartdelay';
import * as smartpromise from '@push.rocks/smartpromise';
import * as smartpath from '@push.rocks/smartpath';
@@ -367,6 +368,12 @@ export class TsCompiler {
// Perform unpack if compilation succeeded
if (result.errorSummary.totalErrors === 0) {
// Force XFS to commit all pending directory entries before unpacking.
// TypeScript's writeFileSync creates entries that may reside in XFS's
// delayed log. Without sync, readdir can return partial results.
require('child_process').execSync('sync');
this.syncDirectoryTree(destDir);
try {
await performUnpack(pattern, destDir, this.cwd);
} catch (unpackErr: any) {
@@ -495,6 +502,27 @@ export class TsCompiler {
return success;
}
/**
* Recursively fsync all directories in a tree.
* Forces XFS to commit pending directory entries from its log.
*/
private syncDirectoryTree(dirPath: string): void {
try {
const fd = fs.openSync(dirPath, 'r');
fs.fsyncSync(fd);
fs.closeSync(fd);
const entries = fs.readdirSync(dirPath, { withFileTypes: true });
for (const entry of entries) {
if (entry.isDirectory()) {
this.syncDirectoryTree(require('path').join(dirPath, entry.name));
}
}
} catch {
// Ignore errors (directory may not exist)
}
}
/**
* Merge multiple error summaries into one
*/