Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Improve error report summaries (#45713)
  • Loading branch information
rbargholz committed Sep 6, 2021
commit 0638417eb17d464f41c2c3f4d560262ad31fea70
12 changes: 12 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4834,6 +4834,18 @@
"category": "Message",
"code": 6257
},
"Found 1 error in {1}": {
"category": "Message",
"code": 6258
},
"Found {0} errors in 1 file.": {
"category": "Message",
"code": 6259
},
"Found {0} errors in {1} files.": {
"category": "Message",
"code": 6260
},

"Enable project compilation": {
"category": "Message",
Expand Down
7 changes: 5 additions & 2 deletions src/compiler/tsbuildPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace ts {
return fileExtensionIs(fileName, Extension.Dts);
}

export type ReportEmitErrorSummary = (errorCount: number) => void;
export type ReportEmitErrorSummary = (errorCount: number, filesInError: (string | undefined)[]) => void;

export interface SolutionBuilderHostBase<T extends BuilderProgram> extends ProgramHost<T> {
createDirectory?(path: string): void;
Expand Down Expand Up @@ -2002,10 +2002,12 @@ namespace ts {
const canReportSummary = state.watch || !!state.host.reportErrorSummary;
const { diagnostics } = state;
let totalErrors = 0;
let filesInError: (string | undefined)[] = [];
if (isCircularBuildOrder(buildOrder)) {
reportBuildQueue(state, buildOrder.buildOrder);
reportErrors(state, buildOrder.circularDiagnostics);
if (canReportSummary) totalErrors += getErrorCountForSummary(buildOrder.circularDiagnostics);
if (canReportSummary) filesInError = [...filesInError, ...getFilesInErrorForSummary(buildOrder.circularDiagnostics)];
}
else {
// Report errors from the other projects
Expand All @@ -2016,13 +2018,14 @@ namespace ts {
}
});
if (canReportSummary) diagnostics.forEach(singleProjectErrors => totalErrors += getErrorCountForSummary(singleProjectErrors));
if (canReportSummary) diagnostics.forEach(singleProjectErrors => [...filesInError, ...getFilesInErrorForSummary(singleProjectErrors)]);
}

if (state.watch) {
reportWatchStatus(state, getWatchErrorSummaryDiagnosticMessage(totalErrors), totalErrors);
}
else if (state.host.reportErrorSummary) {
state.host.reportErrorSummary(totalErrors);
state.host.reportErrorSummary(totalErrors, filesInError);
}
}

Expand Down
46 changes: 41 additions & 5 deletions src/compiler/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ts {
} : undefined;

