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
7 changes: 7 additions & 0 deletions .chronus/changes/fix-subpath-emitter-options-2026-8-22.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/compiler"
---

Resolve tspconfig emitter options for packages exposed as subpath exports.
15 changes: 13 additions & 2 deletions packages/compiler/src/core/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,19 @@ async function createProgram(
const emitFunction = entrypoint.esmExports.$onEmit;
const libDefinition = library.definition;

// Prefer the specifier from tspconfig so subpath exports get matching options.
// Fall back to package.json name for file emitters and older configs.
const optionsFromSpecifier = emittersOptions[emitterNameOrPath];
const optionsFromPackageName =
metadata.name !== undefined ? emittersOptions[metadata.name] : undefined;
Comment on lines +630 to +634
const emitterOptionsKey =
optionsFromSpecifier !== undefined
? emitterNameOrPath
: optionsFromPackageName !== undefined && metadata.name !== undefined
? metadata.name
: emitterNameOrPath;
let { "emitter-output-dir": emitterOutputDir, ...emitterOptions } =
emittersOptions[metadata.name ?? emitterNameOrPath] ?? {};
optionsFromSpecifier ?? optionsFromPackageName ?? {};
if (emitterOutputDir === undefined) {
emitterOutputDir = [options.outputDir, metadata.name].filter(isDefined).join("/");
}
Expand All @@ -644,7 +655,7 @@ async function createProgram(
options.configFile?.file
? {
kind: "path-target",
path: ["options", emitterNameOrPath],
path: ["options", emitterOptionsKey],
script: options.configFile.file,
}
: NoTarget,
Expand Down
92 changes: 92 additions & 0 deletions packages/compiler/test/core/emitter-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,98 @@ it("pass options", async () => {
strictEqual(context.options["max-files"], 10);
});

describe("subpath export emitters", () => {
const subpathLib = createTypeSpecLibrary({
name: "@org/fake-emitter/typescript",
diagnostics: {},
emitter: {
options: {
type: "object",
properties: {
"asset-dir": { type: "string", format: "absolute-path", nullable: true },
"max-files": { type: "number", nullable: true },
},
additionalProperties: false,
},
},
});

async function runSubpathEmitter(options: Record<string, Record<string, unknown>>) {
let emitContext: EmitContext | undefined;
const diagnostics = await Tester.files({
"node_modules/@org/fake-emitter/package.json": JSON.stringify({
name: "@org/fake-emitter",
exports: {
".": "./index.js",
"./typescript": "./typescript/index.js",
},
}),
"node_modules/@org/fake-emitter/index.js": mockFile.js({
$lib: createTypeSpecLibrary({ name: "@org/fake-emitter", diagnostics: {} }),
}),
"node_modules/@org/fake-emitter/typescript/index.js": mockFile.js({
$lib: subpathLib,
$onEmit: (ctx: EmitContext) => {
emitContext = ctx;
},
}),
}).diagnose("", {
compilerOptions: {
emit: ["@org/fake-emitter/typescript"],
options,
},
});
return [emitContext, diagnostics] as const;
}

it("resolves options keyed by the subpath specifier", async () => {
const [context, diagnostics] = await runSubpathEmitter({
"@org/fake-emitter/typescript": {
"emitter-output-dir": "/out",
"asset-dir": "/assets",
"max-files": 10,
},
});
expectDiagnosticEmpty(diagnostics);
ok(context, "Emit context should have been set.");
strictEqual(context.emitterOutputDir, "/out");
strictEqual(context.options["asset-dir"], "/assets");
strictEqual(context.options["max-files"], 10);
});

it("falls back to package.json name when the subpath key is missing", async () => {
const [context, diagnostics] = await runSubpathEmitter({
"@org/fake-emitter": {
"emitter-output-dir": "/from-pkg",
"asset-dir": "/pkg-assets",
},
});
expectDiagnosticEmpty(diagnostics);
ok(context, "Emit context should have been set.");
strictEqual(context.emitterOutputDir, "/from-pkg");
strictEqual(context.options["asset-dir"], "/pkg-assets");
});

it("targets the package-name options key when validating fallback options", async () => {
const [, diagnostics] = await runSubpathEmitter({
"@org/fake-emitter": {
"invalid-option": "abc",
},
});
expectDiagnostics(diagnostics, {
code: "invalid-schema",
message: [
"Schema violation: must NOT have additional properties (/)",
" additionalProperty: invalid-option",
].join("\n"),
});
const target = diagnostics[0]?.target;
if (target && typeof target === "object" && "path" in target) {
strictEqual((target as { path: string[] }).path.join("."), "options.@org/fake-emitter");
}
});
});

it("emit diagnostic if passing unknown option", async () => {
const diagnostics = await diagnoseEmitterOptions({
"invalid-option": "abc",
Expand Down