-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
fix(hmr): prevent update unmounting component during HMR reload #13815
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
Conversation
WalkthroughAdds a DISPOSED/job-flag guard to the HMR reload path in runtime-core so the queued parent update runs only if the instance’s scheduler job is not disposed; preserves isHmrUpdating toggling and dirtyInstances cleanup inside the guarded block. Adds a nested-component HMR test verifying reload behavior. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Dev as Dev Server
participant HMR as runtime-core HMR
participant Inst as Component Instance
participant Parent as Parent Instance
Dev->>HMR: file change -> reload(instance)
HMR->>HMR: queue update job
rect rgba(200,230,255,0.25)
note right of HMR: Guard added before parent update
HMR->>HMR: if (instance.jobFlags & DISPOSED) then skip
alt not disposed
HMR->>HMR: isHmrUpdating = true
HMR->>Parent: parent.update()
HMR->>HMR: dirtyInstances.delete(instance)
HMR->>HMR: isHmrUpdating = false
else disposed
HMR-->>HMR: skip parent.update()
end
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Assessment against linked issues
Out-of-scope changesNo out-of-scope functional changes detected relative to the objectives in the linked issue. Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. ✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
Size ReportBundles
Usages
|
@vue/compiler-core
@vue/compiler-dom
@vue/compiler-sfc
@vue/compiler-ssr
@vue/reactivity
@vue/runtime-core
@vue/runtime-dom
@vue/server-renderer
@vue/shared
vue
@vue/compat
commit: |
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/runtime-core/src/hmr.ts (1)
150-158
: Good guard; add try/finally and re-check parent at execution time to harden against throws/races.This fixes the stale queued update. Two low-risk tweaks improve robustness: ensure
isHmrUpdating
is always reset even ifupdate()
throws, and re-checkparent
at execution time in case it became null after queueing.- // vite-plugin-vue/issues/599 - // don't update if the job is already disposed - if (!(instance.job.flags! & SchedulerJobFlags.DISPOSED)) { - isHmrUpdating = true - instance.parent!.update() - isHmrUpdating = false - // #6930, #11248 avoid infinite recursion - dirtyInstances.delete(instance) - } + // vite-plugin-vue/issues/599 + // don't update if the job is already disposed + const notDisposed = + !((instance.job.flags ?? 0) & SchedulerJobFlags.DISPOSED) + // re-check at execution time; parent may have become null after queueing + const parent = instance.parent + if (parent && notDisposed) { + isHmrUpdating = true + try { + parent.update() + } finally { + isHmrUpdating = false + } + // #6930, #11248 avoid infinite recursion + dirtyInstances.delete(instance) + }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
packages/runtime-core/src/hmr.ts
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: Redirect rules
- GitHub Check: Header rules
- GitHub Check: Pages changed
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.
Actionable comments posted: 0
🧹 Nitpick comments (3)
packages/runtime-core/__tests__/hmr.spec.ts (3)
947-958
: Type the component definitions for better TS safetyAnnotate Inner/Outer as ComponentOptions to avoid implicit any and align with createRecord expectations.
Apply:
- let Inner = { + let Inner: ComponentOptions = { __hmrId: innerId, render() { return h('div', 'foo') }, } - let Outer = { + let Outer: ComponentOptions = { __hmrId: outerId, render() { return h(Inner) }, }
943-990
: Assert absence of the original error signals to prevent silent regressionsGuard against regressions by asserting no console noise matching the reported messages.
Apply:
test('reload nested components from shared dep update', async () => { + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) @@ await nextTick() expect(serializeInner(root)).toBe('<div>bar</div>') + expect(errorSpy.mock.calls.join(' ')).not.toMatch(/parent is null/i) + expect(warnSpy.mock.calls.join(' ')).not.toMatch(/scheduler flush/i) + errorSpy.mockRestore() + warnSpy.mockRestore() })
984-987
: Exercise both reload orderings for robustnessThe linked bug can depend on reload ordering. Consider a second test that triggers inner-first to ensure symmetry.
You can append:
test('reload nested components from shared dep update (inner-first)', async () => { const innerId = 'nested-reload-inner-2' const outerId = 'nested-reload-outer-2' let Inner: ComponentOptions = { __hmrId: innerId, render: () => h('div', 'foo') } let Outer: ComponentOptions = { __hmrId: outerId, render: () => h(Inner) } createRecord(innerId, Inner) createRecord(outerId, Outer) const root = nodeOps.createElement('div') render(h({ render: () => h(Outer) }), root) Inner = { __hmrId: innerId, render: () => h('div', 'bar') } Outer = { __hmrId: outerId, render: () => h(Inner) } // inner-first reload(innerId, Inner) reload(outerId, Outer) await nextTick() expect(serializeInner(root)).toBe('<div>bar</div>') })
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
packages/runtime-core/__tests__/hmr.spec.ts
(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/runtime-core/__tests__/hmr.spec.ts (1)
packages/runtime-test/src/serialize.ts (1)
serializeInner
(22-33)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: Redirect rules
- GitHub Check: Header rules
- GitHub Check: Pages changed
🔇 Additional comments (1)
packages/runtime-core/__tests__/hmr.spec.ts (1)
941-943
: Fix typo in HMR test comment and optionally refine test titleThe linked plugin issue describes importing and updating
service.js
, notserver.js
(github.com). The runtime HMR guard referencing this issue, theSchedulerJobFlags.DISPOSED
check, and thedirtyInstances.delete
cleanup are all present in packages/runtime-core/src/hmr.ts, so no further code changes are needed there.• Location: packages/runtime-core/tests/hmr.spec.ts (around line 942)
• Change comment to match the issue’s filename
• Optional: clarify the test title to reference a shared dependency updateSuggested diff:
- // Both Outer and Inner are reloaded when './server.js' changes + // Both Outer and Inner are reloaded when './service.js' changes - test('reload nested components from single update', async () => { + test('reload nested components from shared dep update', async () => {
close vitejs/vite-plugin-vue#599
Summary by CodeRabbit
New Features
Bug Fixes
Tests
Chores