/**
* Create a function that reports error by writing to the system and handles the formating of the diagnostic
* Create a function that reports error by writing to the system and handles the formatting of the diagnostic
*/
export function createDiagnosticReporter(system: System, pretty?: boolean): DiagnosticReporter {
const host: FormatDiagnosticsHost = system === sys && sysFormatDiagnosticsHost ? sysFormatDiagnosticsHost : {
Expand Down Expand Up @@ -101,15 +101,51 @@ namespace ts {
return countWhere(diagnostics, diagnostic => diagnostic.category === DiagnosticCategory.Error);
}

export function getFilesInErrorForSummary(diagnostics: readonly Diagnostic[]) {
return filter(diagnostics, diagnostic => diagnostic.category === DiagnosticCategory.Error)
.map(
errorDiagnostic => {
if(errorDiagnostic.file === undefined) return;
return `${errorDiagnostic.file.fileName}`;
})
.filter((value, index, self) => self !== undefined && self.indexOf(value) === index)
.map((fileName: string) => {
const diagnosticForFileName = find(diagnostics, diagnostic =>
diagnostic.file !== undefined && diagnostic.file.fileName === fileName
);

if(diagnosticForFileName !== undefined) {
const { line } = getLineAndCharacterOfPosition(diagnosticForFileName.file!, diagnosticForFileName.start!);
return `${fileName}:${line + 1}`;
}
});
}

export function getWatchErrorSummaryDiagnosticMessage(errorCount: number) {
return errorCount === 1 ?
Diagnostics.Found_1_error_Watching_for_file_changes :
Diagnostics.Found_0_errors_Watching_for_file_changes;
}

export function getErrorSummaryText(errorCount: number, newLine: string) {
export function getErrorSummaryText(
errorCount: number,
filesInError: readonly (string | undefined)[],
newLine: string
) {
if (errorCount === 0) return "";
const d = createCompilerDiagnostic(errorCount === 1 ? Diagnostics.Found_1_error : Diagnostics.Found_0_errors, errorCount);
const d = errorCount === 1 ?
createCompilerDiagnostic(
filesInError[0] !== undefined ?
Diagnostics.Found_1_error_in_1 :
Diagnostics.Found_1_error,
errorCount,
filesInError[0]) :
createCompilerDiagnostic(
filesInError.length === 1 ?
Diagnostics.Found_0_errors_in_1_file :
Diagnostics.Found_0_errors_in_1_files,
errorCount,
filesInError.length);
return `${newLine}${flattenDiagnosticMessageText(d.messageText, newLine)}${newLine}${newLine}`;
}

Expand Down Expand Up @@ -350,7 +386,7 @@ namespace ts {
}

if (reportSummary) {
reportSummary(getErrorCountForSummary(diagnostics));
reportSummary(getErrorCountForSummary(diagnostics), getFilesInErrorForSummary(diagnostics));
}

return {
Expand Down Expand Up @@ -656,7 +692,7 @@ namespace ts {
builderProgram,
input.reportDiagnostic || createDiagnosticReporter(system),
s => host.trace && host.trace(s),
input.reportErrorSummary || input.options.pretty ? errorCount => system.write(getErrorSummaryText(errorCount, system.newLine)) : undefined
input.reportErrorSummary || input.options.pretty ? (errorCount, filesInError) => system.write(getErrorSummaryText(errorCount, filesInError, system.newLine)) : undefined
);
if (input.afterProgramEmitAndDiagnostics) input.afterProgramEmitAndDiagnostics(builderProgram);
return exitStatus;
Expand Down
2 changes: 1 addition & 1 deletion src/executeCommandLine/executeCommandLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ namespace ts {

function createReportErrorSummary(sys: System, options: CompilerOptions | BuildOptions): ReportEmitErrorSummary | undefined {
return shouldBePretty(sys, options) ?
errorCount => sys.write(getErrorSummaryText(errorCount, sys.newLine)) :
(errorCount, filesInError) => sys.write(getErrorSummaryText(errorCount, filesInError, sys.newLine)) :
undefined;
}

Expand Down
2 changes: 1 addition & 1 deletion src/harness/harnessIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ namespace Harness {
outputLines += content;
}
if (pretty) {
outputLines += ts.getErrorSummaryText(ts.getErrorCountForSummary(diagnostics), IO.newLine());
outputLines += ts.getErrorSummaryText(ts.getErrorCountForSummary(diagnostics), ts.getFilesInErrorForSummary(diagnostics), IO.newLine());
}
return outputLines;
}
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/tsbuild/publicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function f22() { } // trailing`,
/*createProgram*/ undefined,
createDiagnosticReporter(sys, /*pretty*/ true),
createBuilderStatusReporter(sys, /*pretty*/ true),
errorCount => sys.write(getErrorSummaryText(errorCount, sys.newLine))
(errorCount, filesInError) => sys.write(getErrorSummaryText(errorCount, filesInError, sys.newLine))
);
buildHost.afterProgramEmitAndDiagnostics = cb;
buildHost.afterEmitBundle = cb;
Expand Down
9 changes: 7 additions & 2 deletions src/testRunner/unittests/tscWatch/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,18 @@ namespace ts.tscWatch {
assert.equal(host.exitCode, expectedExitCode);
}

export function checkNormalBuildErrors(host: WatchedSystem, errors: readonly Diagnostic[] | readonly string[], reportErrorSummary?: boolean) {
export function checkNormalBuildErrors(
host: WatchedSystem,
errors: readonly Diagnostic[] | readonly string[],
files: readonly string[],
reportErrorSummary?: boolean
) {
checkOutputErrors(
host,
[
...map(errors, hostOutputDiagnostic),
...reportErrorSummary ?
[hostOutputWatchDiagnostic(getErrorSummaryText(errors.length, host.newLine))] :
[hostOutputWatchDiagnostic(getErrorSummaryText(errors.length, files, host.newLine))] :
emptyArray
]
);
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5249,7 +5249,7 @@ declare namespace ts {
traceResolution?: boolean;
[option: string]: CompilerOptionsValue | undefined;
}
type ReportEmitErrorSummary = (errorCount: number) => void;
type ReportEmitErrorSummary = (errorCount: number, filesInError: (string | undefined)[]) => void;
interface SolutionBuilderHostBase<T extends BuilderProgram> extends ProgramHost<T> {
createDirectory?(path: string): void;
/**
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5249,7 +5249,7 @@ declare namespace ts {
traceResolution?: boolean;
[option: string]: CompilerOptionsValue | undefined;
}
type ReportEmitErrorSummary = (errorCount: number) => void;
type ReportEmitErrorSummary = (errorCount: number, filesInError: (string | undefined)[]) => void;
interface SolutionBuilderHostBase<T extends BuilderProgram> extends ProgramHost<T> {
createDirectory?(path: string): void;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@
}
}

Found 2 errors.
Found 2 errors in 1 file.

Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@
!!! error TS2451: Cannot redeclare block-scoped variable 'Bar'.
!!! related TS6203 tests/cases/compiler/file1.ts:2:7: 'Bar' was also declared here.

Found 6 errors.
Found 6 errors in 3 files.

Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@
class H { }
class I { }

Found 2 errors.
Found 2 errors in 2 files.

Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@
!!! related TS6203 tests/cases/compiler/file1.ts:4:5: 'duplicate3' was also declared here.
}

Found 6 errors.
Found 6 errors in 2 files.

Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@
duplicate8(): number;
}

Found 2 errors.
Found 2 errors in 2 files.

Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@
}
export {}

Found 6 errors.
Found 6 errors in 2 files.

Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@
!!! related TS6203 tests/cases/compiler/file2.ts:7:9: 'duplicate3' was also declared here.
}
}
Found 6 errors.
Found 6 errors in 2 files.

Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@
duplicate9: () => string;
}
}
Found 2 errors.
Found 2 errors in 2 files.

Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
!!! error TS2345: Type '{ default: () => void; }' provides no match for the signature '(): void'.
!!! related TS7038 tests/cases/compiler/index.ts:1:1: Type originates at this import. A namespace-style import cannot be called or constructed, and will cause a failure at runtime. Consider using a default import or import require here instead.

Found 1 error.
Found 1 error in tests/cases/compiler/index.ts:3

Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
!!! error TS2322: Object literal may only specify known properties, and 'a' does not exist in type '{ c: string; }'.
};

Found 1 error.
Found 1 error in tests/cases/compiler/multiLineContextDiagnosticWithPretty.ts:2

Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@

!!! error TS1005: '}' expected.
!!! related TS1007 tests/cases/compiler/index.ts:1:11: The parser expected to find a '}' to match the '{' token here.
Found 1 error.
Found 1 error in tests/cases/compiler/index.ts:2

Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
!!! error TS2322: Type 'number' is not assignable to type 'string'.
}
}
Found 3 errors.
Found 3 errors in 1 file.

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Output::
error TS18003: No inputs were found in config file '/src/tsconfig.second.json'. Specified 'include' paths were '["**/*"]' and 'exclude' paths were '[]'.


Found 4 errors.
Found 4 errors in 0 files.

exitCode:: ExitStatus.DiagnosticsPresent_OutputsSkipped

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ Output::
[12:00:00 AM] Skipping build of project '/src/zoo/tsconfig.json' because its dependency '/src/animals' was not built


Found 7 errors.
Found 7 errors in 0 files.

exitCode:: ExitStatus.DiagnosticsPresent_OutputsSkipped

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Output::
   ~~~~~~~~~~~~~~~~~


Found 1 error.
Found 1 error in src/project/tsconfig.json:6

exitCode:: ExitStatus.DiagnosticsPresent_OutputsGenerated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pkg3/src/keys.ts
pkg3/src/index.ts
Matched by include pattern '**/*' in 'pkg3/tsconfig.json'

Found 1 error.
Found 1 error in /user/username/projects/myproject/pkg3/src/keys.ts:2



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pkg3/src/keys.ts
pkg3/src/index.ts
Matched by include pattern '**/*' in 'pkg3/tsconfig.json'

Found 1 error.
Found 1 error in /user/username/projects/myproject/pkg3/src/keys.ts:2



Expand Down
Loading