Skip to content

fix(compiler-sfc): handle transformed template AST after cache invalidation - #15136

Open
edison1105 wants to merge 1 commit into
mainfrom
edison/fix-sfc-transformed-template-analysis
Open

fix(compiler-sfc): handle transformed template AST after cache invalidation#15136
edison1105 wants to merge 1 commit into
mainfrom
edison/fix-sfc-transformed-template-analysis

Conversation

@edison1105

@edison1105 edison1105 commented Jul 21, 2026

Copy link
Copy Markdown
Member

Template usage analysis normally reuses its cached result. Once that cache entry is evicted, a later compileScript() call recomputes the result from the descriptor's template AST.

If compileTemplate() has already transformed that AST, walking it again can miss template-only imports and v-model identifiers, notably during a later SSR pass.

Reparse transformed template ASTs before analysis, preserving the configured compiler, compiler options, SSR mode, and SFC-relative source locations.

Closes #15126
Closes #15128

Summary by CodeRabbit

  • Bug Fixes

    • Improved template analysis when custom compiler options, delimiters, or SSR settings are used.
    • Corrected import-usage and v-model detection based on configured template options.
    • Ensured templates are correctly re-analyzed after the analysis cache is cleared.
  • Tests

    • Added coverage verifying template analysis cache invalidation and regenerated script accessors.

…dation

Template usage analysis normally reuses its cached result. Once that cache
entry is evicted, a later compileScript() call recomputes the result from the
descriptor's template AST.

If compileTemplate() has already transformed that AST, walking it again can
miss template-only imports and v-model identifiers, notably during a later SSR
pass.

Reparse transformed template ASTs before analysis, preserving the configured
compiler, compiler options, SSR mode, and SFC-relative source locations.

Closes #15126
Closes #15128
@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Changes

Template analysis flow

Layer / File(s) Summary
Resolve transformed template ASTs
packages/compiler-sfc/src/template/resolveTemplateAST.ts, packages/compiler-sfc/src/compileTemplate.ts
Adds shared transformed-AST resolution and uses it from template compilation.
Template usage analysis and caching
packages/compiler-sfc/src/script/importUsageCheck.ts
Passes template options through import and v-model analysis, resolves transformed ASTs, and exports the typed cache.
CompileScript integration and regression coverage
packages/compiler-sfc/src/compileScript.ts, packages/compiler-sfc/__tests__/compileScript.spec.ts
Forwards template options from compileScript and tests re-analysis after clearing the cache.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant compileScript
  participant importUsageCheck
  participant resolveTemplateAST
  participant templateAnalysisCache
  compileScript->>importUsageCheck: analyze template with template options
  importUsageCheck->>templateAnalysisCache: read or write analysis result
  importUsageCheck->>resolveTemplateAST: resolve transformed AST
  resolveTemplateAST-->>importUsageCheck: rooted template AST
  importUsageCheck-->>compileScript: used imports and v-model identifiers
Loading

Possibly related PRs

  • vuejs/core#14214: Updates template usage and v-model analysis to account for template options and resolved ASTs.
  • vuejs/core#15128: Covers transformed-template analysis to prevent template-only imports from being dropped.

Suggested labels: scope: sfc, :hammer: p3-minor-bug

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: handling transformed template ASTs after cache invalidation.
Linked Issues check ✅ Passed The changes address the linked issues by reparsing transformed ASTs, preserving template imports and v-model targets, and adding cache-eviction coverage.
Out of Scope Changes check ✅ Passed The diff stays focused on compiler-sfc template-analysis behavior and test coverage, with no clear unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch edison/fix-sfc-transformed-template-analysis

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.


Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Jul 21, 2026

Copy link
Copy Markdown

Open in StackBlitz

@vue/compiler-core

pnpm add https://pkg.pr.new/@vue/compiler-core@15136
npm i https://pkg.pr.new/@vue/compiler-core@15136
yarn add https://pkg.pr.new/@vue/compiler-core@15136.tgz

@vue/compiler-dom

pnpm add https://pkg.pr.new/@vue/compiler-dom@15136
npm i https://pkg.pr.new/@vue/compiler-dom@15136
yarn add https://pkg.pr.new/@vue/compiler-dom@15136.tgz

@vue/compiler-sfc

pnpm add https://pkg.pr.new/@vue/compiler-sfc@15136
npm i https://pkg.pr.new/@vue/compiler-sfc@15136
yarn add https://pkg.pr.new/@vue/compiler-sfc@15136.tgz

@vue/compiler-ssr

pnpm add https://pkg.pr.new/@vue/compiler-ssr@15136
npm i https://pkg.pr.new/@vue/compiler-ssr@15136
yarn add https://pkg.pr.new/@vue/compiler-ssr@15136.tgz

@vue/reactivity

