Skip to content

Nested draft arrays not commited with enableArrayMethods #1209

@maclockard

Description

@maclockard

🐛 Bug Report

When enableArrayMethods() is added, immer does not always properly commit nested drafts, resulting in illegally accessing revoked proxies. This appears to happen when copying an existing draft object then adding it back to the array via mutation.

Link to repro

Code sandbox link

Source

import { enableArrayMethods, produce } from "immer";
enableArrayMethods();

const oldState = [{ nestedArray: [] }];

const newState = produce(oldState, (state) => {
  const newVal = { ...state[0] };
  state.push(newVal);
});

newState[1].nestedArray.length; // This line throws the error!

To Reproduce

  1. Open the above code sandbox link
  2. Observe error being thrown

Can also run the provided snippet in node.

Observed behavior

newState[1].nestedArray is a revoked proxy, indicating that the draft array was not committed.

This error is thrown:

TypeError: illegal operation attempted on a revoked proxy
    $csb$eval index.js:16
    H eval.js:31
    evaluate transpiled-module.js:715
    evaluateTranspiledModule manager.js:338
    c transpiled-module.js:697
    loadResources index.html:3
    $csb$eval index.html:9
    H eval.js:31
    evaluate transpiled-module.js:715
    evaluateTranspiledModule manager.js:338
    evaluateModule manager.js:309
    fi compile.ts:865

Expected behavior

All nested draft arrays are properly committed as normal arrays. This is the behavior when enableArrayMethods has not been activated.

Environment

We only accept bug reports against the latest Immer version.

  • Immer version:11.1.3
  • I filed this report against the latest version of Immer
  • Occurs with setUseProxies(true)
  • Occurs with setUseProxies(false) (ES5 only)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions