-
Notifications
You must be signed in to change notification settings - Fork 701
[api-extractor] Optimize the analysis type-check pass #5891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -200,13 +200,6 @@ export class Collector { | |
| throw new Error('DtsRollupGenerator.analyze() was already called'); | ||
| } | ||
|
|
||
| // This runs a full type analysis, and then augments the Abstract Syntax Tree (i.e. declarations) | ||
| // with semantic information (i.e. symbols). The "diagnostics" are a subset of the everyday | ||
| // compile errors that would result from a full compilation. | ||
| for (const diagnostic of this._program.getSemanticDiagnostics()) { | ||
| this.messageRouter.addCompilerDiagnostic(diagnostic); | ||
| } | ||
|
|
||
| const sourceFiles: readonly ts.SourceFile[] = this.program.getSourceFiles(); | ||
|
|
||
| if (this.messageRouter.showDiagnostics) { | ||
|
|
@@ -294,6 +287,25 @@ export class Collector { | |
| } | ||
| } | ||
|
|
||
| // Report compiler diagnostics, but only for the source files that are reachable from the entry | ||
| // point: the files that contribute an analyzed declaration, plus the intermediate re-export | ||
| // files that were visited while resolving exports. API Extractor analyzes the compiler's `.d.ts` | ||
| // outputs and (by default) does not enable `skipLibCheck`, so running `getSemanticDiagnostics()` | ||
| // across the entire program would bind-and-check every transitively reachable declaration file, | ||
| // including deep dependencies that are not part of the API surface. Scoping the diagnostics to | ||
| // the analyzed files avoids that cost while still surfacing every error that pertains to the | ||
| // exported API. (Binding, which the analysis relies on, happens eagerly for all files when the | ||
| // type checker is created, so this does not affect the analysis itself.) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we tighten this a bit? It is repeating the exact same sentence from your other check above, reads like slop (trying to explain every exact nuance, no mental model of what the audience would reasonably already know or what questions they would care about) |
||
| const diagnosticSourceFiles: Set<ts.SourceFile> = this.astSymbolTable.collectAnalyzedSourceFiles(); | ||
| for (const sourceFile of nonExternalSourceFiles) { | ||
| diagnosticSourceFiles.add(sourceFile); | ||
| } | ||
| for (const sourceFile of diagnosticSourceFiles) { | ||
| for (const diagnostic of this._program.getSemanticDiagnostics(sourceFile)) { | ||
|
Comment on lines
+290
to
+304
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. API extractor only owns diagnostics relevant to files in the API report; files unrelated should be handled by the normal type checking pass. |
||
| this.messageRouter.addCompilerDiagnostic(diagnostic); | ||
| } | ||
| } | ||
|
|
||
| // Here, we're collecting reference directives from all non-external source files | ||
| // that were encountered while looking for exports, but only those references that | ||
| // were explicitly written by the developer and marked with the `preserve="true"` | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "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.", | ||
| "type": "minor", | ||
| "packageName": "@microsoft/api-extractor" | ||
| } | ||
| ], | ||
| "packageName": "@microsoft/api-extractor", | ||
| "email": "dmichon-msft@users.noreply.github.com" | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was there any validation that this optimization actually improved anything (e.g. runtime against a real repo)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've vetted about 3 seconds of saving on an internal project, and the @rushstack/mcp-server project in the rushstack repo was an immediate beneficiary, as it pulls in a lot of unrelated .d.ts files.