Skip to content

Conversation

edison1105
Copy link
Member

@edison1105 edison1105 commented Sep 18, 2025

close #13903

Summary by CodeRabbit

  • Bug Fixes
    • Nested objects obtained via refs inside readonly structures are now correctly treated as read-only, preventing unintended mutations while preserving prior array-index behavior for refs.
  • Tests
    • Added a test verifying that refs nested within readonly containers are recognized as readonly.

Copy link

coderabbitai bot commented Sep 18, 2025

Walkthrough

Adjusts BaseReactiveHandler.get to unwrap refs differently on readonly proxies: when accessing a ref, derive the value (preserving array-index return of the Ref). If the proxy is readonly and the derived value is an object, return a readonly-wrapped version; otherwise return the value. Adds a test asserting nested ref inside readonly is considered readonly.

Changes

Cohort / File(s) Summary
Readonly ref unwrapping in getters
packages/reactivity/src/baseHandlers.ts
In get(), the isRef(res) branch now computes a local value (preserving array-index behavior returning the Ref for integer keys) and returns readonly(value) when the proxy is readonly and value is an object; otherwise returns value.
Test: nested readonly ref
packages/reactivity/__tests__/readonly.spec.ts
Adds test should keep nested ref readonly which wraps a ref([]) inside a readonly({ items }) and asserts isReadonly(obj.o.items) is true.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor U as User Code
  participant P as Readonly Proxy
  participant H as BaseReactiveHandler.get
  participant R as Ref
  participant RO as readonly()

  U->>P: access target[key]
  P->>H: get(target, key, receiver)
  alt resolved value is Ref
    H->>R: inspect ref (res)
    alt target is array AND key is integer
      note right of H #E8F0FF: preserve array-index behavior
      H-->>P: return Ref (res)
    else
      H->>R: const value = res.value
      alt proxy is readonly AND value is object
        H->>RO: readonly(value)
        RO-->>H: readonly-wrapped object
        H-->>P: return readonly-wrapped object
      else
        H-->>P: return value
      end
    end
  else
    H-->>P: existing get behavior (unchanged)
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested labels

scope: reactivity, :hammer: p3-minor-bug

Poem

A hop, a stop, a guarded flow—
My paws wrap refs where readonlys go.
Arrays keep refs at index true,
But objects don a frosty hue.
No sneaky push shall slip the net—
Immutable burrow, safe as it gets. 🥕🐇

Pre-merge checks and finishing touches

✅ 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 "fix(reactivity): respect readonly during ref unwrapping" is concise, follows conventional commit style, and accurately summarizes the primary change (ensuring readonly behavior when unwrapping refs in the reactivity package). It clearly identifies the intent and affected subsystem without extraneous detail.
Linked Issues Check ✅ Passed The changes modify BaseReactiveHandler.get to wrap unwrapped ref object values in readonly when the containing proxy is readonly and add a test ("should keep nested ref readonly") that asserts a nested ref inside readonly remains readonly; this directly addresses the behavior reported in issue #13903 where readonly({ myArray: ref([]) }) allowed mutation via push. The implementation also preserves previous array-index behavior for integer keys, and no API signatures were changed, so the code changes align with and satisfy the linked issue's coding objective.
Out of Scope Changes Check ✅ Passed The diff only touches packages/reactivity/src/baseHandlers.ts and adds a test in packages/reactivity/tests/readonly.spec.ts, with no exported API signature changes reported in the summary; there are no other unrelated files or features modified, so no out-of-scope changes were introduced.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch edison/fix/13903

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9755f43 and b5de9cc.

📒 Files selected for processing (1)
  • packages/reactivity/__tests__/readonly.spec.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/reactivity/__tests__/readonly.spec.ts (2)
packages/reactivity/src/ref.ts (1)
  • ref (59-61)
packages/reactivity/src/index.ts (3)
  • ref (2-2)
  • readonly (25-25)
  • isReadonly (27-27)
🔇 Additional comments (2)
packages/reactivity/__tests__/readonly.spec.ts (2)

526-534: LGTM! Test correctly validates the fix for issue #13903.

This test properly verifies that when a ref is nested inside a readonly object, accessing the ref property returns a readonly-wrapped value. The test structure accurately reflects the reported issue scenario and ensures the fix prevents mutation of arrays contained in refs within readonly objects.


526-534: No change needed — mutation prevention is already covered.

readonly.spec.ts already contains tests asserting mutation-prevention and warning behavior (e.g. assignment to wrapped[0], collection mutation tests, and readonly ref assignment tests). (fossies.org)

