fix(compiler): Improve diagnostic checking and error handling in the TypeScript compiler integration

This commit is contained in:
Philipp Kunz 2025-03-17 10:27:36 +00:00
parent 31834e0b3e
commit 4f2ac6922a
3 changed files with 51 additions and 21 deletions

View File

@ -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

View File

@ -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.'
}

View File

@ -93,27 +93,50 @@ export const compiler = async (
console.log(`Compiling ${fileNames.length} files...`);
const done = plugins.smartpromise.defer<any[]>();
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) {