Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

Fix: Use cell name to identify setup cells when id is null (#8137)

Myles Scolnickc41ca6f8cf09
diff --git a/frontend/src/core/cells/__tests__/session.test.ts b/frontend/src/core/cells/__tests__/session.test.ts
index 1bf98fd2cb..f6fa73e08e 100644
--- a/frontend/src/core/cells/__tests__/session.test.ts
+++ b/frontend/src/core/cells/__tests__/session.test.ts
@@ -7,7 +7,7 @@ import { parseOutline } from "@/core/dom/outline";
import { MultiColumn, visibleForTesting } from "@/utils/id-tree";
import { invariant } from "@/utils/invariant";
import { Logger } from "@/utils/Logger";
-import type { CellId } from "../ids";
+import { type CellId, SETUP_CELL_ID } from "../ids";
import { notebookStateFromSession } from "../session";
// Mock dependencies
@@ -358,6 +358,42 @@ describe("notebookStateFromSession", () => {
serializedEditorState: null,
});
});
+
+ it("uses SETUP_CELL_ID for setup cell with null id", () => {
+ const notebookCell = {
+ id: null,
+ code: "import marimo as mo",
+ name: "setup",
+ code_hash: null,
+ config: {
+ hide_code: null,
+ disabled: null,
+ column: null,
+ },
+ };
+ const notebook = createNotebook([notebookCell as any]);
+ const result = notebookStateFromSession(null, notebook);
+
+ expect(result).not.toBeNull();
+ invariant(result, "result is null");
+ expect(result.cellIds.inOrderIds).toEqual(
+ MultiColumn.from([[SETUP_CELL_ID]]).inOrderIds,
+ );
+ expect(result.cellData[SETUP_CELL_ID]).toEqual({
+ id: SETUP_CELL_ID,
+ name: "setup",
+ code: "import marimo as mo",
+ edited: false,
+ lastCodeRun: null,
+ lastExecutionTime: null,
+ config: {
+ hide_code: false,
+ disabled: false,
+ column: null,
+ },
+ serializedEditorState: null,
+ });
+ });
});
describe("both session and notebook scenarios", () => {
diff --git a/frontend/src/core/cells/session.ts b/frontend/src/core/cells/session.ts
index bebd80c8b9..7150d25b11 100644
--- a/frontend/src/core/cells/session.ts
+++ b/frontend/src/core/cells/session.ts
@@ -6,7 +6,7 @@ import { MultiColumn } from "@/utils/id-tree";
import { Logger } from "@/utils/Logger";
import { parseOutline } from "../dom/outline";
import type { NotebookState } from "./cells";
-import { CellId } from "./ids";
+import { CellId, SETUP_CELL_ID } from "./ids";
import {
type CellData,
type CellRuntimeState,
@@ -20,6 +20,22 @@ const EMPTY_STRING = "";
type SessionCell = api.Session["NotebookSessionV1"]["cells"][0];
type NotebookCell = api.Notebook["NotebookV1"]["cells"][0];
+/**
+ * Get the cell ID from a cell object.
+ * If the cell has an ID, use it.
+ * If the cell has a name of "setup", use the special SETUP_CELL_ID.
+ * Otherwise, generate a new random ID.
+ */
+function getCellId(cell: { id?: string | null; name?: string | null }): CellId {
+ if (cell.id) {
+ return cell.id as CellId;
+ }
+ if (cell.name === SETUP_CELL_ID) {
+ return SETUP_CELL_ID;
+ }
+ return CellId.create();
+}
+
function mergeSessionAndNotebookCells(
session: api.Session["NotebookSessionV1"] | null | undefined,
notebook: api.Notebook["NotebookV1"] | null | undefined,
@@ -36,9 +52,7 @@ function mergeSessionAndNotebookCells(
}
if (!session) {
- const cellIds = (notebook?.cells.map(
- (cell) => cell.id ?? CellId.create(),
- ) || []) as CellId[];
+ const cellIds = notebook?.cells.map((cell) => getCellId(cell)) || [];
return {
cellIds,
sessionCellData: new Map(
@@ -57,9 +71,7 @@ function mergeSessionAndNotebookCells(
}
if (!notebook) {
- const cellIds = session.cells.map(
- (cell) => cell.id ?? CellId.create(),
- ) as CellId[];
+ const cellIds = session.cells.map((cell) => getCellId(cell));
return {
cellIds,
sessionCellData: new Map(
@@ -105,7 +117,7 @@ function mergeSessionAndNotebookCells(
for (let i = 0; i < notebook.cells.length; i++) {
const notebookCell = notebook.cells[i];
if (notebookCell) {
- const id = (notebookCell.id ?? CellId.create()) as CellId;
+ const id = getCellId(notebookCell);
mergedCellIdsTyped.push(id);
// Should always be set, but good typing fallback too.