From cdfcded0cbeace0c83b5ccdaabce92acefcb1626 Mon Sep 17 00:00:00 2001 From: MLuc24 Date: Mon, 17 Aug 2026 11:39:31 +0700 Subject: [PATCH] [api-extractor] Fix ExtractorConfig failing when bundled into ESM output ExtractorConfig loaded its default config at module initialization from a __dirname-relative path. __dirname does not exist in ESM, and the replacement bundlers inject resolves to the bundle's own output folder, so the schema file was never found. The defaults are now imported statically, the same way the sibling api-extractor.schema.json already is, which lets bundlers inline them and removes the runtime filesystem read. The four comments in the defaults file were dropped so that it parses as strict JSON; the note they carried is now stated where the import is consumed. --- apps/api-extractor/src/api/ExtractorConfig.ts | 7 ++++--- .../src/schemas/api-extractor-defaults.json | 5 ----- ...api-extractor-esm-bundling_2026-08-17-04-39-03.json | 10 ++++++++++ 3 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 common/changes/@microsoft/api-extractor/fix-api-extractor-esm-bundling_2026-08-17-04-39-03.json diff --git a/apps/api-extractor/src/api/ExtractorConfig.ts b/apps/api-extractor/src/api/ExtractorConfig.ts index f3e33f46825..080902d37fc 100644 --- a/apps/api-extractor/src/api/ExtractorConfig.ts +++ b/apps/api-extractor/src/api/ExtractorConfig.ts @@ -33,6 +33,7 @@ import { PackageMetadataManager } from '../analyzer/PackageMetadataManager'; import { MessageRouter } from '../collector/MessageRouter'; import type { IApiModelGenerationOptions } from '../generators/ApiModelGenerator'; import apiExtractorSchema from '../schemas/api-extractor.schema.json'; +import apiExtractorDefaults from '../schemas/api-extractor-defaults.json'; /** * Tokens used during variable expansion of path fields from api-extractor.json. @@ -228,9 +229,9 @@ interface IExtractorConfigParameters { enumMemberOrder: EnumMemberOrder; } -const _defaultConfig: Partial = JsonFile.load( - path.join(__dirname, '../schemas/api-extractor-defaults.json') -); +// The defaults file deliberately omits fields that are required once a section is used +// (for example "apiReport.enabled"), so it does not structurally match IConfigFile. +const _defaultConfig: Partial = apiExtractorDefaults as unknown as Partial; /** * Match all three flavors for type declaration files (.d.ts, .d.mts, .d.cts) diff --git a/apps/api-extractor/src/schemas/api-extractor-defaults.json b/apps/api-extractor/src/schemas/api-extractor-defaults.json index bb6b6157aaf..a5401209c49 100644 --- a/apps/api-extractor/src/schemas/api-extractor-defaults.json +++ b/apps/api-extractor/src/schemas/api-extractor-defaults.json @@ -1,8 +1,6 @@ { "projectFolder": "", - // ("mainEntryPointFilePath" is required) - "bundledPackages": [], "newlineKind": "crlf", @@ -15,7 +13,6 @@ }, "apiReport": { - // ("enabled" is required) "reportFileName": ".api.md", "reportFolder": "/etc/", "reportTempFolder": "/temp/", @@ -23,13 +20,11 @@ }, "docModel": { - // ("enabled" is required) "apiJsonFilePath": "/temp/.api.json", "includeForgottenExports": false }, "dtsRollup": { - // ("enabled" is required) "untrimmedFilePath": "/dist/.d.ts", "alphaTrimmedFilePath": "", "betaTrimmedFilePath": "", diff --git a/common/changes/@microsoft/api-extractor/fix-api-extractor-esm-bundling_2026-08-17-04-39-03.json b/common/changes/@microsoft/api-extractor/fix-api-extractor-esm-bundling_2026-08-17-04-39-03.json new file mode 100644 index 00000000000..98066ac42ca --- /dev/null +++ b/common/changes/@microsoft/api-extractor/fix-api-extractor-esm-bundling_2026-08-17-04-39-03.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "comment": "Fix an error that occurred when `@microsoft/api-extractor` was bundled into ESM output, caused by loading the default config from a `__dirname`-relative path at module initialization.", + "type": "patch", + "packageName": "@microsoft/api-extractor" + } + ], + "packageName": "@microsoft/api-extractor" +}