Skip to content
Open
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
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/compiler"
---

Report better error message when specifying an emitter that is not installed with `--emit` flag
6 changes: 6 additions & 0 deletions packages/compiler/src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,12 @@ const diagnostics = {
default: paramMessage`onValidate failed with errors. ${"error"}`,
},
},
"emitter-not-found": {
severity: "error",
messages: {
default: paramMessage`Emitter "${"emitterPackage"}" not found. Make sure to install it with \`npm install ${"emitterPackage"}\`.`,
},
},
"invalid-emitter": {
severity: "error",
messages: {
Expand Down
15 changes: 13 additions & 2 deletions packages/compiler/src/core/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,14 +582,25 @@ async function createProgram(
async function loadLibrary(
basedir: string,
libraryNameOrPath: string,
context?: { isEmitter?: boolean },
): Promise<LibraryInstance | undefined> {
const [resolution, diagnostics] = await resolveEmitterModuleAndEntrypoint(
basedir,
libraryNameOrPath,
);

if (resolution === undefined) {
program.reportDiagnostics(diagnostics);
if (context?.isEmitter && diagnostics.some((d) => d.code === "import-not-found")) {
program.reportDiagnostic(
createDiagnostic({
code: "emitter-not-found",
format: { emitterPackage: libraryNameOrPath },
target: NoTarget,
}),
);
} else {
program.reportDiagnostics(diagnostics);
}
return undefined;
}
const { module, entrypoint } = resolution;
Expand All @@ -610,7 +621,7 @@ async function createProgram(
emitterNameOrPath: string,
emittersOptions: Record<string, EmitterOptions>,
): Promise<EmitterRef | undefined> {
const library = await loadLibrary(basedir, emitterNameOrPath);
const library = await loadLibrary(basedir, emitterNameOrPath, { isEmitter: true });

if (library === undefined) {
return undefined;
Expand Down
15 changes: 14 additions & 1 deletion packages/compiler/test/core/emitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TypeSpecLibrary,
TypeSpecLibraryDef,
} from "../../src/index.js";
import { expectDiagnosticEmpty } from "../../src/testing/expect.js";
import { expectDiagnosticEmpty, expectDiagnostics } from "../../src/testing/expect.js";
import { mockFile } from "../../src/testing/fs.js";
import { Tester } from "../tester.js";

Expand Down Expand Up @@ -109,3 +109,16 @@ it("when using dry-run only call emitter with the capabilities", async () => {
expect(emitter2.$onEmit).not.toHaveBeenCalled();
expect(emitter3.$onEmit).not.toHaveBeenCalled();
});

it("reports emitter-not-found when emitter package is not installed", async () => {
const diagnostics = await Tester.diagnose("model Foo {}", {
compilerOptions: {
emit: ["not-installed-emitter"],
},
});

expectDiagnostics(diagnostics, {
code: "emitter-not-found",
message: `Emitter "not-installed-emitter" not found. Make sure to install it with \`npm install not-installed-emitter\`.`,
});
});
Loading