Compare commits

...

6 Commits

5 changed files with 66 additions and 42 deletions

View File

@@ -1,5 +1,28 @@
# Changelog
## 2026-03-05 - 4.1.13 - fix(mod_unpack)
Use child_process.execSync (mv/rm) to perform unpack atomically, replacing async fs operations and logs to avoid ENOENT/partial directory listings on XFS
- Replaced async fs.promises.rename/rm and readdir/stat debugging with execSync rm -rf and mv operations for sequential, atomic moves
- Imported execSync from child_process and removed verbose console logging and extra fs checks
- Addresses race conditions observed on filesystems like XFS where libuv async operations can return partial results or ENOENT errors
## 2026-03-05 - 4.1.12 - fix(mod_compiler)
replace runtime require calls with top-level imports and use execSync/path.join for filesystem sync and traversal
- Added top-level imports: path and execSync from child_process
- Replaced require('child_process').execSync('sync') with execSync('sync') to force fs sync
- Replaced require('path').join(...) with path.join(...) when recursing directories
- Refactor is purely local/maintenance-focused (consistency and slight performance/readability improvement); no functional change expected
## 2026-03-05 - 4.1.11 - fix(mod_compiler)
flush directory entries before unpack to avoid XFS delayed-log causing partial readdir results
- Add fs import and call child_process.execSync('sync') before unpacking compiled output to force kernel-level sync
- Add syncDirectoryTree(dir) to recursively open and fsync directory file descriptors and traverse subdirectories
- Call syncDirectoryTree on the destination directory before performing unpack to ensure TypeScript writeFileSync entries are committed (prevents partial readdir results on XFS)
- Errors during directory sync are ignored to avoid breaking normal flow
## 2026-03-05 - 4.1.10 - fix(unpack)
use atomic renames to flatten nested output and make unpacking more reliable

View File

@@ -1,6 +1,6 @@
{
"name": "@git.zone/tsbuild",
"version": "4.1.10",
"version": "4.1.13",
"private": false,
"description": "A tool for compiling TypeScript files using the latest nightly features, offering flexible APIs and a CLI for streamlined development.",
"main": "dist_ts/index.js",

View File

@@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@git.zone/tsbuild',
version: '4.1.10',
version: '4.1.13',
description: 'A tool for compiling TypeScript files using the latest nightly features, offering flexible APIs and a CLI for streamlined development.'
}

View File

@@ -1,5 +1,8 @@
import type { CompilerOptions, Diagnostic, Program } from 'typescript';
import typescript from 'typescript';
import * as fs from 'fs';
import * as path from 'path';
import { execSync } from 'child_process';
import * as smartdelay from '@push.rocks/smartdelay';
import * as smartpromise from '@push.rocks/smartpromise';
import * as smartpath from '@push.rocks/smartpath';
@@ -367,6 +370,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.
execSync('sync');
this.syncDirectoryTree(destDir);
try {
await performUnpack(pattern, destDir, this.cwd);
} catch (unpackErr: any) {
@@ -495,6 +504,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(path.join(dirPath, entry.name));
}
}
} catch {
// Ignore errors (directory may not exist)
}
}
/**
* Merge multiple error summaries into one
*/

View File

@@ -1,5 +1,5 @@
import * as fs from 'fs';
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,11 +83,10 @@ export class TsUnpacker {
/**
* Perform the unpack operation - flatten nested output directories.
*
* Strategy: instead of listing entries and moving them individually (which is
* vulnerable to async readdir returning partial results under signal pressure),
* we rename the entire nested directory out, remove the dest dir, then rename
* the nested directory back as the dest dir. This uses only rename operations
* which are atomic at the kernel level.
* 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.
*
* Returns true if unpacking was performed, false if skipped.
*/
@@ -103,40 +102,12 @@ export class TsUnpacker {
const nestedPath = this.getNestedPath();
const tempPath = this.destDir + '.__unpack_temp__';
// Log what we're about to do
const nestedEntries = fs.readdirSync(nestedPath);
console.log(` 📦 Unpacking ${this.sourceFolderName}/: ${nestedEntries.length} entries in nested dir`);
console.log(` 📦 Entries: [${nestedEntries.join(', ')}]`);
// Also list the dest dir to see what TypeScript created
const destEntries = fs.readdirSync(this.destDir);
console.log(` 📦 destDir entries: [${destEntries.join(', ')}]`);
// Clean up any leftover temp dir from a previous failed unpack
await fs.promises.rm(tempPath, { recursive: true, force: true });
// Step 1: Rename the nested source folder out to a temp location.
// e.g. dist_ts/ts/ → dist_ts.__unpack_temp__/
await fs.promises.rename(nestedPath, tempPath);
// Verify step 1
const tempEntries = fs.readdirSync(tempPath);
console.log(` 📦 Step 1 (rename to temp): ${tempEntries.length} entries in temp`);
// Step 2: Remove the dest dir (which now only contains sibling folders
// like ts_interfaces/). Use recursive rm to handle any contents.
await fs.promises.rm(this.destDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
console.log(` 📦 Step 2 (remove dest): done`);
// Step 3: Rename the temp dir to the dest dir.
// e.g. dist_ts.__unpack_temp__/ → dist_ts/
await fs.promises.rename(tempPath, this.destDir);
// Verify final state
const finalEntries = fs.readdirSync(this.destDir);
const finalDirs = finalEntries.filter((e: string) => {
return fs.statSync(path.join(this.destDir, e)).isDirectory();
});
console.log(` 📦 Step 3 (rename to dest): ${finalEntries.length} entries (${finalDirs.length} dirs)`);
// 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' });
return true;
}