Skip to content

fix(hmr): preserve runtime-added properties of setup store refs - #3132

Merged
posva merged 1 commit into
vuejs:v4from
chatman-media:fix/hmr-preserve-setup-store-ref-properties
Jun 19, 2026
Merged

posva merged 1 commit into
vuejs:v4from
chatman-media:fix/hmr-preserve-setup-store-ref-properties

Conversation

@chatman-media

@chatman-media chatman-media commented Jun 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2611

Problem

In a setup store, state created with ref({ ... }) can gain new properties at runtime — e.g. an action that sets an optional field:

export const useCounterStore = defineStore('counter', () => {
  const counter = ref<{ nr: number; sign?: 'positive' | 'negative' }>({ nr: 0 })
  function increment() {
    counter.value.nr++
    counter.value.sign = counter.value.nr > 0 ? 'positive' : 'negative'
  }
  return { counter, increment }
})

After clicking increment, counter is { nr: 1, sign: 'positive' }. On HMR, the new module reinitializes counter to its declared value { nr: 0 } (no sign). acceptHMRUpdate then called patchObject(newValue, oldValue), which removes any key not present in the new value — so the runtime-added sign was dropped, even though nr was preserved.

Fix

patchObject's removal semantics are correct for option stores, whose entire state shape is declared upfront in state(): a key missing from the new definition was intentionally removed. But setup stores build their state imperatively (ref({}), reactive({})), and properties may be added at runtime that were never part of any declaration — diffing them against the freshly-initialized value wrongly removes them.

The one-line change gates patchObject behind isOptionsStore. For setup stores the old value is transferred as a whole (the else branch already does exactly this — "transfer the ref" — for primitive/non-plain-object refs), which is also consistent with how the function form of $patch and direct assignment behave.

Tests

it.todo('handles nested objects updates') in the setup-store block is replaced with two real tests:

  • keeps runtime-added properties of a ref object (#2611) — the exact reported scenario.
  • handles nested objects updates — a runtime-mutated nested value survives HMR even when the new definition drops a key.

Both fail before the change and pass after. The existing option-store tests (patches nested objects, skips arrays, skips nested arrays) are unchanged and still pass — removal behavior for option stores is preserved. Full suite: 204 passing.

Summary by CodeRabbit

  • Bug Fixes
    • Improved hot module reload (HMR) behavior to preserve store properties added at runtime and maintain nested object values during module updates.
  • Tests
    • Added coverage for HMR preservation of runtime-added properties and nested object changes.

In a setup store, state created with `ref({ ... })` can gain new
properties at runtime (e.g. an action setting an optional field). During
HMR the new module reinitializes the ref to its declared value, and
`patchObject` reconciled the old state against that fresh value, dropping
any key the new value did not declare.

This is correct for option stores, whose whole state shape is declared
upfront in `state()`, but wrong for setup stores where the shape is
imperative. Only run `patchObject` for option stores; for setup stores
transfer the old ref value as a whole, matching the behavior of the
function form of `$patch` and direct assignment.

Closes vuejs#2611
@coderabbitai

coderabbitai Bot commented Jun 13, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR fixes a Hot Module Replacement (HMR) bug in Pinia where setup stores lose runtime-added properties during module reloads. The implementation adds conditional state reconciliation logic, and new tests validate preservation of both runtime-added properties and nested object values.

Changes

Setup Store HMR State Preservation

Layer / File(s) Summary
HMR state reconciliation for setup stores
packages/pinia/src/store.ts
Updated _hotUpdate method to guard patchObject with isOptionsStore condition. Setup stores now transfer the previous state ref instead of patching when both old and new values are plain objects, with documentation explaining the behavioral difference.
Test cases for setup store HMR state preservation
packages/pinia/__tests__/hmr.spec.ts
Added two HMR simulation tests: one verifying runtime-added properties on ref-backed state objects persist after store redefinition, and another confirming nested object values (including runtime changes) are preserved when subsequent definitions omit keys.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • posva

Poem

🐰 Through hot updates swift and clean,
Setup stores now keep what's been seen—
Runtime properties won't disappear,
Nested objects hold what's dear.
With tests to prove each edge case true,
HMR magic works for you! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: fixing preservation of runtime-added properties on setup store refs during HMR, which is the core objective of the pull request.
Linked Issues check ✅ Passed The code changes directly address issue #2611 by gating patchObject behind isOptionsStore check, preserving runtime-added properties in setup stores while maintaining existing behavior for option stores.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the HMR regression: test cases verify the fix for setup stores and the store.ts modification implements the required logic change.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

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

@posva posva left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@posva posva changed the title fix: preserve runtime-added properties of setup store refs during HMR fix(hmr): preserve runtime-added properties of setup store refs Jun 19, 2026
@posva
posva merged commit aec2ac2 into vuejs:v4 Jun 19, 2026
1 check passed
@github-project-automation github-project-automation Bot moved this from 🆕 Triaging to ✅ Done in Pinia Roadmap Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

acceptHMRUpdate skips optional attributes without default values

2 participants