fix(start-plugin-core): drop parsed AST after import analysis to avoid dev memory leak #8682
main fromrefs/pull/8682/head·opened Summary#
Fixes #8614.
During development, ImportAnalysis previously retained the full parsed AST on both result.analysis.ast and result.parsedAst for every analyzed module. Because TransformResult is cached per module in dev server environments, this led to substantial live heap accumulation (hundreds of megabytes in large apps).
Solution#
- In
packages/start-plugin-core/src/import-protection/analysis.ts, once import sources, specifier positions, mock export names, and named exports are extracted duringbuildImportAnalysis, deleteresult.parsedAstand omitastfrom the cachedanalysisobject. - In
findUsagePosInAnalysis, lazily parse the AST viagetOrParseAst(result)only when checking violation usage positions, and ensureresult.parsedAstis cleared immediately after the traversal completes. - Added a regression test in
tests/importProtection/analysis.exports.test.tsverifying thatparsedAstandanalysis.astare not retained. - Added changeset for
@tanstack/start-plugin-core.
Summary by CodeRabbit#
- Performance
- Import analysis now uses less retained memory during development by releasing parsed data after analysis. Source-location lookups continue to work as expected.
- 1
Important
Review skipped#
Review was skipped as selected files did not have any reviewable changes.
Run configuration-
- Source mode: Auto
- Repository: TanStack/router
- Branch: main
- File: .coderabbit.yaml
- Commit: c5320d7
- Layers: Repository YAML
- Review profile: CHILL
- Plan: Advanced
- Run ID:
7271357d-c4e0-4447-b518-e18dee26db36
Commits-
Reviewing files that changed from the base of the PR and between 7f7a513c8dffe8ddd0ee825ade3c440ed3026ce9 and 504d35645df67bce9b8c06a0557627ca231a4121.
You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
- Trigger review
No actionable comments were generated in the recent review.
Walkthrough
-
Import analysis no longer retains parsed module ASTs in its cached analysis. Usage lookup obtains an AST when needed and removes it after traversal. A test checks the retained AST state and the reported usage line.
Changes: Import analysis memory handling
Layer / File(s) Summary Drop ASTs from cached analysis
packages/start-plugin-core/src/import-protection/analysis.ts,packages/start-plugin-core/src/import-protection/INTERNALS.md,.changeset/import-protection-drop-ast.mdImportAnalysis.astis optional. Analysis construction removesresult.parsedAstand does not store the AST in the analysis. The internal documentation and changeset describe the change.Parse AST for usage lookup
packages/start-plugin-core/src/import-protection/analysis.ts,packages/start-plugin-core/tests/importProtection/analysis.exports.test.tsUsage lookup calls getOrParseAstand removesresult.parsedAstafter traversal, including when traversal throws. The test checks that analysis and usage lookup leave no AST retained and that lookup returns line 2.Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix · Severity of issue fixed: Medium
Merge Risk:
Minimal · previous review7f7a5-
This change reduces development-server memory retention in import protection. No actionable merge-blocking risk was identified.
Pre-merge checks:
4-
Check name Status Explanation Linked Issues check Issue #8614requires import analysis to avoid retaining one AST per analyzed module and to parse again only when a violation usage position is required. The diff makesImportAnalysis.astoptional, r…Out of Scope Changes check The changes stay within issue #8614. The source change implements AST lifetime management, the test covers the memory behavior and usage lookup, the documentation records the optional AST, and the cha…Title check The title clearly summarizes the main change: dropping the parsed AST after import analysis to reduce development memory use. Description check The description explains the problem, solution, regression test, and changeset. It omits the template’s checklist and explicit release-impact section, but it is otherwise mostly complete. Full details: Linked Issues check
-
Explanation
Issue
#8614requires import analysis to avoid retaining one AST per analyzed module and to parse again only when a violation usage position is required. The diff makesImportAnalysis.astoptional, removesresult.parsedAstafterbuildImportAnalysis, reparses throughgetOrParseAstinfindUsagePosInAnalysis, and clearsresult.parsedAstin afinallyblock. The regression test verifies that both AST references are absent after analysis and that usage lookup returns the correct line and clears the parsed AST.
Full details: Out of Scope Changes check
-
Explanation
The changes stay within issue
#8614. The source change implements AST lifetime management, the test covers the memory behavior and usage lookup, the documentation records the optional AST, and the changeset documents the package change. No unrelated product behavior is changed.
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
ℹ️ Recent review info
-
⚙️ Run configuration
-
- Configuration used: Repository: TanStack/router/.coderabbit.yaml
- Review profile: CHILL
- Plan: Advanced
- Run ID:
65ccef70-40fd-45e3-8f98-1e7771a9b600
📥 Commits
-
Reviewing files that changed from the base of the PR and between 57bd1a92f9839af5bf773c3b36d039569fb1a41e and 7f7a513c8dffe8ddd0ee825ade3c440ed3026ce9.
📒 Files selected for processing (4)
-
.changeset/import-protection-drop-ast.mdpackages/start-plugin-core/src/import-protection/INTERNALS.mdpackages/start-plugin-core/src/import-protection/analysis.tspackages/start-plugin-core/tests/importProtection/analysis.exports.test.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
-
-
lx3133584(GitHub profile, opens in a new tab) linked this to fix #8614
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 504d35645df67bce9b8c06a0557627ca231a4121
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 5 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR