Skip to content

fix(vanilla): remove Object.preventExtensions from createSnapshotDefault for Hermes compatibility#1220

Merged
dai-shi merged 2 commits intopmndrs:mainfrom
manakuro:fix/874-hermes-type-error
May 1, 2026
Merged

fix(vanilla): remove Object.preventExtensions from createSnapshotDefault for Hermes compatibility#1220
dai-shi merged 2 commits intopmndrs:mainfrom
manakuro:fix/874-hermes-type-error

Conversation

@manakuro
Copy link
Copy Markdown
Contributor

Related Bug Reports or Discussions

Fixes #874

Summary

Removes Object.preventExtensions(snap) from createSnapshotDefault to fix a TypeError that occurs on React Native (Hermes).

Problem

Hermes enforces strict Proxy invariant checks. When a Proxy target is non-extensible, Hermes requires that the ownKeys trap returns every key present on the target — any mismatch throws:

TypeError: ownKeys target is non-extensible but key is missing from trap result

createSnapshotDefault calls Object.preventExtensions(snap) at the end of snapshot creation. Because Object.preventExtensions is irreversible, there is no way to undo it after the fact. The only viable fix is to not call it in the first place.

This is particularly pronounced with arrays (e.g. InformationItem[]), since their dynamic key structure ('0', '1', 'length', etc.) makes Hermes's invariant check especially strict.

The error surfaces at render time rather than at the point of assignment, because createSnapshotDefault is only called when useSnapshot runs during the render phase — not when the value is written to the store.

Note: A fix on the Hermes side is not planned. The React Native team has indicated that the Proxy API is internally discouraged at Meta. See: facebook/hermes#1609

Change

  • Removed Object.preventExtensions(snap) from createSnapshotDefault

Trade-off

Removing Object.preventExtensions means snapshots are no longer guaranteed to be non-extensible. New properties could technically be added to a snapshot object at runtime. However, since snapshots are intended to be consumed as read-only values within a render cycle, this is unlikely to cause issues in practice.

Check List

  • pnpm run fix for formatting and linting code and docs

Dropped `Object.preventExtensions` to simplify snapshot behavior. Also removed associated test cases, as immutability is no longer enforced. This fixes the issue of Hermes enforcing strict Proxy invariant checks.
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 13, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
valtio Error Error Apr 13, 2026 0:52am

Request Review

@codesandbox-ci
Copy link
Copy Markdown

codesandbox-ci Bot commented Apr 13, 2026

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Comment thread src/vanilla.ts
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 13, 2026

More templates

npm i https://pkg.pr.new/valtio@1220

commit: 3510558

@manakuro
Copy link
Copy Markdown
Contributor Author

The Vercel deployment is failing, but I don't have permission to view the logs. Could you please take a look?

@dai-shi
Copy link
Copy Markdown
Member

dai-shi commented Apr 13, 2026

Build Failed
No Next.js version detected. Make sure your package.json has "next" in either "dependencies" or "devDependencies". Also check your Root Directory setting matches the directory of your package.json file.

It's not your fault obviously.

@dai-shi
Copy link
Copy Markdown
Member

dai-shi commented Apr 29, 2026

Still trying to figure out #1221. But maybe I can merge this without waiting for it.

@manakuro
Copy link
Copy Markdown
Contributor Author

Still trying to figure out #1221. But maybe I can merge this without waiting for it.

Thanks! I appreciate your help.

@dai-shi dai-shi merged commit 2cbc84c into pmndrs:main May 1, 2026
32 of 33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants