[api-extractor] Fix self-package import resolution in TypeScript compiler #5301
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes an issue where API Extractor incorrectly resolves self-package imports to source
.ts
files instead of the intended.d.ts
build artifacts.Problem
When a
.d.ts
file imports the package itself by its name, TypeScript's module resolution redirects to source files instead of analyzing the build outputs. This occurs because TypeScript tries to avoid analyzing build outputs whenoutDir
ordeclarationDir
compiler options are set.See the TypeScript compiler logic here: https://github.com/microsoft/TypeScript/blob/391616532d6827254a86dc266121fd5d1958ffb9/src/compiler/moduleNameResolver.ts#L2867
Solution
Remove
outDir
anddeclarationDir
from the compiler options inCompilerState.ts
. This prevents TypeScript from redirecting imports and ensures API Extractor analyzes the correct.d.ts
files. Since API Extractor only uses the compiler for analysis and doesn't emit any files, these options are unnecessary.Test Plan
rushx build
.d.ts
filesAlloy issue
I found this issue by trying to build Alloy on a Windows machine. Strangely, this self-import issue causes cyclic dependency warnings only on Windows.
When you run
pnpm exec api-extractor run -v --diagnostics
inpackages/core
of the repository (regardless of the OS), you can see that the compiler is analyzing src directory.After the fix, this changes:
And the Windows issue is also gone.
api.json impact
The members with self-dynamic-import are affected. Others are unaffected.
Before:
After:
As you can see,
!~
is changed to!
.I don't completely understand what this change is, and which one is better. It seems
!~
means local and!
means exported?Then!
looks fine. The markdown generation was the same (both did not generate the link toChildren
).Generated Markdown
Home > @alloy-js/core > MemberName
MemberName() function
Signature:
Returns:
import("@alloy-js/core/jsx-runtime").Children