From c2690390507c833f42b03217aee0a33e21bf0029 Mon Sep 17 00:00:00 2001 From: Juergen Kunz Date: Thu, 5 Mar 2026 12:19:24 +0000 Subject: [PATCH] fix(fs/compiler/unpack): robustify directory removal and remove noisy diagnostic logging --- changelog.md | 9 +++++ ts/00_commitinfo_data.ts | 2 +- ts/mod_compiler/classes.tscompiler.ts | 49 --------------------------- ts/mod_fs/classes.fshelpers.ts | 13 +++++-- ts/mod_unpack/classes.tsunpacker.ts | 16 +-------- 5 files changed, 22 insertions(+), 67 deletions(-) diff --git a/changelog.md b/changelog.md index 8205811..2fb0685 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,14 @@ # Changelog +## 2026-03-05 - 4.1.7 - fix(fs/compiler/unpack) +robustify directory removal and remove noisy diagnostic logging + +- Use fs.promises.rm with force, maxRetries and retryDelay in removeDirectory to reduce intermittent failures during removals. +- Handle ENOTEMPTY in removeEmptyDirectory by falling back to a recursive rm and ignore ENOENT to avoid errors from transient filesystem metadata lag. +- Remove several diagnostic/verbose console logs in the compiler and unpacker paths to reduce noisy output and simplify flow. +- Short-circuit unpack logic when no nesting is detected to avoid unnecessary work and logs. +- No public API or behavior-breaking changes; suitable for a patch release. + ## 2026-03-05 - 4.1.6 - fix(mod_compiler) add diagnostic logging to report dist_ts and output directory contents after each compilation task and after import-path rewriting diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index d355d21..7052239 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@git.zone/tsbuild', - version: '4.1.6', + version: '4.1.7', description: 'A tool for compiling TypeScript files using the latest nightly features, offering flexible APIs and a CLI for streamlined development.' } diff --git a/ts/mod_compiler/classes.tscompiler.ts b/ts/mod_compiler/classes.tscompiler.ts index 482b3f1..6baaefc 100644 --- a/ts/mod_compiler/classes.tscompiler.ts +++ b/ts/mod_compiler/classes.tscompiler.ts @@ -365,48 +365,13 @@ export class TsCompiler { emittedFiles.push(...result.emittedFiles); errorSummaries.push(result.errorSummary); - // DIAGNOSTIC: Log directory contents after compilation - if (!isQuiet && !isJson) { - try { - const fs = await import('fs'); - const dirEntries = fs.readdirSync(destDir, { withFileTypes: true }); - const dirs = dirEntries.filter((e: any) => e.isDirectory()).map((e: any) => e.name).sort(); - const fileCount = dirEntries.filter((e: any) => e.isFile()).length; - console.log(` 📋 [DIAG] After emit: ${dirs.length} dirs [${dirs.join(', ')}], ${fileCount} top-level files`); - console.log(` 📋 [DIAG] emittedFiles.length=${result.emittedFiles.length}, totalErrors=${result.errorSummary.totalErrors}`); - } catch (e: any) { console.log(` 📋 [DIAG] Error reading destDir: ${e.message}`); } - } - // Perform unpack if compilation succeeded if (result.errorSummary.totalErrors === 0) { await performUnpack(pattern, destDir, this.cwd); - // DIAGNOSTIC: Log directory contents after unpack - if (!isQuiet && !isJson) { - try { - const fs = await import('fs'); - const dirEntries = fs.readdirSync(destDir, { withFileTypes: true }); - const dirs = dirEntries.filter((e: any) => e.isDirectory()).map((e: any) => e.name).sort(); - const fileCount = dirEntries.filter((e: any) => e.isFile()).length; - console.log(` 📋 [DIAG] After unpack: ${dirs.length} dirs [${dirs.join(', ')}], ${fileCount} top-level files`); - } catch (e: any) { console.log(` 📋 [DIAG] Error reading destDir after unpack: ${e.message}`); } - } - successfulOutputDirs.push(destDir); } - // DIAGNOSTIC: Check dist_ts after each task (not just current destDir) - if (!isQuiet && !isJson) { - try { - const fs = await import('fs'); - const distTsPath = require('path').join(this.cwd, 'dist_ts'); - if (fs.existsSync(distTsPath)) { - const dirEntries = fs.readdirSync(distTsPath, { withFileTypes: true }); - const dirs = dirEntries.filter((e: any) => e.isDirectory()).map((e: any) => e.name).sort(); - console.log(` 📋 [DIAG-CROSSTASK] dist_ts after task ${currentTask}: ${dirs.length} dirs [${dirs.join(', ')}]`); - } - } catch (e: any) { /* ignore */ } - } } // Rewrite import paths in all output directories to handle cross-module references @@ -423,20 +388,6 @@ export class TsCompiler { } } - // DIAGNOSTIC: Check dist_ts after path rewriting - if (!isQuiet && !isJson) { - for (const outputDir of successfulOutputDirs) { - try { - const fs = await import('fs'); - const dirEntries = fs.readdirSync(outputDir, { withFileTypes: true }); - const dirs = dirEntries.filter((e: any) => e.isDirectory()).map((e: any) => e.name).sort(); - const fileCount = dirEntries.filter((e: any) => e.isFile()).length; - const relDir = outputDir.replace(this.cwd, '').replace(/^\//, ''); - console.log(` 📋 [DIAG-FINAL] ${relDir}: ${dirs.length} dirs [${dirs.join(', ')}], ${fileCount} files`); - } catch (e: any) { console.log(` 📋 [DIAG-FINAL] Error: ${e.message}`); } - } - } - // Merge all error summaries const finalErrorSummary = this.mergeErrorSummaries(errorSummaries); diff --git a/ts/mod_fs/classes.fshelpers.ts b/ts/mod_fs/classes.fshelpers.ts index eeb4126..ae11b96 100644 --- a/ts/mod_fs/classes.fshelpers.ts +++ b/ts/mod_fs/classes.fshelpers.ts @@ -125,7 +125,7 @@ export class FsHelpers { * Remove a directory recursively */ public static async removeDirectory(dirPath: string): Promise { - await fs.promises.rm(dirPath, { recursive: true }); + await fs.promises.rm(dirPath, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 }); } /** @@ -139,6 +139,15 @@ export class FsHelpers { * Remove an empty directory */ public static async removeEmptyDirectory(dirPath: string): Promise { - await fs.promises.rmdir(dirPath); + try { + await fs.promises.rmdir(dirPath); + } catch (err: any) { + if (err.code === 'ENOTEMPTY') { + // Filesystem metadata lag — use recursive rm as fallback + await fs.promises.rm(dirPath, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 }); + } else if (err.code !== 'ENOENT') { + throw err; + } + } } } diff --git a/ts/mod_unpack/classes.tsunpacker.ts b/ts/mod_unpack/classes.tsunpacker.ts index 222e435..17aa663 100644 --- a/ts/mod_unpack/classes.tsunpacker.ts +++ b/ts/mod_unpack/classes.tsunpacker.ts @@ -87,30 +87,16 @@ export class TsUnpacker { public async unpack(): Promise { // Check if we should unpack based on config if (!(await this.shouldUnpack())) { - console.log(` 📋 [DIAG-UNPACK] Skipping unpack: shouldUnpack=false`); return false; } // Check if nested structure exists - const hasNesting = await this.detectNesting(); - console.log(` 📋 [DIAG-UNPACK] detectNesting=${hasNesting}, nestedPath=${this.getNestedPath()}`); - if (!hasNesting) { + if (!(await this.detectNesting())) { return false; } const nestedPath = this.getNestedPath(); - // DIAGNOSTIC: Log contents before unpack - try { - const destEntries = fs.readdirSync(this.destDir, { withFileTypes: true }); - const destDirs = destEntries.filter(e => e.isDirectory()).map(e => e.name).sort(); - console.log(` 📋 [DIAG-UNPACK] Before removeSibling: destDir dirs=[${destDirs.join(', ')}]`); - const nestedEntries = fs.readdirSync(nestedPath, { withFileTypes: true }); - const nestedDirs = nestedEntries.filter(e => e.isDirectory()).map(e => e.name).sort(); - const nestedFiles = nestedEntries.filter(e => e.isFile()).length; - console.log(` 📋 [DIAG-UNPACK] Nested dir (${this.sourceFolderName}/): ${nestedDirs.length} dirs [${nestedDirs.join(', ')}], ${nestedFiles} files`); - } catch (e: any) { console.log(` 📋 [DIAG-UNPACK] Error reading: ${e.message}`); } - // Delete sibling folders (not the source folder) await this.removeSiblingDirectories();