From 050804001d356266d13f56829ed1c3a0f173ddbc Mon Sep 17 00:00:00 2001 From: Shaurya Singh Date: Wed, 27 May 2026 11:53:17 -0700 Subject: [PATCH] [api-extractor] Surface bundledPackages no-op when packageId is missing (#5730) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When `bundledPackages` is configured but TypeScript could not assign a `packageId` to a resolved module, the lookup in `_isExternalModulePath` silently failed via optional chaining and the module was bundled as external. The most common cause is a local workspace package whose `package.json` lacks a `"version"` field — TypeScript does not assign a `packageId` without one (microsoft/TypeScript#63307). Throw an `InternalError` with a remediation message in that exact combination (`packageId` undefined AND `_bundledPackageNames.size > 0`) so users see the cause instead of staring at an unexpectedly-not-bundled `from "foo"` in their `.d.ts` rollup. The non-bundled case (`_bundledPackageNames.size === 0`) is unchanged — the missing `packageId` flows through to the existing `isExternalLibraryImport` check that already handles ambiguous modules. Matches the fix suggested by the issue reporter in microsoft/rushstack#5730. --- .../src/analyzer/ExportAnalyzer.ts | 22 +++++++++++++++++++ ...ages-missing-version_2026-05-27-19-00.json | 10 +++++++++ 2 files changed, 32 insertions(+) create mode 100644 common/changes/@microsoft/api-extractor/fix-bundled-packages-missing-version_2026-05-27-19-00.json diff --git a/apps/api-extractor/src/analyzer/ExportAnalyzer.ts b/apps/api-extractor/src/analyzer/ExportAnalyzer.ts index 0bc6236aa4d..a8c8326b134 100644 --- a/apps/api-extractor/src/analyzer/ExportAnalyzer.ts +++ b/apps/api-extractor/src/analyzer/ExportAnalyzer.ts @@ -309,6 +309,28 @@ export class ExportAnalyzer { ); } + // If the user configured `bundledPackages` but TypeScript could not produce a `packageId` for this + // *external* module, the bundled-package lookup above is guaranteed to miss it and the module will be + // silently treated as external (issue #5730). The most common cause is a package whose `package.json` + // lacks a `"version"` field, since TypeScript does not assign a `packageId` without one + // (microsoft/TypeScript#63307). Surface the cause with a remediable error rather than producing a + // confusing bundling result. Internal (relative) imports legitimately have no `packageId`, so this + // check is scoped to external library imports only. + if ( + resolvedModule.isExternalLibraryImport && + packageName === undefined && + this._bundledPackageNames.size > 0 + ) { + throw new InternalError( + `Cannot determine the package name for the external module ${JSON.stringify(moduleSpecifier)}, ` + + `but "bundledPackages" is configured. This is usually because the resolved package's ` + + `package.json is missing a "version" field, since TypeScript does not assign a packageId ` + + `without one (microsoft/TypeScript#63307), so the bundledPackages lookup cannot match it. ` + + `Add a "version" field (any value works) to the package's package.json and re-run.\n` + + SourceFileLocationFormatter.formatDeclaration(importOrExportDeclaration) + ); + } + return resolvedModule.isExternalLibraryImport; } diff --git a/common/changes/@microsoft/api-extractor/fix-bundled-packages-missing-version_2026-05-27-19-00.json b/common/changes/@microsoft/api-extractor/fix-bundled-packages-missing-version_2026-05-27-19-00.json new file mode 100644 index 00000000000..44d84268fae --- /dev/null +++ b/common/changes/@microsoft/api-extractor/fix-bundled-packages-missing-version_2026-05-27-19-00.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@microsoft/api-extractor", + "comment": "Throw an actionable InternalError instead of silently failing to bundle when bundledPackages is configured but TypeScript could not assign a packageId to the resolved module (most often because the package's package.json is missing a \"version\" field).", + "type": "patch" + } + ], + "packageName": "@microsoft/api-extractor" +} \ No newline at end of file