Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print diagnostics when a protoplugin fails to emit files #1003

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,66 @@ describe("built-in transpile", () => {
]);
});
});

describe("failing to emit", () => {
test("raises error with helpful message", async () => {
await expect(async () =>
testTranspileToDts([
`export interface Foo {`,
` p: {`,
` [K in keyof P]: string;`,
` },`,
`}`,
]),
).rejects.toThrow(
/^A problem occurred during transpilation and files were not generated\. {2}Contact the plugin author for support\.\n/,
);
});
test("raises error with diagnostics", async () => {
await expect(async () =>
testTranspileToDts([
`export interface Foo {`,
` p: {`,
` [K in keyof P]: string;`,
` },`,
`}`,
]),
).rejects.toThrow(
/test\.ts\(3,17\): error TS4033: Property 'p' of exported interface has or is using private name 'P'\.$/,
);
});
test("raises error with 3 diagnostics, and elides the rest", async () => {
await expect(async () =>
testTranspileToDts([
`export interface Foo1 {`,
` p: {`,
` [K in keyof P]: string;`,
` },`,
`}`,
`export interface Foo2 {`,
` p: {`,
` [K in keyof P]: string;`,
` },`,
`}`,
`export interface Foo3 {`,
` p: {`,
` [K in keyof P]: string;`,
` },`,
`}`,
`export interface Foo4 {`,
` p: {`,
` [K in keyof P]: string;`,
` },`,
`}`,
`export interface Foo5 {`,
` p: {`,
` [K in keyof P]: string;`,
` },`,
`}`,
]),
).rejects.toThrow(
/(?:test\.ts\(\d+,\d+\): .+\n){3}2 more diagnostics elided/,
);
});
});
});
28 changes: 27 additions & 1 deletion packages/protoplugin/src/transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,35 @@ export function transpile(
throw err;
}
if (result.emitSkipped) {
// When compilation fails, this error message is printed to stderr.
const diagnostics = formatDiagnostics(result.diagnostics);
throw Error(
"A problem occurred during transpilation and files were not generated. Contact the plugin author for support.",
`A problem occurred during transpilation and files were not generated. Contact the plugin author for support.\n\n${diagnostics}`,
);
}
return results;
}

function formatDiagnostics(diagnostics: readonly ts.Diagnostic[]): string {
const sorted = ts.sortAndDeduplicateDiagnostics(diagnostics);
if (sorted.length == 0) {
return "";
}
const first = sorted.slice(0, 3);
const formatHost: ts.FormatDiagnosticsHost = {
getCanonicalFileName(fileName: string): string {
return fileName;
},
getCurrentDirectory(): string {
return ".";
},
getNewLine(): string {
return "\n";
},
};
let out = ts.formatDiagnostics(first, formatHost).trim();
if (first.length < sorted.length) {
out += `\n${sorted.length - first.length} more diagnostics elided`;
}
return out;
}