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: empty notebooks should have empty venvs in sandboxes (#3456)

Akshay Agrawal0cd10738fedb
diff --git a/marimo/_cli/sandbox.py b/marimo/_cli/sandbox.py
index c8309fa9df..e5990e9998 100644
--- a/marimo/_cli/sandbox.py
+++ b/marimo/_cli/sandbox.py
@@ -248,13 +248,7 @@ def _normalize_sandbox_dependencies(
return filtered + [chosen]
-def run_in_sandbox(
- args: List[str],
- name: Optional[str] = None,
-) -> int:
- if not DependencyManager.which("uv"):
- raise click.UsageError("uv must be installed to use --sandbox")
-
+def construct_uv_command(args: list[str], name: str | None) -> list[str]:
cmd = ["marimo"] + args
if "--sandbox" in cmd:
cmd.remove("--sandbox")
@@ -264,6 +258,12 @@ def run_in_sandbox(
get_dependencies_from_filename(name) if name is not None else []
)
+ # If there are no dependencies, which can happen for marimo new or
+ # on marimo edit a_new_file.py, uv may use a cached venv, even though
+ # we are passing --isolated; `--refresh` ensures that the venv is
+ # actually ephemeral.
+ uv_needs_refresh = not dependencies
+
# Normalize marimo dependencies
dependencies = _normalize_sandbox_dependencies(dependencies, __version__)
@@ -288,23 +288,38 @@ def run_in_sandbox(
python_version = None
# Construct base UV command
- uv_cmd = [
- "uv",
- "run",
- "--isolated",
- # sandboxed notebook shouldn't pick up existing pyproject.toml,
- # which may conflict with the sandbox requirements
- "--no-project",
- "--with-requirements",
- temp_file_path,
- ]
+ uv_cmd = (
+ [
+ "uv",
+ "run",
+ "--isolated",
+ # sandboxed notebook shouldn't pick up existing pyproject.toml,
+ # which may conflict with the sandbox requirements
+ "--no-project",
+ "--with-requirements",
+ temp_file_path,
+ ]
+ + ["--refresh"]
+ if uv_needs_refresh
+ else []
+ )
# Add Python version if specified
if python_version:
uv_cmd.extend(["--python", python_version])
- # Final command assembly
- uv_cmd = uv_cmd + cmd
+ # Final command assembly: combine the uv prefix with the original marimo
+ # command.
+ return uv_cmd + cmd
+
+
+def run_in_sandbox(
+ args: list[str],
+ name: Optional[str] = None,
+) -> int:
+ if not DependencyManager.which("uv"):
+ raise click.UsageError("uv must be installed to use --sandbox")
+ uv_cmd = construct_uv_command(args, name)
echo(f"Running in a sandbox: {muted(' '.join(uv_cmd))}")
diff --git a/tests/_cli/test_sandbox.py b/tests/_cli/test_sandbox.py
index a5780869e4..77952e2763 100644
--- a/tests/_cli/test_sandbox.py
+++ b/tests/_cli/test_sandbox.py
@@ -9,6 +9,7 @@ from marimo._cli.sandbox import (
_normalize_sandbox_dependencies,
_pyproject_toml_to_requirements_txt,
_read_pyproject,
+ construct_uv_command,
get_dependencies_from_filename,
)
@@ -308,3 +309,33 @@ def test_is_marimo_dependency():
assert not _is_marimo_dependency("pandas")
assert not _is_marimo_dependency("marimo-ai")
assert not _is_marimo_dependency("marimo-ai==0.1.0")
+
+
+def test_construct_uv_cmd_marimo_new() -> None:
+ uv_cmd = construct_uv_command(["new"], None)
+ assert "--refresh" in uv_cmd
+
+
+def test_construct_uv_cmd_marimo_edit_empty_file() -> None:
+ # a file that doesn't yet exist
+ uv_cmd = construct_uv_command(["edit", "foo_123.py"], "foo_123.py")
+ assert "--refresh" in uv_cmd
+
+
+def test_construct_uv_cmd_marimo_edit_file_no_sandbox(
+ temp_marimo_file: str,
+) -> None:
+ # a file that has no inline metadata yet
+ uv_cmd = construct_uv_command(["edit", temp_marimo_file], temp_marimo_file)
+ assert "--refresh" in uv_cmd
+
+
+def test_construct_uv_cmd_marimo_edit_sandboxed_file(
+ temp_sandboxed_marimo_file: str,
+) -> None:
+ # a file that has inline metadata; shouldn't refresh the cache, uv
+ # --isolated will do the right thing.
+ uv_cmd = construct_uv_command(
+ ["edit", temp_sandboxed_marimo_file], temp_sandboxed_marimo_file
+ )
+ assert "--refresh" not in uv_cmd
diff --git a/tests/conftest.py b/tests/conftest.py
index c7b5c980a5..dec4f77805 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -261,6 +261,49 @@ def temp_marimo_file() -> Generator[str, None, None]:
tmp_dir.cleanup()
+def temp_sandboxed_marimo_file() -> Generator[str, None, None]:
+ tmp_dir = TemporaryDirectory()
+ tmp_file = os.path.join(tmp_dir.name, "notebook.py")
+ content = inspect.cleandoc(
+ """
+ # Copyright 2024 Marimo. All rights reserved.
+ # /// script
+ # requires-python = ">=3.11"
+ # dependencies = [
+ # "polars",
+ # "marimo>=0.8.0",
+ # "quak",
+ # "vega-datasets",
+ # ]
+ # ///
+
+ import marimo
+ app = marimo.App()
+
+ @app.cell
+ def __():
+ import marimo as mo
+ return mo,
+
+ @app.cell
+ def __(mo):
+ slider = mo.ui.slider(0, 10)
+ return slider,
+
+ if __name__ == "__main__":
+ app.run()
+ """
+ )
+
+ try:
+ with open(tmp_file, "w") as f:
+ f.write(content)
+ yield tmp_file
+ finally:
+ tmp_dir.cleanup()
+
+
@pytest.fixture
def temp_async_marimo_file() -> Generator[str, None, None]:
tmp_dir = TemporaryDirectory()