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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: set file in compilation for app mode linecache and better stack traces (#8800)
Dylan Madisetti27d5e870b9c7
diff --git a/marimo/_ast/compiler.py b/marimo/_ast/compiler.pyindex 1db3396bfa..f2779f25ca 100644--- a/marimo/_ast/compiler.py+++ b/marimo/_ast/compiler.py@@ -2,6 +2,7 @@from __future__ import annotationsimport ast+import functoolsimport inspectimport ioimport linecache@@ -400,34 +401,63 @@ def compile_cell()-def solve_source_position(- code: str, filename: str-) -> Optional[SourcePosition]:[email protected]_cache(maxsize=1)+def _build_source_position_map(+ filename: str,+) -> tuple[tuple[str, SourcePosition], ...]:+ """Parse a notebook file and return a code→SourcePosition mapping.++ Cached per filename to avoid O(N²) when resolving N cells from+ the same file.+ """from marimo._ast.load import _maybe_contentsfrom marimo._ast.parse import parse_notebook- from marimo._utils.cell_matching import match_cell_ids_by_similaritycontents = _maybe_contents(filename)if not contents:- return None+ return ()notebook = parse_notebook(contents)if notebook is None or not notebook.valid:+ return ()+ return tuple(+ (+ cell.code.strip(),+ SourcePosition(+ filename=filename,+ lineno=cell.lineno,+ col_offset=cell.col_offset,+ ),+ )+ for cell in notebook.cells+ )+++def solve_source_position(+ code: str, filename: str+) -> Optional[SourcePosition]:+ entries = _build_source_position_map(filename)++ if not entries:return None++ # Fast path: exact match (covers run mode and unmodified cells)+ stripped = code.strip()+ for cell_code, position in entries:+ if cell_code == stripped:+ return position++ # Slow path: similarity matching for edited cells+ from marimo._utils.cell_matching import match_cell_ids_by_similarity+on_disk = {- CellId_t(str(i)): cell.code for i, cell in enumerate(notebook.cells)+ CellId_t(str(i)): cell_code for i, (cell_code, _) in enumerate(entries)}matches = match_cell_ids_by_similarity(on_disk, {CellId_t("new"): code})if not matches or len(matches) != 1:return None(cell_index,) = matches.keys()- index = int(cell_index)-- return SourcePosition(- filename=filename,- lineno=notebook.cells[index].lineno,- col_offset=notebook.cells[index].col_offset,- )+ return entries[int(cell_index)][1]def get_source_position(diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex 8fe47bf304..a06b56f861 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -28,7 +28,7 @@ from uuid import uuid4from marimo import _loggersfrom marimo._ast.cell import CellConfig, CellImpl-from marimo._ast.compiler import compile_cell+from marimo._ast.compiler import _build_source_position_map, compile_cellfrom marimo._ast.errors import ImportStarErrorfrom marimo._ast.names import SETUP_CELL_NAMEfrom marimo._ast.variables import BUILTINS, is_local@@ -158,6 +158,7 @@ from marimo._runtime.context.kernel_context import (initialize_kernel_context,)from marimo._runtime.context.types import teardown_context+from marimo._runtime.context.utils import get_modefrom marimo._runtime.control_flow import MarimoInterruptfrom marimo._runtime.input_override import getpass_override, input_overridefrom marimo._runtime.packages.import_error_extractors import (@@ -850,10 +851,17 @@ class Kernel:) -> tuple[Optional[CellImpl], Optional[Error]]:error: Optional[Error] = Nonetry:+ # In run mode or debugpy, pass the notebook filename so+ # tracebacks reference the real file instead of synthetic+ # cell files.+ filename = None+ if get_mode() == "run" or os.environ.get("DEBUGPY_RUNNING"):+ filename = self.app_metadata.filenamecell = compile_cell(code,cell_id=cell_id,carried_imports=carried_imports,+ filename=filename,)except Exception as e:cell = None@@ -1163,6 +1171,11 @@ class Kernel:"""LOGGER.debug("Mutating graph.")LOGGER.debug("Current set of errors: %s", self.errors)+ # Invalidate the cached notebook→source position map so that+ # recompiled cells pick up the current file contents. This+ # matters for debugpy (edit mode) and `marimo run --watch`+ # where cells are recompiled after the file changes on disk.+ _build_source_position_map.cache_clear()cells_before_mutation = set(self.graph.cells.keys())cells_with_errors_before_mutation = set(self.errors.keys())cells_starting_stale = (diff --git a/tests/_runtime/test_trace.py b/tests/_runtime/test_trace.pyindex 71a708f131..5d198f6359 100644--- a/tests/_runtime/test_trace.py+++ b/tests/_runtime/test_trace.py@@ -6,6 +6,7 @@ import reimport subprocessimport sysimport textwrap+from typing import TYPE_CHECKINGfrom marimo._runtime.commands import (ExecuteCellCommand,@@ -14,6 +15,11 @@ from marimo._runtime.runtime import Kernelfrom marimo._utils import async_pathfrom tests._messaging.mocks import MockStderr, MockStream+if TYPE_CHECKING:+ from pathlib import Path++ from tests.conftest import MockedKernel+class TestScriptTrace:@staticmethod@@ -288,6 +294,149 @@ class TestAppTrace:assert "NameError" in stderr_messages.messages[-1]+class TestRunModeTrace:+ @staticmethod+ async def test_run_mode_trace_references_real_file(+ run_mode_kernel: MockedKernel,+ tmp_path: Path,+ ) -> None:+ """In run mode, tracebacks should reference the real notebook file."""+ # Create a real notebook file so solve_source_position can match+ notebook_code = textwrap.dedent("""\+ import marimo++ __generated_with = "0.0.0"+ app = marimo.App()+++ @app.cell+ def _():+ x = 1+ return (x,)+++ @app.cell+ def _(x):+ y = x + 1+ raise ValueError("boom")+ return (y,)+++ if __name__ == "__main__":+ app.run()+ """)+ notebook_file = tmp_path / "test_notebook.py"+ notebook_file.write_text(notebook_code)++ k = run_mode_kernel.k+ k.app_metadata.filename = str(notebook_file)++ cell_code = textwrap.dedent(+ """+ x = 1+ """+ )+ await k.run(+ [+ ExecuteCellCommand(cell_id="0", code=cell_code),+ ExecuteCellCommand(+ cell_id="1",+ code=textwrap.dedent(+ """+ y = x + 1+ raise ValueError("boom")+ """+ ),+ ),+ ]+ )++ tag_re = re.compile(r"(<!--.*?-->|<[^>]*>)")++ # Collect all output: stderr messages and stream operations+ all_output = "\n".join(k.stderr.messages)+ stream_messages = MockStream(k.stream)+ for op in stream_messages.operations:+ all_output += "\n" + str(op)++ result = tag_re.sub("", all_output)++ assert "ValueError" in result or "boom" in result+ # Should reference the real notebook file, NOT __marimo__cell_+ assert "__marimo__cell_" not in result+ assert "test_notebook.py" in result++ @staticmethod+ async def test_run_mode_watch_invalidates_cache(+ run_mode_kernel: MockedKernel,+ tmp_path: Path,+ ) -> None:+ """Source position cache is invalidated on recompilation (--watch)."""+ from marimo._ast.compiler import _build_source_position_map++ notebook_v1 = textwrap.dedent("""\+ import marimo++ __generated_with = "0.0.0"+ app = marimo.App()+++ @app.cell+ def _():+ x = 1+ return (x,)+++ if __name__ == "__main__":+ app.run()+ """)+ notebook_file = tmp_path / "watch_notebook.py"+ notebook_file.write_text(notebook_v1)++ k = run_mode_kernel.k+ k.app_metadata.filename = str(notebook_file)++ # First compile — populates cache+ await k.run([ExecuteCellCommand(cell_id="0", code="x = 1")])+ cached = _build_source_position_map(str(notebook_file))+ assert len(cached) == 1++ # Simulate file change (--watch): add a second cell+ notebook_v2 = textwrap.dedent("""\+ import marimo++ __generated_with = "0.0.0"+ app = marimo.App()+++ @app.cell+ def _():+ x = 1+ return (x,)+++ @app.cell+ def _(x):+ y = x + 2+ return (y,)+++ if __name__ == "__main__":+ app.run()+ """)+ notebook_file.write_text(notebook_v2)++ # Second compile — mutate_graph should clear cache+ await k.run(+ [+ ExecuteCellCommand(cell_id="0", code="x = 1"),+ ExecuteCellCommand(cell_id="1", code="y = x + 2"),+ ]+ )+ refreshed = _build_source_position_map(str(notebook_file))+ assert len(refreshed) == 2++class TestEmbedTrace:@staticmethodasync def test_embed_trace(