Skip to content

Commit 221e3a4

Browse files
[tsp-client] Fix compiler diagnostic reporting (#8547)
* fix compiler diagnostic reporting * use formatDiagnostic * fix exit --------- Co-authored-by: Catalina Peralta <caperal@microsoft.com>
1 parent 3e90e19 commit 221e3a4

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

tools/tsp-client/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,17 @@ async function generate({
219219
args.push("--force");
220220
}
221221
await npmCommand(srcDir, args);
222-
await compileTsp({ emitterPackage: emitter, outputPath: rootUrl, resolvedMainFilePath, saveInputs: noCleanup, additionalEmitterOptions });
222+
const succeeded = await compileTsp({ emitterPackage: emitter, outputPath: rootUrl, resolvedMainFilePath, saveInputs: noCleanup, additionalEmitterOptions });
223223

224224
if (noCleanup) {
225225
Logger.debug(`Skipping cleanup of temp directory: ${tempRoot}`);
226226
} else {
227227
Logger.debug("Cleaning up temp directory");
228228
await removeDirectory(tempRoot);
229229
}
230+
if (!succeeded) {
231+
process.exit(1);
232+
}
230233
}
231234

232235

tools/tsp-client/src/typespec.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export async function compileTsp({
6464
resolvedMainFilePath: string;
6565
additionalEmitterOptions?: string;
6666
saveInputs?: boolean;
67-
}) {
67+
}): Promise<boolean> {
6868
const parsedEntrypoint = getDirectoryPath(resolvedMainFilePath);
6969
const { compile, NodeHost, resolveCompilerOptions } = await importTsp(parsedEntrypoint);
7070

@@ -100,19 +100,21 @@ export async function compileTsp({
100100
Logger.debug(`Compiler options: ${JSON.stringify(options)}`);
101101
if (diagnostics.length > 0) {
102102
// This should not happen, but if it does, we should log it.
103-
Logger.debug(`Compiler options diagnostic information: ${JSON.stringify(diagnostics)}`);
103+
Logger.error("Diagnostics were reported while resolving compiler options...")
104+
diagnostics.forEach((diagnostic) => { Logger.error(formatDiagnostic(diagnostic)); });
105+
return false;
104106
}
105107

106108
const program = await compile(NodeHost, resolvedMainFilePath, options);
107109

108110
if (program.diagnostics.length > 0) {
109-
for (const diagnostic of program.diagnostics) {
110-
Logger.error(formatDiagnostic(diagnostic));
111-
}
112-
process.exit(1);
111+
Logger.error("Diagnostics were reported during compilation...");
112+
program.diagnostics.forEach((diagnostic) => { Logger.error(formatDiagnostic(diagnostic)); });
113+
return false;
113114
} else {
114115
Logger.success("generation complete");
115116
}
117+
return true;
116118
}
117119

118120
export async function importTsp(baseDir: string): Promise<typeof import("@typespec/compiler")> {

0 commit comments

Comments
 (0)