Likely an incorrect or invalid review comment.


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

Copy link

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB (+44 B) 38.5 kB (+16 B) 34.7 kB (+19 B)
vue.global.prod.js 159 kB (+44 B) 58.6 kB (+15 B) 52.1 kB (+26 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.7 kB (+44 B) 18.3 kB (+13 B) 16.7 kB (-1 B)
createApp 54.7 kB (+44 B) 21.3 kB (+14 B) 19.5 kB (+11 B)
createSSRApp 58.9 kB (+44 B) 23 kB (+16 B) 21 kB (+21 B)
defineCustomElement 59.7 kB (+44 B) 22.9 kB (+15 B) 20.9 kB (+14 B)
overall 68.8 kB (+44 B) 26.5 kB (+18 B) 24.1 kB (-13 B)

Copy link

pkg-pr-new bot commented Sep 18, 2025

Open in StackBlitz

@vue/compiler-core

npm i https://pkg.pr.new/@vue/compiler-core@13905

@vue/compiler-dom

npm i https://pkg.pr.new/@vue/compiler-dom@13905

@vue/compiler-sfc

npm i https://pkg.pr.new/@vue/compiler-sfc@13905

@vue/compiler-ssr

npm i https://pkg.pr.new/@vue/compiler-ssr@13905

@vue/reactivity

npm i https://pkg.pr.new/@vue/reactivity@13905

@vue/runtime-core

npm i https://pkg.pr.new/@vue/runtime-core@13905

@vue/runtime-dom

npm i https://pkg.pr.new/@vue/runtime-dom@13905

@vue/server-renderer

npm i https://pkg.pr.new/@vue/server-renderer@13905

@vue/shared

npm i https://pkg.pr.new/@vue/shared@13905

vue

npm i https://pkg.pr.new/vue@13905

@vue/compat

npm i https://pkg.pr.new/@vue/compat@13905

commit: b5de9cc

Copy link

@coderabbitai coderabbitai bot left a 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/reactivity/src/baseHandlers.ts (1)

122-123: Check behavioral change: readonly(arrayOfRefs)[i] becomes a readonly-wrapped Ref

For array + integer keys, value is the Ref itself, and we now return readonly(ref). That prevents writes to .value (warns/no-op). If this change is intentional, please add tests to codify it; if not, guard against wrapping Refs:

-      return isReadonly && isObject(value) ? readonly(value) : value
+      return isReadonly && isObject(value) && !isRef(value)
+        ? readonly(value)
+        : value

Suggested tests:

  • readonly({ a: ref([]) }).a.push(...) warns and no mutation (bug repro).
  • readonly([ref(0)])[0].value = 1 — assert expected behavior per your decision (warn/no-op vs allowed).
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b555f02 and 9755f43.

📒 Files selected for processing (1)
  • packages/reactivity/src/baseHandlers.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/reactivity/src/baseHandlers.ts (3)
packages/reactivity/src/ref.ts (7)
  • value (123-134)
  • value (136-158)
  • value (307-309)
  • value (311-313)
  • value (360-363)
  • value (365-367)
  • value (380-382)
packages/shared/src/general.ts (2)
  • isIntegerKey (77-81)
  • isObject (53-54)
packages/reactivity/src/reactive.ts (2)
  • isReadonly (336-338)
  • readonly (205-215)
⏰ 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/reactivity/src/baseHandlers.ts (1)

120-124: LGTM: readonly now applied to unwrapped ref values on readonly proxies

This fixes the reported mutation via push on readonly({ myArray: ref([]) }) by returning a readonly-wrapped array/object from ref properties. Behavior matches deep-readonly expectations while preserving array-index non-unwrapping.

@edison1105 edison1105 added ready to merge The PR is ready to be merged. scope: reactivity 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. labels Sep 18, 2025
@edison1105
Copy link
Member Author

/ecosystem-ci run

@vue-bot
Copy link
Contributor

vue-bot commented Sep 18, 2025

📝 Ran ecosystem CI: Open

suite result latest scheduled
nuxt success success
language-tools success success
pinia success success
primevue success success
quasar success success
test-utils success success
vant success success
vuetify failure failure
radix-vue success success
router success success
vitepress success success
vue-i18n success success
vue-simple-compiler success success
vite-plugin-vue success success
vueuse success success
vue-macros 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: reactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Array.prototype.push mutates readonly arrays created with ref([])
2 participants