Skip to content

Commit 67809dc

Browse files
dmichon-msftCopilot
andcommitted
[api-extractor] Optimize the analysis type-check pass
Previously, `Collector.analyze` type-checked the entire program via `getSemanticDiagnostics()`, and `getGlobalVariableAnalyzer` forced a full-program check inside `getEmitResolver()`. Because API Extractor analyzes the compiler's .d.ts outputs with `skipLibCheck` disabled by default, this bind-and-checked every transitively reachable declaration file, including deep dependencies outside the API surface. This change: - Passes `skipDiagnostics: true` to `getEmitResolver()`, since only `hasGlobalName` is needed and the globals table is populated eagerly at checker creation. - Scopes `getSemanticDiagnostics()` to the source files reachable from the entry point (analyzed declarations plus intermediate re-export files) via the new `AstSymbolTable.collectAnalyzedSourceFiles()`. Binding, which the analysis relies on, still happens eagerly for all files, so the analysis itself is unchanged. Compiler errors are now only reported for declarations that contribute to the analyzed API. Verified on @rushstack/mcp-server: the api-extractor step dropped from ~4.8s to ~0.9s with byte-identical output. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8d872db commit 67809dc

4 files changed

Lines changed: 67 additions & 8 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,25 @@ export class AstSymbolTable {
129129
return this._exportAnalyzer.fetchAstModuleExportInfo(astModule);
130130
}
131131

132+
/**
133+
* Returns the set of source files that contain a declaration that was analyzed, i.e. that is
134+
* reachable from the entry point.
135+
*
136+
* @remarks
137+
* API Extractor analyzes the compiler's `.d.ts` outputs, and by default does not use
138+
* `skipLibCheck`, so type-checking the entire program would bind-and-check every transitively
139+
* reachable declaration file -- including deep dependencies that are not part of the API surface.
140+
* This set allows compiler diagnostics to be scoped to just the files that contribute
141+
* declarations to the analyzed API.
142+
*/
143+
public collectAnalyzedSourceFiles(): Set<ts.SourceFile> {
144+
const sourceFiles: Set<ts.SourceFile> = new Set<ts.SourceFile>();
145+
for (const declaration of this._astDeclarationsByDeclaration.keys()) {
146+
sourceFiles.add(declaration.getSourceFile());
147+
}
148+
return sourceFiles;
149+
}
150+
132151
/**
133152
* Attempts to retrieve an export by name from the specified `AstModule`.
134153
* Returns undefined if no match was found.

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,24 @@ export class TypeScriptInternals {
139139
if (!typeChecker.getEmitResolver) {
140140
throw new InternalError('Missing TypeChecker.getEmitResolver');
141141
}
142-
const resolver: any = typeChecker.getEmitResolver();
142+
143+
// We only use `EmitResolver.hasGlobalName`, which simply queries the type checker's `globals`
144+
// symbol table. That table (including ambient declarations, `jsGlobalAugmentations`, and
145+
// `declare global` augmentations) is populated synchronously when the checker is created, so it
146+
// does not require type checking to have run.
147+
//
148+
// By default, `getEmitResolver()` forces a full-program diagnostic type-check before returning
149+
// the resolver. Since API Extractor otherwise only type-checks the source files that are
150+
// reachable from the entry point (see `Collector.analyze`), that full check would dominate the
151+
// analysis cost. Passing `skipDiagnostics: true` avoids it while still returning a resolver
152+
// whose `hasGlobalName` behaves identically. The parameter is a compiler internal; older
153+
// TypeScript versions that lack it simply ignore the extra argument (falling back to the
154+
// previous, slower behavior).
155+
const resolver: any = typeChecker.getEmitResolver(
156+
/*sourceFile*/ undefined,
157+
/*cancellationToken*/ undefined,
158+
/*skipDiagnostics*/ true
159+
);
143160
if (!resolver.hasGlobalName) {
144161
throw new InternalError('Missing EmitResolver.hasGlobalName');
145162
}

apps/api-extractor/src/collector/Collector.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,6 @@ export class Collector {
200200
throw new Error('DtsRollupGenerator.analyze() was already called');
201201
}
202202

203-
// This runs a full type analysis, and then augments the Abstract Syntax Tree (i.e. declarations)
204-
// with semantic information (i.e. symbols). The "diagnostics" are a subset of the everyday
205-
// compile errors that would result from a full compilation.
206-
for (const diagnostic of this._program.getSemanticDiagnostics()) {
207-
this.messageRouter.addCompilerDiagnostic(diagnostic);
208-
}
209-
210203
const sourceFiles: readonly ts.SourceFile[] = this.program.getSourceFiles();
211204

212205
if (this.messageRouter.showDiagnostics) {
@@ -294,6 +287,25 @@ export class Collector {
294287
}
295288
}
296289

290+
// Report compiler diagnostics, but only for the source files that are reachable from the entry
291+
// point: the files that contribute an analyzed declaration, plus the intermediate re-export
292+
// files that were visited while resolving exports. API Extractor analyzes the compiler's `.d.ts`
293+
// outputs and (by default) does not enable `skipLibCheck`, so running `getSemanticDiagnostics()`
294+
// across the entire program would bind-and-check every transitively reachable declaration file,
295+
// including deep dependencies that are not part of the API surface. Scoping the diagnostics to
296+
// the analyzed files avoids that cost while still surfacing every error that pertains to the
297+
// exported API. (Binding, which the analysis relies on, happens eagerly for all files when the
298+
// type checker is created, so this does not affect the analysis itself.)
299+
const diagnosticSourceFiles: Set<ts.SourceFile> = this.astSymbolTable.collectAnalyzedSourceFiles();
300+
for (const sourceFile of nonExternalSourceFiles) {
301+
diagnosticSourceFiles.add(sourceFile);
302+
}
303+
for (const sourceFile of diagnosticSourceFiles) {
304+
for (const diagnostic of this._program.getSemanticDiagnostics(sourceFile)) {
305+
this.messageRouter.addCompilerDiagnostic(diagnostic);
306+
}
307+
}
308+
297309
// Here, we're collecting reference directives from all non-external source files
298310
// that were encountered while looking for exports, but only those references that
299311
// were explicitly written by the developer and marked with the `preserve="true"`
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Improve analysis performance by scoping compiler diagnostics to the source files that are reachable from the entry point, rather than type-checking the entire program. As a result, compiler errors are now only reported for declarations that contribute to the analyzed API surface.",
5+
"type": "minor",
6+
"packageName": "@microsoft/api-extractor"
7+
}
8+
],
9+
"packageName": "@microsoft/api-extractor",
10+
"email": "dmichon-msft@users.noreply.github.com"
11+
}

0 commit comments

Comments
 (0)