[api-extractor] Reuse TypeScript's module-resolution results to avoid realpath/lstat storm when resolving external package names
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 52/100
- issue の種類
- リファクタリング
- 明瞭さ
- おおむね明確
- 活発さ
- 静か
- 技術スタック
- node.js, typescript
- 領域
- performance, tooling
調査の方向性
Start at DeclarationReferenceGenerator._getPackageName, then inspect Collector, AstSymbolTable, and TypeScriptInternals.ts for program-resolution access. Include module and type-reference resolutions, verify packageId and resolvedFileName matching, and run the acceptance-test projects to confirm byte-identical API reports while preserving the filesystem fallback.
索引モデルが issue の本文から書いたものです。
説明
Summary
During analysis, DeclarationReferenceGenerator._getPackageName(sourceFile) calls PackageJsonLookup.tryLoadNodePackageJsonFor(sourceFile.fileName) once per external source file. That path (PackageJsonLookup._tryLoadNodePackageJsonInner) calls FileSystem.getRealPath() (Node fs.realpathSync → an lstat per path segment, following symlinks) before the content cache can help, and the upward folder walk in _tryGetPackageFolderFor does it at each directory level. In pnpm workspaces (a node_modules symlink farm) this produces a large lstat storm and shows up as a significant chunk of api-extractor CPU time in profiles.
This was noticed while profiling the type-check optimization in https://github.com/microsoft/rushstack/pull/5891 — after removing the whole-program semantic check, realpathSync→lstat inside FileSystem.getRealPath → tryLoadNodePackageJsonFor was the next-largest chunk.
Insight
TypeScript already resolved every one of these modules during program construction and recorded the answer. Each ResolvedModuleFull carries:
resolvedFileName— the already-realpath'd target (TS paid thelstatcost once, cached), andpackageId.name— the bare package name, which is exactly what_getPackageNamereconstructs via the filesystem walk (packageId.subModuleNameholds the sub-path separately).
Proof of concept
Probing the @rushstack/mcp-server program via the (internal) program.forEachResolvedModule(cb):
resolutions: total=1111, withPackageId=1103, uniqueFiles=258
@modelcontextprotocol/sdk <= @modelcontextprotocol/sdk/dist/esm/server/mcp.d.ts
zod <= zod/index.d.cts
...
99.3% of resolutions carry packageId, and packageId.name came back as the bare package name even for non-index files.
Proposed design
- Build a
Map<resolvedFileName, packageId.name>once (e.g. offCollector/AstSymbolTable) via the internalprogram.forEachResolvedModule(...). Type-reference-directive resolutions (forEachResolvedTypeReferenceDirective) also carrypackageIdand should be included. - In
_getPackageName, consult the map first (keyed bysourceFile.fileName) — zero filesystem access for the common case. - Fall back to
tryLoadNodePackageJsonForon a miss (the ~0.7% withoutpackageId, or files reached via other means), preserving today's exact behavior.
forEachResolvedModule / getResolvedModule are TypeScript internals (not in the public .d.ts), but api-extractor already accesses TS internals via TypeScriptInternals.ts, so this is consistent with existing patterns.
Caveats to validate
packageId.namesemantics — the TS doc comment hints it may include a subpath in some cases; the PoC showed bare names, but this needs verification against@typespackages and deep imports vs. the existingpackageJson.namebehavior.- Key matching —
resolvedFileNamemust equal the program'ssourceFile.fileName(generally true;preserveSymlinksis the edge case). - Correctness — must produce byte-identical API reports across all acceptance-test projects. The fast-path-with-fallback design keeps this low-risk.
Related
- Follow-up to #5891 (type-check pass optimization).
- Also worth checking
SourceMapper.getRealPathusage for the same treatment.
- 主要言語
- TypeScript
- スター
- 6.5k
- フォーク
- 708
- 平均マージ
- 5日 19時間
- マージ済み PR(30日)
- 48
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
microsoft/rushstack のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
microsoft/rushstack の issue をすべて見る
似ている issue
-
VerificationGate: ATTRIBUTION quote guard never matches a normal quotation (\b around the quote) オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
danielmiessler/LifeOS#2234 ·
-
T: Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
-
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
-
Mend: dependency security vulnerability untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100