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: add cell prefix to setup cell registration (#7648)

Dylan Madisetti489b72d02a19
diff --git a/marimo/_ast/cell_manager.py b/marimo/_ast/cell_manager.py
index 152b6b897a..4d84e2ec51 100644
--- a/marimo/_ast/cell_manager.py
+++ b/marimo/_ast/cell_manager.py
@@ -165,7 +165,7 @@ class CellManager:
) -> Cell:
"""Registers cells when called through a context block."""
cell = context_cell_factory(
- cell_id=CellId_t(SETUP_CELL_NAME),
+ cell_id=self.setup_cell_id,
# NB. carry along the frame from the initial call.
frame=frame,
)
@@ -221,7 +221,7 @@ class CellManager:
self, cell_def: CellDef, app: InternalApp | None = None
) -> None:
if isinstance(cell_def, SetupCell):
- cell_id = CellId_t(SETUP_CELL_NAME)
+ cell_id = self.setup_cell_id
else:
cell_id = self.create_cell_id()
filename = app.filename if app is not None else None
@@ -265,7 +265,7 @@ class CellManager:
# If this is the first cell, and its name is setup, assume that it's
# the setup cell.
if len(self._cell_data) == 0 and name == SETUP_CELL_NAME:
- cell_id = CellId_t(SETUP_CELL_NAME)
+ cell_id = self.setup_cell_id
else:
cell_id = self.create_cell_id()
@@ -518,3 +518,7 @@ class CellManager:
@property
def seen_ids(self) -> set[CellId_t]:
return self._cell_id_generator.seen_ids
+
+ @property
+ def setup_cell_id(self) -> CellId_t:
+ return CellId_t(self.prefix + SETUP_CELL_NAME)
diff --git a/marimo/_smoke_tests/appcomp/setup_embed_setup/inner_with_setup.py b/marimo/_smoke_tests/appcomp/setup_embed_setup/inner_with_setup.py
new file mode 100644
index 0000000000..4b4972f91a
--- /dev/null
+++ b/marimo/_smoke_tests/appcomp/setup_embed_setup/inner_with_setup.py
@@ -0,0 +1,27 @@
+# Copyright 2026 Marimo. All rights reserved.
+
+import marimo
+
+__generated_with = "0.18.4"
+app = marimo.App(width="medium")
+
+
+with app.setup:
+ import marimo as mo
+
+
+def _():
+ inner_value = 42
+ mo.md("# Inner App with Setup Cell")
+ return (inner_value,)
+
+
+def _(inner_value):
+ mo.md(f"Inner value is: {inner_value}")
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/marimo/_smoke_tests/appcomp/setup_embed_setup/main.py b/marimo/_smoke_tests/appcomp/setup_embed_setup/main.py
new file mode 100644
index 0000000000..406239c251
--- /dev/null
+++ b/marimo/_smoke_tests/appcomp/setup_embed_setup/main.py
@@ -0,0 +1,81 @@
+# Copyright 2026 Marimo. All rights reserved.
+"""
+Smoke test for setup cell embedding.
+
+This tests the scenario where:
+- Main app has a setup cell
+- Inner app has a setup cell
+- Main app embeds the inner app
+
+Bug: The main app's setup cell should NOT rerun when the inner app is embedded.
+"""
+
+import marimo
+
+__generated_with = "0.18.4"
+app = marimo.App(width="medium")
+
+with app.setup:
+ setup_tracker = {"count": 0}
+ setup_tracker["count"] += 1
+ import marimo as mo
+ from inner_with_setup import app as inner_app
+
+
[email protected](hide_code=True)
+def _():
+ mo.md("""
+ # Setup Cell Embedding Smoke Test
+
+ This notebook tests embedding an app with a setup cell from a notebook
+ that also has a setup cell
+ The outer app's setup cell should only run **once**, not rerun when
+ the inner app is embedded.
+ """)
+ return
+
+
+def _():
+ setup_count_before_embed = setup_tracker["count"]
+ mo.md(f"Setup run count before embed: **{setup_count_before_embed}**")
+ return (setup_count_before_embed,)
+
+
+async def _():
+ # Embed the inner app (which also has a setup cell)
+ result = await inner_app.embed()
+ result.output
+ return (result,)
+
+
+def _(result):
+ mo.md(f"""
+ Inner app value: **{result.defs.get('inner_value')}**
+ """)
+ return
+
+
+def _(setup_count_before_embed):
+ setup_count_after_embed = setup_tracker["count"]
+
+ # This is the key assertion - setup should still be 1
+ status = "PASS" if setup_count_after_embed == 1 else "FAIL"
+ color = "green" if status == "PASS" else "red"
+
+ mo.md(f"""
+ ## Test Result: <span style="color: {color}">{status}</span>
+
+ - Setup run count before embed: {setup_count_before_embed}
+ - Setup run count after embed: {setup_count_after_embed}
+
+ Expected setup to run exactly **1** time.
+ """)
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_ast/test_app.py b/tests/_ast/test_app.py
index 9e53b44ab1..d0c44ff37f 100644
--- a/tests/_ast/test_app.py
+++ b/tests/_ast/test_app.py
@@ -19,6 +19,7 @@ from marimo._ast.app import (
InternalApp,
)
from marimo._ast.app_config import _AppConfig
+from marimo._ast.cell_id import is_external_cell_id
from marimo._ast.errors import (
CycleError,
IncompleteRefsError,
@@ -27,18 +28,20 @@ from marimo._ast.errors import (
UnparsableError,
)
from marimo._ast.load import load_app
+from marimo._ast.names import SETUP_CELL_NAME
from marimo._convert.converters import MarimoConvert
from marimo._dependencies.dependencies import DependencyManager
from marimo._plugins.stateless.flex import vstack
from marimo._runtime.context.types import get_context
-from marimo._runtime.commands import UpdateUIElementCommand
+from marimo._runtime.commands import UpdateUIElementCommand, ExecuteCellCommand
from marimo._schemas.serialization import (
AppInstantiation,
CellDef,
NotebookSerializationV1,
)
from marimo._types.ids import CellId_t
-from tests.conftest import ExecReqProvider
+from tests.conftest import ExecReqProvider, MockedKernel
+from tests._messaging.mocks import MockStream
if TYPE_CHECKING:
from marimo._runtime.runtime import Kernel
@@ -1561,6 +1564,27 @@ class TestAppComposition:
)
assert "App.embed() cannot be called" in k.stderr.messages[0]
+ async def test_imported_app_has_prefixed_setup_cell(
+ self, k: Kernel, exec_req: ExecReqProvider
+ ) -> None:
+ """Verify app imported in kernel context has UUID-prefixed setup cell.
+
+ This tests the fix where setup cells get the prefix like other cells.
+ """
+ await k.run([
+ exec_req.get("""
+ # Import in kernel context
+ from tests._ast.app_data.notebook_filename import app
+ """)
+ ])
+ assert not k.errors
+ nb_app = k.globals["app"]
+ cell_ids = list(InternalApp(nb_app).cell_manager.cell_ids())
+ all_prefixed = all(is_external_cell_id(cid) for cid in cell_ids)
+ setup_cell_ids = [cid for cid in cell_ids if cid.endswith(SETUP_CELL_NAME)]
+ assert len(setup_cell_ids) == 1
+ assert is_external_cell_id(setup_cell_ids[0])
+
class TestAppKernelRunnerRegistry:
def test_get_runner(self, k: Kernel) -> None: