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: resolve notebook paths when running cells with pytest (#10864)

Myles Scolnick7322384e20b9
diff --git a/marimo/_ast/app.py b/marimo/_ast/app.py
index ac5a4f49ec..3cd20f9a59 100644
--- a/marimo/_ast/app.py
+++ b/marimo/_ast/app.py
@@ -50,6 +50,7 @@ from marimo._runtime.commands import (
InvokeFunctionCommand,
UpdateUIElementCommand,
)
+from marimo._runtime.context.filename import notebook_filename
from marimo._runtime.context.types import (
ContextNotInitializedError,
get_context,
@@ -780,9 +781,10 @@ class App:
from marimo._runtime.runner import by_refs
self._maybe_initialize()
- output, defs = await by_refs.run_cell_async(
- self._graph, cell._cell.cell_id, kwargs
- )
+ with notebook_filename(self._filename):
+ output, defs = await by_refs.run_cell_async(
+ self._graph, cell._cell.cell_id, kwargs
+ )
return output, _Namespace(defs, owner=self)
def _run_cell_sync(
@@ -791,9 +793,10 @@ class App:
from marimo._runtime.runner import by_refs
self._maybe_initialize()
- output, defs = by_refs.run_cell_sync(
- self._graph, cell._cell.cell_id, kwargs
- )
+ with notebook_filename(self._filename):
+ output, defs = by_refs.run_cell_sync(
+ self._graph, cell._cell.cell_id, kwargs
+ )
return output, _Namespace(defs, owner=self)
async def _set_ui_element_value(
diff --git a/marimo/_ast/pytest.py b/marimo/_ast/pytest.py
index 51693937a9..c8c3a7bbd1 100644
--- a/marimo/_ast/pytest.py
+++ b/marimo/_ast/pytest.py
@@ -13,6 +13,7 @@ from typing import TYPE_CHECKING, Any, NoReturn, TypeVar, cast
from marimo._ast.cell import Cell
from marimo._ast.fast_stack import fast_stack
from marimo._ast.parse import ast_parse
+from marimo._runtime.context.filename import notebook_filename
if TYPE_CHECKING:
from collections.abc import Mapping
@@ -221,6 +222,7 @@ def _make_hook(
tuple[Any, Mapping[str, Any]]
| Awaitable[tuple[Any, Mapping[str, Any]]],
],
+ file: str,
use_wrapped: bool = False,
is_async: bool = False,
) -> Callable[..., Any]:
@@ -229,34 +231,43 @@ def _make_hook(
if is_async:
async def _async_hook(*args: Any, **kwargs: Any) -> Any:
- res = run()
- if isinstance(res, Awaitable):
- _, cell_defs = await res
- else:
- _, cell_defs = res
-
- target = (
- cell_defs[var].__wrapped__ if use_wrapped else cell_defs[var]
- )
- return await target(*args, **kwargs)
+ with notebook_filename(file):
+ res = run()
+ if isinstance(res, Awaitable):
+ _, cell_defs = await res
+ else:
+ _, cell_defs = res
+
+ target = (
+ cell_defs[var].__wrapped__
+ if use_wrapped
+ else cell_defs[var]
+ )
+ return await target(*args, **kwargs)
return _async_hook
else:
def _hook(*args: Any, **kwargs: Any) -> Any:
- res = run()
- if isinstance(res, Awaitable):
- import asyncio
-
- loop = asyncio.new_event_loop()
- _, cell_defs = loop.run_until_complete(res)
- else:
- _, cell_defs = res
-
- target = (
- cell_defs[var].__wrapped__ if use_wrapped else cell_defs[var]
- )
- return target(*args, **kwargs)
+ with notebook_filename(file):
+ res = run()
+ if isinstance(res, Awaitable):
+ import asyncio
+
+ loop = asyncio.new_event_loop()
+ try:
+ _, cell_defs = loop.run_until_complete(res)
+ finally:
+ loop.close()
+ else:
+ _, cell_defs = res
+
+ target = (
+ cell_defs[var].__wrapped__
+ if use_wrapped
+ else cell_defs[var]
+ )
+ return target(*args, **kwargs)
return _hook
@@ -271,7 +282,9 @@ def _build_hook(
) -> Callable[..., Any]:
"""Build hook for test or fixture function."""
is_async = isinstance(test, ast.AsyncFunctionDef)
- hook = _make_hook(var, run, use_wrapped=is_fixture, is_async=is_async)
+ hook = _make_hook(
+ var, run, file, use_wrapped=is_fixture, is_async=is_async
+ )
stub_fn = build_stub_fn(test, file)
functools.wraps(stub_fn)(hook)
diff --git a/marimo/_runtime/context/filename.py b/marimo/_runtime/context/filename.py
new file mode 100644
index 0000000000..7366e4e4f1
--- /dev/null
+++ b/marimo/_runtime/context/filename.py
@@ -0,0 +1,28 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+from contextlib import contextmanager
+from contextvars import ContextVar
+from pathlib import Path
+from typing import TYPE_CHECKING
+
+from marimo._utils.paths import normalize_path
+
+if TYPE_CHECKING:
+ from collections.abc import Iterator
+
+NOTEBOOK_FILENAME: ContextVar[str | None] = ContextVar(
+ "marimo_notebook_filename", default=None
+)
+
+
+@contextmanager
+def notebook_filename(filename: str | None) -> Iterator[None]:
+ """Track the notebook source during cell and pytest execution."""
+ # Resolve before user code can change the working directory.
+ filename = str(normalize_path(Path(filename))) if filename else None
+ token = NOTEBOOK_FILENAME.set(filename)
+ try:
+ yield
+ finally:
+ NOTEBOOK_FILENAME.reset(token)
diff --git a/marimo/_runtime/runner/by_refs.py b/marimo/_runtime/runner/by_refs.py
index 4bd9ca02f0..652c57d7e1 100644
--- a/marimo/_runtime/runner/by_refs.py
+++ b/marimo/_runtime/runner/by_refs.py
@@ -10,6 +10,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Any
+from marimo._runtime.context.filename import NOTEBOOK_FILENAME
from marimo._runtime.control_flow import MarimoStopError
from marimo._runtime.exceptions import MarimoRuntimeException
from marimo._runtime.executor import (
@@ -126,7 +127,8 @@ async def run_cell_async(
ancestor_ids = _get_ancestors(graph, cell_impl, refs)
evaluator = _new_evaluator()
- glbls: MutableGlobals = {}
+ filename = NOTEBOOK_FILENAME.get()
+ glbls: MutableGlobals = {"__file__": filename} if filename else {}
for cid in topological_sort(graph, ancestor_ids):
stop = _classify(await evaluator.evaluate(graph.cells[cid], glbls))
if stop is not None:
@@ -174,7 +176,8 @@ def run_cell_sync(
)
evaluator = _new_evaluator()
- glbls: MutableGlobals = {}
+ filename = NOTEBOOK_FILENAME.get()
+ glbls: MutableGlobals = {"__file__": filename} if filename else {}
for cid in topological_sort(graph, ancestor_ids):
stop = _classify(evaluator.evaluate_sync(graph.cells[cid], glbls))
if stop is not None:
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index ccead97387..0bce620461 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -120,6 +120,7 @@ from marimo._runtime.context import (
ExecutionContext,
get_context,
)
+from marimo._runtime.context.filename import NOTEBOOK_FILENAME
from marimo._runtime.context.kernel_context import (
KernelRuntimeContext,
)
@@ -365,12 +366,13 @@ def notebook_dir() -> pathlib.Path | None:
try:
ctx = get_context()
except ContextNotInitializedError:
- # If we are not running in a notebook (e.g. exported to Jupyter),
- # return the current working directory
- return pathlib.Path().cwd()
-
- # NB: __file__ is patched by runner, so always bound to be correct.
- filename = ctx.globals.get("__file__", None) or ctx.filename
+ filename = NOTEBOOK_FILENAME.get()
+ if filename is None:
+ # Outside a notebook (e.g. exported to Jupyter), use the cwd.
+ return pathlib.Path.cwd()
+ else:
+ # NB: __file__ is patched by runner, so always bound to be correct.
+ filename = ctx.globals.get("__file__", None) or ctx.filename
if filename is not None:
path = normalize_path(pathlib.Path(filename))
while not path.is_dir():
diff --git a/tests/_ast/test_cell.py b/tests/_ast/test_cell.py
index 0951862eeb..da2432e268 100644
--- a/tests/_ast/test_cell.py
+++ b/tests/_ast/test_cell.py
@@ -1,19 +1,177 @@
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations
+import asyncio
import logging
import os
import sys
from collections.abc import Awaitable
+from pathlib import Path
import pytest
-from marimo import _loggers
+from marimo import _loggers, notebook_dir
from marimo._ast.app import App
from marimo._ast.cell import CellConfig
+from marimo._runtime.context import runtime_context_installed
+from marimo._runtime.context.filename import notebook_filename
+from marimo._runtime.exceptions import MarimoRuntimeException
class TestCellRun:
+ @staticmethod
+ @pytest.mark.parametrize("relative", [False, True])
+ def test_cell_location_after_chdir(
+ tmp_path, monkeypatch, relative
+ ) -> None:
+ monkeypatch.chdir(tmp_path)
+ directory = tmp_path / "notebooks with spaces"
+ directory.mkdir()
+ filename = directory / "notebook.py"
+ app = App(
+ _filename=str(
+ filename.relative_to(tmp_path) if relative else filename
+ )
+ )
+
+ @app.cell
+ def location():
+ import os
+
+ import marimo as mo
+
+ os.chdir("notebooks with spaces")
+ paths = (__file__, mo.notebook_dir(), mo.notebook_location())
+ return (paths,)
+
+ _, defs = location.run()
+ assert defs["paths"] == (str(filename), directory, directory)
+
+ @staticmethod
+ @pytest.mark.parametrize("fail", [False, True])
+ def test_nested_cell_locations(tmp_path, fail) -> None:
+ outer_dir = tmp_path / "outer"
+ inner_dir = tmp_path / "inner"
+ outer_dir.mkdir()
+ inner_dir.mkdir()
+ outer_app = App(_filename=str(outer_dir / "notebook.py"))
+ inner_app = App(_filename=str(inner_dir / "notebook.py"))
+
+ @inner_app.cell
+ def inner(fail):
+ import marimo as mo
+
+ directory = mo.notebook_dir()
+ if fail:
+ raise ValueError("inner cell failed")
+ return (directory,)
+
+ @outer_app.cell
+ def outer(fail, inner):
+ import marimo as mo
+
+ before = mo.notebook_dir()
+ try:
+ _, inner_defs = inner.run(fail=fail)
+ inside = inner_defs["directory"]
+ except ValueError:
+ inside = None
+ paths = (before, inside, mo.notebook_dir())
+ return (paths,)
+
+ _, defs = outer.run(fail=fail, inner=inner)
+ assert defs["paths"] == (
+ outer_dir,
+ None if fail else inner_dir,
+ outer_dir,
+ )
+ assert notebook_dir() == Path.cwd()
+
+ @staticmethod
+ async def test_concurrent_cell_locations(tmp_path) -> None:
+ release = asyncio.Event()
+ entered = [asyncio.Event(), asyncio.Event()]
+
+ async def location(entered, release):
+ import marimo as mo
+
+ before = mo.notebook_dir()
+ entered.set()
+ await release.wait()
+ paths = (before, mo.notebook_dir())
+ return (paths,)
+
+ directories = [tmp_path / "first", tmp_path / "second"]
+ tasks = []
+ for directory, ready in zip(directories, entered, strict=True):
+ directory.mkdir()
+ app = App(_filename=str(directory / "notebook.py"))
+ cell = app.cell(location)
+ tasks.append(
+ asyncio.create_task(cell.run(entered=ready, release=release))
+ )
+ try:
+ await asyncio.wait_for(
+ asyncio.gather(*(event.wait() for event in entered)), timeout=5
+ )
+ finally:
+ release.set()
+ results = await asyncio.gather(*tasks)
+ assert [defs["paths"] for _, defs in results] == [
+ (directory, directory) for directory in directories
+ ]
+
+ @staticmethod
+ async def test_cancelled_cell_restores_location(tmp_path) -> None:
+ app = App()
+
+ @app.cell
+ async def location():
+ import asyncio
+
+ await asyncio.sleep(0)
+ raise asyncio.CancelledError
+
+ with notebook_filename(str(tmp_path / "outer.py")):
+ with pytest.raises(asyncio.CancelledError):
+ await location.run()
+ assert notebook_dir() == tmp_path
+ assert notebook_dir() == Path.cwd()
+
+ @staticmethod
+ @pytest.mark.parametrize("fail", [False, True])
+ async def test_async_cell_notebook_location(fail: bool) -> None:
+ app = App()
+ main_module = sys.modules["__main__"]
+
+ @app.cell
+ async def location(fail):
+ import asyncio
+
+ import marimo as mo
+
+ await asyncio.sleep(0)
+ paths = (__file__, mo.notebook_dir(), mo.notebook_location())
+ if fail:
+ raise ValueError("cell failed")
+ return (paths,)
+
+ result = location.run(fail=fail)
+ assert isinstance(result, Awaitable)
+ if fail:
+ with pytest.raises(MarimoRuntimeException) as exc_info:
+ await result
+ assert isinstance(exc_info.value.__cause__, ValueError)
+ else:
+ _, defs = await result
+ assert defs["paths"] == (
+ __file__,
+ Path(__file__).parent,
+ Path(__file__).parent,
+ )
+ assert not runtime_context_installed()
+ assert sys.modules["__main__"] is main_module
+
@staticmethod
def test_cell_basic() -> None:
def f() -> tuple[int]:
diff --git a/tests/_runtime/test_notebook_filename.py b/tests/_runtime/test_notebook_filename.py
new file mode 100644
index 0000000000..86c3014420
--- /dev/null
+++ b/tests/_runtime/test_notebook_filename.py
@@ -0,0 +1,47 @@
+from __future__ import annotations
+
+from pathlib import Path
+
+import pytest
+
+import marimo as mo
+from marimo._runtime.context import get_context
+from marimo._runtime.context.filename import notebook_filename
+
+
[email protected]("filename", [None, ""])
+def test_unknown_filename_uses_cwd(filename: str | None) -> None:
+ with notebook_filename(filename):
+ assert (mo.notebook_dir(), mo.notebook_location()) == (
+ Path.cwd(),
+ Path.cwd(),
+ )
+
+
+def test_filename_preserves_symlinks(tmp_path: Path) -> None:
+ directory = tmp_path / "original"
+ directory.mkdir()
+ alias = tmp_path / "alias"
+ try:
+ alias.symlink_to(directory, target_is_directory=True)
+ except (OSError, NotImplementedError):
+ pytest.skip("Creating symlinks is not supported")
+
+ with notebook_filename(str(alias / "notebook.py")):
+ assert (mo.notebook_dir(), mo.notebook_location()) == (alias, alias)
+
+
+def test_filename_does_not_override_kernel_context(
+ execution_kernel, tmp_path: Path
+) -> None:
+ del execution_kernel
+ context = get_context()
+ expected = (mo.notebook_dir(), mo.notebook_location(), mo.app_meta().mode)
+ with notebook_filename(str(tmp_path / "other.py")):
+ assert get_context() is context
+ assert (
+ mo.notebook_dir(),
+ mo.notebook_location(),
+ mo.app_meta().mode,
+ ) == expected
+ assert get_context() is context
diff --git a/tests/_runtime/test_pytest_runtime.py b/tests/_runtime/test_pytest_runtime.py
index f1dce7353b..9debb87cbe 100644
--- a/tests/_runtime/test_pytest_runtime.py
+++ b/tests/_runtime/test_pytest_runtime.py
@@ -310,9 +310,134 @@ def test_pytest_result_summary_includes_xfail() -> None:
assert "XPassed: 1" in result.summary
+def test_offline_notebook_location(tmp_path: Path) -> None:
+ import subprocess
+
+ notebook_dir = tmp_path / "notebooks"
+ notebook_dir.mkdir()
+ notebook = notebook_dir / "test_location.py"
+ notebook.write_text(
+ """
+import marimo
+app = marimo.App()
+
+with app.setup:
+ import pytest
+
+def imports():
+ import marimo as mo
+ from pathlib import Path
+ return mo, Path
+
+def location(mo):
+ directory = mo.notebook_dir()
+ return (directory,)
+
+def test_cell_location(Path, directory, mo):
+ assert directory == Path(__file__).parent
+ assert mo.notebook_location() == directory
+ assert directory.name == "notebooks"
+
+def _(Path, directory, mo):
+ @pytest.fixture
+ def location_fixture():
+ assert mo.notebook_dir() == Path(__file__).parent
+ return mo.notebook_location()
+
+ def test_location(location_fixture):
+ assert mo.notebook_dir() == directory == location_fixture
+ assert directory.name == "notebooks"
+
+ @pytest.mark.parametrize("change_cwd", [False, True])
+ def test_location_after_chdir(change_cwd, monkeypatch, tmp_path):
+ if change_cwd:
+ monkeypatch.chdir(tmp_path)
+ assert mo.notebook_dir() == Path(__file__).parent
+ assert mo.app_meta().mode == "test"
+
+ @pytest.mark.xfail(raises=ValueError, strict=True)
+ def test_failure():
+ assert mo.notebook_dir() == Path(__file__).parent
+ raise ValueError("expected failure")
+
+ @pytest.mark.asyncio
+ async def test_async_location():
+ import asyncio
+ await asyncio.sleep(0)
+ assert mo.notebook_location() == Path(__file__).parent
+ assert directory.name == "notebooks"
+
+ class TestLocation:
+ def test_location(self):
+ assert mo.notebook_dir() == Path(__file__).parent
+ assert directory.name == "notebooks"
+
+def test_context_restored():
+ from pathlib import Path
+ from marimo._runtime.context import runtime_context_installed
+ assert not runtime_context_installed()
+ assert marimo.notebook_dir() == Path.cwd()
+ assert marimo.app_meta().mode == "test"
+"""
+ )
+ env = {k: v for k, v in os.environ.items() if k != "PYTEST_CURRENT_TEST"}
+ proc = subprocess.run(
+ [
+ sys.executable,
+ "-m",
+ "pytest",
+ str(notebook),
+ "-q",
+ "-p",
+ "no:inline_snapshot",
+ ],
+ capture_output=True,
+ text=True,
+ cwd=tmp_path,
+ env=env,
+ )
+ out = proc.stdout + proc.stderr
+ assert proc.returncode == 0, out
+ assert "7 passed, 1 xfailed" in out
+
+
def test_pytest_result_summary_omits_zero_xfail() -> None:
from marimo._runtime.pytest import MarimoPytestResult
result = MarimoPytestResult(passed=5, failed=0, errors=0, skipped=0)
assert "XFailed" not in result.summary
assert "XPassed" not in result.summary
+
+
[email protected]("failure", [None, "cell", "test"])
+def test_sync_hook_closes_event_loop(monkeypatch, failure: str | None) -> None:
+ from marimo._ast.pytest import _make_hook
+
+ loop = asyncio.new_event_loop()
+ monkeypatch.setattr(asyncio, "new_event_loop", lambda: loop)
+
+ def test_function():
+ if failure == "test":
+ raise ValueError("test failed")
+ return "test result"
+
+ async def run_cell():
+ await asyncio.sleep(0)
+ if failure == "cell":
+ raise ValueError("cell failed")
+ return None, {"test_function": test_function}
+
+ hook = _make_hook("test_function", run_cell, __file__)
+ try:
+ if failure:
+ with pytest.raises(ValueError, match=f"{failure} failed"):
+ hook()
+ else:
+ assert hook() == "test result"
+ assert loop.is_closed()
+ finally:
+ loop.close()