pnpm add https://pkg.pr.new/@vue/reactivity@15136
npm i https://pkg.pr.new/@vue/reactivity@15136
yarn add https://pkg.pr.new/@vue/reactivity@15136.tgz

@vue/runtime-core

pnpm add https://pkg.pr.new/@vue/runtime-core@15136
npm i https://pkg.pr.new/@vue/runtime-core@15136
yarn add https://pkg.pr.new/@vue/runtime-core@15136.tgz

@vue/runtime-dom

pnpm add https://pkg.pr.new/@vue/runtime-dom@15136
npm i https://pkg.pr.new/@vue/runtime-dom@15136
yarn add https://pkg.pr.new/@vue/runtime-dom@15136.tgz

@vue/server-renderer

pnpm add https://pkg.pr.new/@vue/server-renderer@15136
npm i https://pkg.pr.new/@vue/server-renderer@15136
yarn add https://pkg.pr.new/@vue/server-renderer@15136.tgz

@vue/shared

pnpm add https://pkg.pr.new/@vue/shared@15136
npm i https://pkg.pr.new/@vue/shared@15136
yarn add https://pkg.pr.new/@vue/shared@15136.tgz

vue

pnpm add https://pkg.pr.new/vue@15136
npm i https://pkg.pr.new/vue@15136
yarn add https://pkg.pr.new/vue@15136.tgz

@vue/compat

pnpm add https://pkg.pr.new/@vue/compat@15136
npm i https://pkg.pr.new/@vue/compat@15136
yarn add https://pkg.pr.new/@vue/compat@15136.tgz

commit: 97e05bd

@edison1105 edison1105 added ready to merge The PR is ready to be merged. scope: sfc 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. labels Jul 21, 2026
@github-actions

Copy link
Copy Markdown

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 107 kB 40.5 kB 36.3 kB
vue.global.prod.js 166 kB 60.6 kB 53.8 kB

Usages

Name Size Gzip Brotli
createApp (CAPI only) 49.2 kB 19.1 kB 17.5 kB
createApp 57.3 kB 22.2 kB 20.3 kB
createSSRApp 61.9 kB 24.1 kB 21.9 kB
defineCustomElement 63.5 kB 24.1 kB 22 kB
overall 71.9 kB 27.5 kB 25 kB

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/compiler-sfc/src/script/importUsageCheck.ts (1)

59-86: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Key cached analysis by its parsing context.

At Line 68, the cache uses only content, although the new result depends on compiler, compilerOptions, and ssr. A transformed shared descriptor analyzed under one delimiter/compiler setting can return stale usedIds or vModelIds during a later pass with different options, again omitting returned imports or v-model demotion. Include parse context in cache validation/keying, or bypass this content-only cache for transformed ASTs.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/compiler-sfc/src/script/importUsageCheck.ts` around lines 59 - 86,
Update resolveTemplateAnalysisResult so templateAnalysisCache entries are keyed
and validated by the parsing context, including options.compiler,
options.compilerOptions, and options.ssr, in addition to template content.
Ensure analyses from transformed descriptors or differing compiler settings
cannot reuse stale usedIds or vModelIds; alternatively bypass the cache for
transformed ASTs while preserving reuse for compatible contexts.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@packages/compiler-sfc/src/script/importUsageCheck.ts`:
- Around line 59-86: Update resolveTemplateAnalysisResult so
templateAnalysisCache entries are keyed and validated by the parsing context,
including options.compiler, options.compilerOptions, and options.ssr, in
addition to template content. Ensure analyses from transformed descriptors or
differing compiler settings cannot reuse stale usedIds or vModelIds;
alternatively bypass the cache for transformed ASTs while preserving reuse for
compatible contexts.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 52fb116e-ea9c-4a00-a0ce-22dde73dfb96

📥 Commits

Reviewing files that changed from the base of the PR and between fa2885d and 97e05bd.

📒 Files selected for processing (5)
  • packages/compiler-sfc/__tests__/compileScript.spec.ts
  • packages/compiler-sfc/src/compileScript.ts
  • packages/compiler-sfc/src/compileTemplate.ts
  • packages/compiler-sfc/src/script/importUsageCheck.ts
  • packages/compiler-sfc/src/template/resolveTemplateAST.ts

@edison1105

Copy link
Copy Markdown
Member Author

/ecosystem-ci run

@vue-bot

vue-bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

📝 Ran ecosystem CI: Open

suite result latest scheduled
router success success
vite-plugin-vue success success
pinia success success
quasar success success
vant success success
language-tools success success
radix-vue success success
vitepress success success
nuxt failure success
primevue success success
vue-i18n success success
vue-macros success success
test-utils success success
vueuse success success
vue-simple-compiler success success
vuetify success success

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. ready to merge The PR is ready to be merged. scope: sfc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

compileScript drops template imports from __returned__ when analysis re-runs on a transformed template AST

2 participants