Skip to content
Source
Sign inSign up
TanStack/router

fix(start-plugin-core): drop parsed AST after import analysis to avoid dev memory leak #8682

Openlx3133584(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/8682/headopened

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

  1. In packages/start-plugin-core/src/import-protection/analysis.ts, once import sources, specifier positions, mock export names, and named exports are extracted during buildImportAnalysis, delete result.parsedAst and omit ast from the cached analysis object.
  2. In findUsagePosInAnalysis, lazily parse the AST via getOrParseAst(result) only when checking violation usage positions, and ensure result.parsedAst is cleared immediately after the traversal completes.
  3. Added a regression test in tests/importProtection/analysis.exports.test.ts verifying that parsedAst and analysis.ast are not retained.
  4. 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. 🦋 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
      NameType
      @tanstack/start-plugin-corePatch
      @tanstack/react-start-rscPatch
      @tanstack/react-startPatch
      @tanstack/solid-startPatch
      @tanstack/vue-startPatch

      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

    2. Review in Change Stack →

      Important

      Review skipped

      Review was skipped as selected files did not have any reviewable changes.

      Run configuration
      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_status to false in 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.md
      ImportAnalysis.ast is optional. Analysis construction removes result.parsedAst and 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.ts
      Usage lookup calls getOrParseAst and removes result.parsedAst after 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 review 7f7a5

      This change reduces development-server memory retention in import protection. No actionable merge-blocking risk was identified.

      Pre-merge checks: Passed 4
      Check nameStatusExplanation
      Linked Issues checkPassedIssue #8614 requires import analysis to avoid retaining one AST per analyzed module and to parse again only when a violation usage position is required. The diff makes ImportAnalysis.ast optional, r…
      Out of Scope Changes checkPassedThe 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 checkPassedThe title clearly summarizes the main change: dropping the parsed AST after import analysis to reduce development memory use.
      Description checkPassedThe 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 #8614 requires import analysis to avoid retaining one AST per analyzed module and to parse again only when a violation usage position is required. The diff makes ImportAnalysis.ast optional, removes result.parsedAst after buildImportAnalysis, reparses through getOrParseAst in findUsagePosInAnalysis, and clears result.parsedAst in a finally block. 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.

      Share
      ℹ️ 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.md
      • packages/start-plugin-core/src/import-protection/INTERNALS.md
      • packages/start-plugin-core/src/import-protection/analysis.ts
      • packages/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.

Sign in to join this conversation.