Skip to content

Commit 346e882

Browse files
committed
[api-extractor] Surface bundledPackages no-op when packageId is missing (#5730)
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 #5730.
1 parent e625a0f commit 346e882

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

apps/api-extractor/src/analyzer/ExportAnalyzer.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,28 @@ export class ExportAnalyzer {
300300
return false;
301301
}
302302

303+
// If the user configured `bundledPackages` but TypeScript could not
304+
// produce a `packageId` for this module, the bundled-package lookup is
305+
// guaranteed to miss it and the module will be silently treated as
306+
// external (issue #5730). The most common cause is a local package
307+
// whose `package.json` lacks a `"version"` field — TypeScript does not
308+
// assign a `packageId` without one (microsoft/TypeScript#63307). Surface
309+
// the cause with a remediable error rather than producing a confusing
310+
// bundling result.
311+
if (packageName === undefined && this._bundledPackageNames.size > 0) {
312+
throw new InternalError(
313+
`Cannot determine the package name for the module ${JSON.stringify(
314+
moduleSpecifier
315+
)}, but "bundledPackages" is configured. ` +
316+
`This is usually because the resolved package's package.json ` +
317+
`is missing a "version" field — TypeScript does not assign a ` +
318+
`packageId without one (microsoft/TypeScript#63307), so the ` +
319+
`bundledPackages lookup cannot match it. Add a "version" field ` +
320+
`(any value works) to the package's package.json and re-run.\n` +
321+
SourceFileLocationFormatter.formatDeclaration(importOrExportDeclaration)
322+
);
323+
}
324+
303325
if (resolvedModule.isExternalLibraryImport === undefined) {
304326
// This presumably means the compiler couldn't figure out whether the module was external, but we're not
305327
// sure how this can happen.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/api-extractor",
5+
"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).",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@microsoft/api-extractor"
10+
}

0 commit comments

Comments
 (0)