fix(fs): replace execSync and fsync workarounds with atomic async FsHelpers operations to avoid XFS races and shell dependencies

This commit is contained in:
2026-03-05 14:01:00 +00:00
parent 4f8443d33f
commit 49d62e20a4
4 changed files with 24 additions and 48 deletions

View File

@@ -1,5 +1,4 @@
import * as path from 'path';
import { execSync } from 'child_process';
import { TsPublishConfig } from '../mod_config/index.js';
import { FsHelpers } from '../mod_fs/index.js';
@@ -83,10 +82,9 @@ export class TsUnpacker {
/**
* Perform the unpack operation - flatten nested output directories.
*
* Uses shell commands (mv, rm) via execSync for reliability. Node.js async
* fs operations (rename, rm) can race on XFS filesystems where metadata
* commits are delayed, causing ENOENT or partial directory listings.
* Shell commands execute as direct syscalls without libuv's async layer.
* Renames the nested directory to a temp location, removes the dest dir,
* then renames the temp dir back as dest. Uses only rename operations
* which are atomic at the kernel level.
*
* Returns true if unpacking was performed, false if skipped.
*/
@@ -102,12 +100,17 @@ export class TsUnpacker {
const nestedPath = this.getNestedPath();
const tempPath = this.destDir + '.__unpack_temp__';
// Use shell commands for atomic, sequential filesystem operations.
// This avoids race conditions between Node.js async fs operations on XFS.
execSync(`rm -rf "${tempPath}"`, { stdio: 'ignore' });
execSync(`mv "${nestedPath}" "${tempPath}"`, { stdio: 'ignore' });
execSync(`rm -rf "${this.destDir}"`, { stdio: 'ignore' });
execSync(`mv "${tempPath}" "${this.destDir}"`, { stdio: 'ignore' });
// Step 1: Clean up any leftover temp dir from a previous failed run
await FsHelpers.removeDirectory(tempPath);
// Step 2: Rename nested → temp
await FsHelpers.move(nestedPath, tempPath);
// Step 3: Remove dest dir (now contains only sibling folders)
await FsHelpers.removeDirectory(this.destDir);
// Step 4: Rename temp → dest
await FsHelpers.move(tempPath, this.destDir);
return true;
}