ianstormtaylor/slate · error · Error

Cannot exit root

Error message

Cannot exit root

What it means

Internal invariant of slate-react's chunk tree helper: exitChunk() moves the pointer to the parent chunk and refuses when the current chunk is the root, since the root has no parent. Called from readLeaf, returnToPreviousLeaf, insertAfter, and remove — all internal paths that should never exit from root given correct state. Marked 'istanbul ignore next'.

Source

Thrown at packages/slate-react/src/chunking/chunk-tree-helper.ts:455

   * Assuming the current node is a chunk, move the pointer into that chunk
   * repeatedly until the current node is a leaf
   *
   * @param end If true, place the pointer on the last node of the chunk.
   * Otherwise, place the pointer on the first node.
   */
  private enterChunkUntilLeaf(end: boolean) {
    while (this.pointerNode?.type === 'chunk') {
      this.enterChunk(end)
    }
  }

  /**
   * Move the pointer to the parent chunk
   */
  private exitChunk() {
    // istanbul ignore next
    if (this.pointerChunk.type === 'root') {
      throw new Error('Cannot exit root')
    }

    const previousPointerChunk = this.pointerChunk
    this.pointerChunk = previousPointerChunk.parent
    this.pointerIndex = this.pointerIndexStack.pop()!
    this.cachedPointerNode = undefined
    this.validateState()
  }

  /**
   * Insert leaves immediately after the current node, leaving the pointer on
   * the last inserted leaf
   *
   * Leaves are chunked according to the number of nodes already in the parent
   * plus the number of nodes being inserted, or the minimum depth if larger
   */
  private rawInsertAfter(leaves: ChunkLeaf[], minDepth: number) {
    if (leaves.length === 0) return

View on GitHub (pinned to 72a37c701e)

Solutions

  1. Upgrade slate-react
  2. Report the issue upstream with a minimal reproduction
  3. In forks, verify enterChunk/exitChunk calls are balanced
Defensive patterns

Strategy: retry

Try / catch

try {
  edit()
} catch (e) {
  if (e.message === 'Cannot exit root') {
    // internal bug: recover by remounting the editor
  } else throw e
}

Prevention

When it happens

Trigger: Internal bug where the pointer index stack is unbalanced (more exitChunk calls than enterChunk calls); not producible through the public API.

Common situations: Slate-react chunking bugs; forks that call exitChunk/enterChunk out of order.

Related errors


AI-assisted analysis of ianstormtaylor/slate@72a37c701e (2026-08-27). Data as JSON: /api/errors/415c0de2cce30d3d. Report an issue: GitHub.