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: strict execution single cell reference error (#1686)

Dylan Madisetti89bc10549297
diff --git a/frontend/src/components/editor/output/MarimoErrorOutput.tsx b/frontend/src/components/editor/output/MarimoErrorOutput.tsx
index 2a757f462f..53841db7ff 100644
--- a/frontend/src/components/editor/output/MarimoErrorOutput.tsx
+++ b/frontend/src/components/editor/output/MarimoErrorOutput.tsx
@@ -157,7 +157,7 @@ export const MarimoErrorOutput = ({
<Tip>
Ensure that&nbsp;
<CellLinkError cellId={error.blamed_cell as CellId} />
- &nbsp;defines the variable {error.ref}, or turn off strict
+ &nbsp;defines the variable `{error.ref}`, or turn off strict
execution.
</Tip>
</div>
@@ -166,7 +166,12 @@ export const MarimoErrorOutput = ({
titleContents = "Ancestor prevented from running";
alertVariant = "default";
textColor = "text-secondary-foreground";
- return (
+ return error.blamed_cell == null ? (
+ <div key={idx}>
+ {error.msg}
+ (<CellLinkError cellId={error.raising_cell as CellId} />)
+ </div>
+ ) : (
<div key={idx}>
{error.msg}
(<CellLinkError cellId={error.raising_cell as CellId} />
diff --git a/marimo/_messaging/errors.py b/marimo/_messaging/errors.py
index 5151b775ae..b39468fcc5 100644
--- a/marimo/_messaging/errors.py
+++ b/marimo/_messaging/errors.py
@@ -37,7 +37,7 @@ class MarimoInterruptionError:
class MarimoAncestorPreventedError:
msg: str
raising_cell: CellId_t
- blamed_cell: CellId_t
+ blamed_cell: Optional[CellId_t]
type: Literal["ancestor-prevented"] = "ancestor-prevented"
@@ -73,7 +73,7 @@ class UnknownError:
class MarimoStrictExecutionError:
msg: str
ref: str
- blamed_cell: CellId_t
+ blamed_cell: Optional[CellId_t]
type: Literal["strict-exception"] = "strict-exception"
diff --git a/marimo/_runtime/dataflow.py b/marimo/_runtime/dataflow.py
index 8f77afb30b..44b8ccaad6 100644
--- a/marimo/_runtime/dataflow.py
+++ b/marimo/_runtime/dataflow.py
@@ -362,7 +362,7 @@ class DirectedGraph:
processed.add(maybe_private)
if inclusive:
- return processed
+ return processed | refs
return processed - refs
diff --git a/marimo/_runtime/runner/cell_runner.py b/marimo/_runtime/runner/cell_runner.py
index 2a0b0115c5..f6b9db1ac9 100644
--- a/marimo/_runtime/runner/cell_runner.py
+++ b/marimo/_runtime/runner/cell_runner.py
@@ -350,8 +350,8 @@ class Runner:
try:
(blamed_cell, *_) = self.graph.get_defining_cells(e.ref)
except KeyError:
- # This should never happen, but just in case
- blamed_cell = e.ref
+ # The reference is not found anywhere else in the graph
+ blamed_cell = None
output = MarimoStrictExecutionError(
f"marimo was unable to resolve "
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index 2c9fd08cca..45772d744c 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -26,7 +26,12 @@ from marimo._ast.visitor import Name
from marimo._config.config import ExecutionType, MarimoConfig, OnCellChangeType
from marimo._data.preview_column import get_column_preview
from marimo._messaging.cell_output import CellChannel
-from marimo._messaging.errors import Error, MarimoSyntaxError, UnknownError
+from marimo._messaging.errors import (
+ Error,
+ MarimoStrictExecutionError,
+ MarimoSyntaxError,
+ UnknownError,
+)
from marimo._messaging.ops import (
Alert,
CellOp,
@@ -932,6 +937,13 @@ class Kernel:
isolated=is_python_isolated(),
).broadcast()
+ def propagate_kernel_errors(
+ runner: cell_runner.Runner,
+ ) -> None:
+ for cell_id, error in runner.exceptions.items():
+ if isinstance(error, MarimoStrictExecutionError):
+ self.errors[cell_id] = (error,)
+
runner = cell_runner.Runner(
roots=roots,
graph=self.graph,
@@ -944,7 +956,8 @@ class Kernel:
preparation_hooks=PREPARATION_HOOKS + [invalidate_state],
pre_execution_hooks=PRE_EXECUTION_HOOKS,
post_execution_hooks=POST_EXECUTION_HOOKS,
- on_finish_hooks=ON_FINISH_HOOKS + [broadcast_missing_packages],
+ on_finish_hooks=ON_FINISH_HOOKS
+ + [broadcast_missing_packages, propagate_kernel_errors],
)
# I/O
diff --git a/tests/_runtime/test_runtime.py b/tests/_runtime/test_runtime.py
index 7a92c317b1..868858ffd5 100644
--- a/tests/_runtime/test_runtime.py
+++ b/tests/_runtime/test_runtime.py
@@ -2,6 +2,7 @@
from __future__ import annotations
import sys
+import textwrap
from typing import TYPE_CHECKING, Sequence
import pytest
@@ -11,6 +12,7 @@ from marimo._messaging.errors import (
CycleError,
DeleteNonlocalError,
Error,
+ MarimoStrictExecutionError,
MultipleDefinitionError,
)
from marimo._messaging.ops import CellOp
@@ -428,9 +430,16 @@ class TestExecution:
)
assert "x" not in k.globals
assert "y" not in k.globals
- assert set(k.errors.keys()) == {"0", "1"}
+ if k.execution_type == "strict":
+ # 2 isn't valid in strict mode because it will refuse to execute
+ # due to missing x.
+ assert set(k.errors.keys()) == {"0", "1", "2"}
+ else:
+ assert set(k.errors.keys()) == {"0", "1"}
assert len(k.errors["0"]) == 1
assert len(k.errors["1"]) == 1
+ if k.execution_type == "strict":
+ assert len(k.errors["2"]) == 1
_check_edges(k.errors["0"][0], [("0", ["x"], "1"), ("1", ["y"], "0")])
_check_edges(k.errors["1"][0], [("0", ["x"], "1"), ("1", ["y"], "0")])
@@ -467,7 +476,12 @@ class TestExecution:
# break cycle by deleting cell
await k.delete(DeleteCellRequest(cell_id="1"))
- assert not k.errors
+ if k.execution_type == "strict":
+ # Still invalid in strict mode because y is missing.
+ assert set(k.errors.keys()) == {"0"}
+ assert isinstance(k.errors["0"][0], MarimoStrictExecutionError)
+ else:
+ assert not k.errors
async def test_delete_nonlocal_error(self, any_kernel: Kernel) -> None:
k = any_kernel
@@ -480,7 +494,10 @@ class TestExecution:
)
assert "y" not in k.globals
assert "z" not in k.globals
- assert set(k.errors.keys()) == {"1"}
+ if k.execution_type == "strict":
+ assert set(k.errors.keys()) == {"1", "2"}
+ else:
+ assert set(k.errors.keys()) == {"1"}
assert k.errors["1"] == (DeleteNonlocalError("x", ("0",)),)
# fix cell 1, should run cell 1 and 2
@@ -510,7 +527,8 @@ class TestExecution:
# Delete the cell that defines x. There shouldn't be any more errors
# because x no longer exists.
await k.delete(DeleteCellRequest(cell_id="0"))
- assert not k.errors
+ if k.execution_type != "strict":
+ assert not k.errors
# Add x back in.
await k.run([ExecutionRequest(cell_id="2", code="x=0")])
@@ -994,6 +1012,35 @@ class TestStrictExecution:
else:
assert k.globals["V1"] == 11
+ @staticmethod
+ async def test_wont_execute_bad_ref(execution_kernel: Kernel) -> None:
+ k = execution_kernel
+ await k.run(
+ [
+ ExecutionRequest(
+ cell_id="0",
+ code=textwrap.dedent("""
+ try:
+ missing
+ except:
+ pass
+ x = 1
+ """),
+ ),
+ ]
+ )
+
+ if k.execution_type == "strict":
+ assert "x" not in k.globals
+ assert set(k.errors.keys()) == {"0"}
+ assert len(k.errors["0"]) == 1
+ assert isinstance(k.errors["0"][0], MarimoStrictExecutionError)
+ assert k.errors["0"][0].ref == "missing"
+ # Check that normal execution still runs the block
+ else:
+ assert "x" in k.globals
+ assert not k.errors
+
class TestStoredOutput:
async def test_ui_element_in_output_stored(