microsoft / microsoft/typespec
[protobuf] Best practices for configuring onValidate/onEmit
- Dominant language
- Java
- Stars
- 5.9k
- Forks
- 394
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 104
Description
Currently we handle emitting and validating like this:
```ts
/**
* Emitter main function.
*
* @param program - the program to emit
*/
export async function $onEmit(ctx: EmitContext>) {
const emitter = createProtobufEmitter(ctx.program);
await emitter(resolvePath(ctx.emitterOutputDir), ctx.options);
}
/**
* Validation function
*/
export async function $onValidate(program: Program) {
// Is this correct? See
/* c8 ignore next 6 */
if (program.compilerOptions.noEmit) {
const options = program.emitters.find((e) => e.emitFunction === $onEmit)
?.options as ProtobufEmitterOptions;
const emitter = createProtobufEmitter(program);
await emitter("", options);
}
}
```
But this seems a little weird. The reason we only run `onValidate` when `noEmit` is set is that all diagnostics come from the emitter itself. We just run the emitter with no `outDir` when `noEmit` is set in the `onValidate` function. There is definitely a better way, but without this check we get double diagnostics because the emitter does the emit pass twice: once during `onValidate` and again during `onEmit.
Contributor guide
Assessment
This issue has not been assessed yet.