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: marimo edit --sandbox no longer creates new files (#3181)

Myles Scolnickc9f0a7f5ae34
diff --git a/marimo/_cli/sandbox.py b/marimo/_cli/sandbox.py
index b69b3a8360..f1c8defa25 100644
--- a/marimo/_cli/sandbox.py
+++ b/marimo/_cli/sandbox.py
@@ -106,6 +106,8 @@ def _pyproject_toml_to_requirements_txt(
def get_dependencies_from_filename(name: str) -> List[str]:
+ if not name or not os.path.exists(name):
+ return []
try:
contents, _ = FileContentReader().read_file(name)
return _get_dependencies(contents) or []
@@ -239,7 +241,7 @@ def run_in_sandbox(
atexit.register(lambda: os.unlink(temp_file_path))
# Get Python version requirement if available
- if name is not None:
+ if name is not None and os.path.exists(name):
contents, _ = FileContentReader().read_file(name)
pyproject = _read_pyproject(contents)
python_version = (
diff --git a/tests/_cli/test_cli.py b/tests/_cli/test_cli.py
index ed7a0880fb..f22db96f73 100644
--- a/tests/_cli/test_cli.py
+++ b/tests/_cli/test_cli.py
@@ -549,6 +549,27 @@ def test_cli_sandbox_edit(temp_marimo_file: str) -> None:
_check_contents(p, b"marimo-mode data-mode='edit'", contents)
[email protected](not HAS_UV, reason="uv is required for sandbox tests")
+def test_cli_sandbox_edit_new_file() -> None:
+ port = _get_port()
+ d = tempfile.TemporaryDirectory()
+ path = os.path.join(d.name, "new_sandbox_file.py")
+ p = subprocess.Popen(
+ [
+ "marimo",
+ "edit",
+ path,
+ "-p",
+ str(port),
+ "--headless",
+ "--no-token",
+ "--sandbox",
+ ]
+ )
+ contents = _try_fetch(port)
+ _check_contents(p, b"marimo-mode data-mode='edit'", contents)
+
+
@pytest.mark.skipif(not HAS_UV, reason="uv is required for sandbox tests")
def test_cli_sandbox_run(temp_marimo_file: str) -> None:
port = _get_port()
diff --git a/tests/_cli/test_sandbox.py b/tests/_cli/test_sandbox.py
index 795cdfbec8..be11bfd0cd 100644
--- a/tests/_cli/test_sandbox.py
+++ b/tests/_cli/test_sandbox.py
@@ -7,6 +7,7 @@ from marimo._cli.sandbox import (
_get_python_version_requirement,
_pyproject_toml_to_requirements_txt,
_read_pyproject,
+ get_dependencies_from_filename,
)
@@ -222,3 +223,11 @@ import marimo
assert pyproject_no_python is not None
assert _get_python_version_requirement(pyproject_no_python) is None
assert _get_dependencies(SCRIPT_NO_PYTHON) == ["polars"]
+
+
+def test_get_dependencies_with_nonexistent_file():
+ # Test with a non-existent file
+ assert get_dependencies_from_filename("nonexistent_file.py") == []
+
+ # Test with None
+ assert get_dependencies_from_filename(None) == [] # type: ignore