fix: draft relocated base refs after reverse/sort in array-methods plugin#1255
Open
spokodev wants to merge 1 commit into
Open
fix: draft relocated base refs after reverse/sort in array-methods plugin#1255spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
…ugin With enableArrayMethods(), reverse() and sort() run natively on copy_, which still holds raw base references. Reordering relocates an un-drafted base object to an index where it no longer equals base_[prop], so the get trap's positional check misses it and returns the raw base object. Writing to that object mutates the user's base state, violating immer's guarantee that the base state is never modified. Patches and inverse patches are wrong too. Detect a relocated base reference in the get trap (only after a reorder, for indices the user did not explicitly assign) and draft it before exposing it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
immer's core contract is that the base state passed to
produceis never mutated (the docs state the result is produced "without modifying the originalbaseState"). WithenableArrayMethods(), callingreverse()orsort()inside a recipe and then mutating an element breaks this guarantee: the write lands on the user's base object.Repro
Without the plugin, vanilla immer handles the same recipe correctly.
Root cause
handleReorderingOperationinsrc/plugins/arrayMethods.tsruns the nativereverse/sortdirectly onstate.copy_. SinceprepareCopyshallow-copiesbase_,copy_still holds the raw base references, and the reorder relocates an un-drafted base object to a new index.The get trap in
src/core/proxy.tsonly drafts a child when it sits at its original position (value === peek(state.base_, prop)). After a reordercopy_[0] !== base_[0], so the check falls through andreturn valuehands back the raw base object. Mutating it then mutates the base, and the generated patches/inverse are wrong too.Fix
Recognize a relocated base reference in the get trap and draft it before exposing it. The extra check only runs after a reorder (
allIndicesReassigned_), skips indices the user explicitly assigned, and skips values that are already drafts or non-draftable, so the plugin's lazy-proxy optimization is preserved for the common path.Tests
Added two cases under "mutating array methods" in
__tests__/base.js:reverse()then mutate index 0: assertsisDraft(d[0])is true, base and the relocated object are untouched, and the result is correct.sort()then mutate index 0 withproduceWithPatches: same base-untouched assertions plus a patch / inverse-patch round-trip.Both fail on
main(in the array-plugin suite) and pass with the fix. Full suite is green (3669 passed, 8 skipped).