astral-sh/ruff · error

Notebook document expected from notebook source kind

Error message

Notebook document expected from notebook source kind

What it means

In the Ruff language server's `fix_all`, when the request targets a notebook source kind, the code calls `query.as_notebook()` and bails with this anyhow error if the query does not actually carry a notebook document. This is an internal invariant violation: the source kind says notebook but the document payload is not one.

Source

Thrown at crates/ruff_server/src/fix.rs:107

    }

    // fast path: if `transformed` is still borrowed, no changes were made and we can return early
    if let Cow::Borrowed(_) = transformed {
        return Ok(Fixes::default());
    }

    if let (Some(source_notebook), Some(modified_notebook)) =
        (source_kind.as_ipy_notebook(), transformed.as_ipy_notebook())
    {
        fn cell_source(cell: &ruff_notebook::Cell) -> String {
            match cell.source() {
                SourceValue::String(string) => string.clone(),
                SourceValue::StringArray(array) => array.join(""),
            }
        }

        let Some(notebook) = query.as_notebook() else {
            anyhow::bail!("Notebook document expected from notebook source kind");
        };
        let mut fixes = Fixes::default();
        for ((source, modified), uri) in source_notebook
            .cells()
            .iter()
            .map(cell_source)
            .zip(modified_notebook.cells().iter().map(cell_source))
            .zip(notebook.uris())
        {
            let source_index = LineIndex::from_source_text(&source);
            let modified_index = LineIndex::from_source_text(&modified);

            let Replacement {
                source_range,
                modified_range,
            } = Replacement::between(
                &source,
                source_index.line_starts(),

View on GitHub (pinned to 26f38c119c)

Solutions

  1. Reopen or refocus the notebook in the editor so the server re-resolves the document, then retry the fix-all request
  2. Check the client request payload: the document URI/kind must match an open notebook document
  3. Update the Ruff language server / editor extension; if reproducible, file a bug with reproduction steps since this indicates an internal mismatch
Defensive patterns

Strategy: try-catch

Try / catch

client.onRequest('textDocument/codeAction', async (params) => {
  try {
    return await requestFixAll(params);
  } catch (e) {
    if (String(e.message).includes('Notebook document expected')) {
      // re-resolve the document and retry once
      await reopenNotebook(params.textDocument.uri);
      return await requestFixAll(params);
    }
    throw e;
  }
});

Prevention

When it happens

Trigger: Sending a code-action/fix-all request whose document identifier claims a notebook source kind while the resolved query holds a plain string document (or vice versa the notebook lookup fails after conversion).

Common situations: Editor/plugin bugs constructing fix-all params for notebook cells with stale or wrong URIs; race where a notebook was closed/converted to a text document between request and handling; custom clients mislabeling source kinds.

Related errors


AI-assisted analysis of astral-sh/ruff@26f38c119c (2026-09-05). Data as JSON: /api/errors/22c3a49148cf484e. Report an issue: GitHub.