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: duplicate notebook when running 'marimo edit <folder>' (#7841)

Myles Scolnick1249c47754d2
diff --git a/marimo/_server/api/endpoints/files.py b/marimo/_server/api/endpoints/files.py
index d7080670db..c75dbbb1c5 100644
--- a/marimo/_server/api/endpoints/files.py
+++ b/marimo/_server/api/endpoints/files.py
@@ -202,9 +202,11 @@ async def copy(
body = await parse_request(request, cls=CopyNotebookRequest)
# Resolve relative filenames against the file router's directory
- if body.destination and not Path(body.destination).is_absolute():
- directory = app_state.session_manager.file_router.directory
- if directory:
+ directory = app_state.session_manager.file_router.directory
+ if directory:
+ if body.source and not Path(body.source).is_absolute():
+ body.source = str(Path(directory) / body.source)
+ if body.destination and not Path(body.destination).is_absolute():
body.destination = str(Path(directory) / body.destination)
session = app_state.require_current_session()
diff --git a/marimo/_server/models/models.py b/marimo/_server/models/models.py
index d82eb42e2a..21744f3ea1 100644
--- a/marimo/_server/models/models.py
+++ b/marimo/_server/models/models.py
@@ -1,7 +1,6 @@
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations
-import os
from typing import Any, Literal, Optional
import msgspec
@@ -277,19 +276,6 @@ class CopyNotebookRequest(msgspec.Struct, rename="camel"):
source: str
destination: str
- # Validate filenames are valid, and destination path does not already exist
- def __post_init__(self) -> None:
- destination = os.path.basename(self.destination)
- assert self.source is not None
- assert self.destination is not None
- assert os.path.exists(self.source), (
- f'File "{self.source}" does not exist.'
- + "Please save the notebook and try again."
- )
- assert not os.path.exists(self.destination), (
- f'File "{destination}" already exists in this directory.'
- )
-
class SaveAppConfigurationRequest(msgspec.Struct, rename="camel"):
# partial app config
diff --git a/tests/_server/api/endpoints/test_files.py b/tests/_server/api/endpoints/test_files.py
index ae9e8861ee..c3368949c2 100644
--- a/tests/_server/api/endpoints/test_files.py
+++ b/tests/_server/api/endpoints/test_files.py
@@ -344,6 +344,52 @@ def test_copy_file(client: TestClient) -> None:
try_assert_n_times(5, _assert_contents)
+@with_session(SESSION_ID)
+def test_copy_file_with_relative_paths(client: TestClient) -> None:
+ """Test that copy works with relative paths when file_router has a directory."""
+ filename = get_session_manager(client).file_router.get_unique_file_key()
+ assert filename
+ path = Path(filename)
+ assert path.exists()
+ file_contents = path.read_text()
+ assert "import marimo as mo" in file_contents
+
+ # Get the directory and mock the file_router.directory property to simulate
+ # running from a subdirectory (marimo edit foo/dir/)
+ directory = str(path.parent)
+ file_router = get_session_manager(client).file_router
+
+ # Use relative paths (as the frontend would send when running from a
+ # subdirectory)
+ source_relative = path.name
+ dest_relative = f"_copy_{path.name}"
+ copied_file = path.parent / dest_relative
+
+ # Mock the directory property on the file router
+ with patch.object(
+ type(file_router),
+ "directory",
+ new_callable=lambda: property(lambda _: directory),
+ ):
+ response = client.post(
+ "/api/kernel/copy",
+ headers=HEADERS,
+ json={
+ "source": source_relative,
+ "destination": dest_relative,
+ },
+ )
+ assert response.status_code == 200, response.text
+ assert dest_relative in response.text
+
+ def _assert_contents():
+ file_contents = copied_file.read_text()
+ assert "import marimo as mo" in file_contents
+ assert 'marimo.App(width="full"' in file_contents
+
+ try_assert_n_times(5, _assert_contents)
+
+
@with_websocket_session(SESSION_ID)
def test_rename_propagates(
client: TestClient, websocket: WebSocketTestSession