From 242ea5d4259fcc1d83ba3c1e824dd5757107c806 Mon Sep 17 00:00:00 2001 From: Roman Claudiu Date: Sat, 7 Nov 2026 16:41:29 +0200 Subject: [PATCH 1/3] fix: emit replace patches for indices dropped by shrinking length Assigning a smaller `length` to a draft array truncated `copy_` without marking the dropped indices as assigned. If the array then grew back to at least its base length, generateArrayPatches skipped those indices, so the patches did not replay to the result and the inverse patches did not restore the base. Mark the dropped base indices as assigned in the array set trap, as deleting them one by one already does. Fixes #1355 --- __tests__/patch.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++ src/core/proxy.ts | 16 ++++++++++- 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/__tests__/patch.js b/__tests__/patch.js index c4aff9a6..65a83b73 100644 --- a/__tests__/patch.js +++ b/__tests__/patch.js @@ -790,6 +790,74 @@ runPatchTests( ] ) +// #1355 +runPatchTests( + "arrays - truncate with length, then grow", + {x: [1]}, + d => { + d.x.length = 0 + d.x[1] = 2 + }, + [ + {op: "replace", path: ["x", 0], value: undefined}, + {op: "add", path: ["x", 1], value: 2} + ], + [ + {op: "replace", path: ["x", 0], value: 1}, + {op: "remove", path: ["x", 1]} + ], + {x: [undefined, 2]} +) + +runPatchTests( + "arrays - truncate with length, then restore the length", + {x: [1, 2]}, + d => { + d.x.length = 0 + d.x.length = 2 + }, + [ + {op: "replace", path: ["x", 0], value: undefined}, + {op: "replace", path: ["x", 1], value: undefined} + ], + [ + {op: "replace", path: ["x", 0], value: 1}, + {op: "replace", path: ["x", 1], value: 2} + ], + {x: [undefined, undefined]} +) + +runPatchTests( + "arrays - truncate with length, then push", + {x: [1, 2, 3]}, + d => { + d.x.length = 1 + d.x.push(4, 5) + }, + [ + {op: "replace", path: ["x", 1], value: 4}, + {op: "replace", path: ["x", 2], value: 5} + ], + [ + {op: "replace", path: ["x", 1], value: 2}, + {op: "replace", path: ["x", 2], value: 3} + ], + {x: [1, 4, 5]} +) + +runPatchTests( + "arrays - truncate with length, then grow back to the same values", + {x: [1, 2]}, + d => { + d.x.length = 0 + d.x[0] = 1 + d.x[1] = 2 + }, + [], + [], + {x: [1, 2]} +) + runPatchTests( "arrays - pop twice", {x: [1, 2, 3]}, diff --git a/src/core/proxy.ts b/src/core/proxy.ts index 8a2ee62d..363dfe73 100644 --- a/src/core/proxy.ts +++ b/src/core/proxy.ts @@ -284,7 +284,21 @@ arrayTraps.set = function (state, prop, value) { isNaN(parseInt(prop as any)) ) die(14) - return objectTraps.set!.call(this, state[0], prop, value, state[0]) + const arrayState = state[0] + const oldLength = prop === "length" ? latest(arrayState).length : 0 + const result = objectTraps.set!.call( + this, + arrayState, + prop, + value, + arrayState + ) + // Shrinking `length` drops the tail without going through `deleteProperty`, + // so mark the dropped base indices as assigned. Otherwise, if the array grows + // back, patch generation sees no assignment for them and emits no `replace`. + const end = Math.min(oldLength, arrayState.base_.length) + for (let i = +value; i < end; i++) arrayState.assigned_!.set("" + i, true) + return result } // Access a property without creating an Immer draft. From c160dc5f56b157eb1b8c1be4d264d8627ce36061 Mon Sep 17 00:00:00 2001 From: Roman Claudiu Date: Sun, 8 Nov 2026 23:01:42 +0200 Subject: [PATCH 2/3] refactor: only mark dropped indices on length writes --- src/core/proxy.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/core/proxy.ts b/src/core/proxy.ts index 363dfe73..fd92e7dc 100644 --- a/src/core/proxy.ts +++ b/src/core/proxy.ts @@ -285,7 +285,9 @@ arrayTraps.set = function (state, prop, value) { ) die(14) const arrayState = state[0] - const oldLength = prop === "length" ? latest(arrayState).length : 0 + if (prop !== "length") + return objectTraps.set!.call(this, arrayState, prop, value, arrayState) + const oldLength = latest(arrayState).length const result = objectTraps.set!.call( this, arrayState, From 172b2cdc3d06095afb55365d520852d6e024b016 Mon Sep 17 00:00:00 2001 From: Roman Claudiu Date: Thu, 26 Nov 2026 22:23:37 +0200 Subject: [PATCH 3/3] perf: skip marking dropped indices when patches are not enabled Shrinking length only marks the dropped base indices when the patches plugin is loaded. scope_.patches_ is only set after the recipe has run (and at finishDraft for manual drafts), so it cannot be the guard. --- __tests__/patch.js | 19 ++++++++++++++++++- src/core/proxy.ts | 9 +++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/__tests__/patch.js b/__tests__/patch.js index 65a83b73..d6d82a69 100644 --- a/__tests__/patch.js +++ b/__tests__/patch.js @@ -8,7 +8,9 @@ import { immerable, nothing, enablePatches, - enableMapSet + enableMapSet, + createDraft, + finishDraft } from "../src/immer" enablePatches() @@ -858,6 +860,21 @@ runPatchTests( {x: [1, 2]} ) +test("arrays - truncate with length, then grow, on a draft from createDraft", () => { + // finishDraft is the first place a manual draft learns that patches are wanted + const draft = createDraft({x: [1]}) + draft.x.length = 0 + draft.x[1] = 2 + let patches + finishDraft(draft, p => { + patches = p + }) + expect(patches).toEqual([ + {op: "replace", path: ["x", 0], value: undefined}, + {op: "add", path: ["x", 1], value: 2} + ]) +}) + runPatchTests( "arrays - pop twice", {x: [1, 2, 3]}, diff --git a/src/core/proxy.ts b/src/core/proxy.ts index fd92e7dc..9546e30c 100644 --- a/src/core/proxy.ts +++ b/src/core/proxy.ts @@ -22,7 +22,9 @@ import { ENUMERABLE, VALUE, isArray, - isArrayIndex + isArrayIndex, + isPluginLoaded, + PluginPatches } from "../internal" interface ProxyBaseState extends ImmerBaseState { @@ -285,7 +287,10 @@ arrayTraps.set = function (state, prop, value) { ) die(14) const arrayState = state[0] - if (prop !== "length") + // Only patch generation reads the dropped indices below; skip the work when + // patches are not enabled. (`scope_.patches_` is only set once the recipe + // has run, so it can't tell yet whether this call records patches.) + if (prop !== "length" || !isPluginLoaded(PluginPatches)) return objectTraps.set!.call(this, arrayState, prop, value, arrayState) const oldLength = latest(arrayState).length const result = objectTraps.set!.call(