diff --git a/changelog.md b/changelog.md index a216931..6d1fd3e 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,12 @@ # Changelog +## 2025-03-17 - 2.2.7 - fix(compiler) +Improve diagnostic checking and error handling in the TypeScript compiler integration + +- Added pre-emit diagnostics check to log errors before emitting +- Process exits on encountering pre-emit errors to ensure build correctness +- Enhanced logging for emit diagnostics to aid debugging + ## 2025-03-04 - 2.2.6 - fix(package) Fix repository URL in package.json diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 845a5bb..345c8ed 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: '2.2.6', + version: '2.2.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/tsbuild.classes.compiler.ts b/ts/tsbuild.classes.compiler.ts index 91db0be..71036b6 100644 --- a/ts/tsbuild.classes.compiler.ts +++ b/ts/tsbuild.classes.compiler.ts @@ -93,27 +93,50 @@ export const compiler = async ( console.log(`Compiling ${fileNames.length} files...`); const done = plugins.smartpromise.defer(); const program = plugins.typescript.createProgram(fileNames, options); + + // Check for pre-emit diagnostics first + const preEmitDiagnostics = plugins.typescript.getPreEmitDiagnostics(program); + let hasErrors = false; + + // Log pre-emit diagnostics if any + if (preEmitDiagnostics.length > 0) { + preEmitDiagnostics.forEach((diagnostic) => { + hasErrors = true; + if (diagnostic.file) { + const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!); + const message = plugins.typescript.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); + console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`); + } else { + console.log( + `${plugins.typescript.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}` + ); + } + }); + } + + // Only continue to emit phase if no pre-emit errors + if (hasErrors) { + console.error('TypeScript pre-emit checks failed. Please fix the issues above.'); + process.exit(1); + } + + // If no pre-emit errors, proceed with emit const emitResult = program.emit(); - - // implement check only - /*let emitResult = program.emit(undefined,(args) => { - console.log(args) - });*/ - - const allDiagnostics = plugins.typescript - .getPreEmitDiagnostics(program) - .concat(emitResult.diagnostics); - allDiagnostics.forEach((diagnostic) => { - if (diagnostic.file) { - const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!); - const message = plugins.typescript.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); - console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`); - } else { - console.log( - `${plugins.typescript.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}` - ); - } - }); + + // Check for emit diagnostics + if (emitResult.diagnostics.length > 0) { + emitResult.diagnostics.forEach((diagnostic) => { + if (diagnostic.file) { + const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!); + const message = plugins.typescript.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); + console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`); + } else { + console.log( + `${plugins.typescript.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}` + ); + } + }); + } const exitCode = emitResult.emitSkipped ? 1 : 0; if (exitCode === 0) {