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: watch for file move with watchdog and Claude Code (#6798)

Myles Scolnick4c010f1b0247
diff --git a/marimo/_server/sessions.py b/marimo/_server/sessions.py
index 32d3d72ede..6e6b84891e 100644
--- a/marimo/_server/sessions.py
+++ b/marimo/_server/sessions.py
@@ -1199,6 +1199,11 @@ class SessionFileChangeHandler:
# Get the latest codes
codes = list(session.app_file_manager.app.cell_manager.codes())
cell_ids = list(session.app_file_manager.app.cell_manager.cell_ids())
+
+ LOGGER.info(
+ f"File changed: {file_path}. num_cell_ids: {len(cell_ids)}, num_codes: {len(codes)}, changed_cell_ids: {changed_cell_ids}"
+ )
+
# Send the updated cell ids and codes to the frontend
session.write_operation(
UpdateCellIdsRequest(cell_ids=cell_ids),
diff --git a/marimo/_utils/file_watcher.py b/marimo/_utils/file_watcher.py
index 7c6a98f07d..89a0dd1ce5 100644
--- a/marimo/_utils/file_watcher.py
+++ b/marimo/_utils/file_watcher.py
@@ -7,7 +7,7 @@ from abc import ABC, abstractmethod
from collections import defaultdict
from collections.abc import Coroutine
from pathlib import Path
-from typing import Any, Callable, Optional
+from typing import Callable, Optional
from marimo import _loggers
from marimo._dependencies.dependencies import DependencyManager
@@ -110,15 +110,36 @@ def _create_watchdog(
self.loop = loop
self.observer = watchdog.observers.Observer()
- def on_modified(self, event: Any) -> None:
+ def on_modified(
+ self,
+ event: watchdog.events.FileModifiedEvent
+ | watchdog.events.DirModifiedEvent,
+ ) -> None:
del event
self.loop.create_task(self.on_file_changed())
+ def on_moved(
+ self,
+ event: watchdog.events.FileMovedEvent
+ | watchdog.events.DirMovedEvent,
+ ) -> None:
+ # Handle editors that save by creating a temp file and moving it
+ # (e.g., Claude Code, some vim configurations)
+ dest_path_str = (
+ event.dest_path
+ if isinstance(event.dest_path, str)
+ else event.dest_path.decode("utf-8")
+ )
+
+ if self.path == Path(dest_path_str):
+ self.loop.create_task(self.on_file_changed())
+
def start(self) -> None:
event_handler = watchdog.events.PatternMatchingEventHandler( # type: ignore # noqa: E501
patterns=[str(self.path)]
)
event_handler.on_modified = self.on_modified # type: ignore
+ event_handler.on_moved = self.on_moved # type: ignore
self.observer.schedule( # type: ignore
event_handler,
str(self.path.parent),
diff --git a/tests/_server/test_sessions.py b/tests/_server/test_sessions.py
index 1d56f82fde..6f8534b356 100644
--- a/tests/_server/test_sessions.py
+++ b/tests/_server/test_sessions.py
@@ -15,8 +15,6 @@ from textwrap import dedent
from typing import Any, Callable, TypeVar
from unittest.mock import MagicMock
-import pytest
-
from marimo._ast.app import App, InternalApp
from marimo._ast.app_config import _AppConfig
from marimo._config.manager import (
@@ -438,10 +436,6 @@ def test_session_with_kiosk_consumers() -> None:
assert session.room.main_consumer is None
- sys.version_info < (3, 10),
- reason="This test is flaky on Python 3.9",
-)
@save_and_restore_main
async def test_session_manager_file_watching(tmp_path: Path) -> None:
# Create a temporary file
@@ -659,10 +653,6 @@ def test_watch_mode_does_not_override_config(tmp_path: Path) -> None:
session_manager_no_watch.shutdown()
- sys.version_info < (3, 10),
- reason="This test is flaky on Python 3.9",
-)
@save_and_restore_main
async def test_watch_mode_with_watcher_on_save_config() -> None:
"""Test that watch mode works correctly with watcher_on_save config."""
@@ -814,10 +804,6 @@ def __():
os.remove(tmp_path)
- sys.version_info < (3, 10),
- reason="This test is flaky on Python 3.9",
-)
@save_and_restore_main
async def test_session_manager_file_rename() -> None:
"""Test that file renaming works correctly with file watching."""
diff --git a/tests/_utils/test_async_path.py b/tests/_utils/test_async_path.py
index 7b95a86b1a..95e345fc81 100644
--- a/tests/_utils/test_async_path.py
+++ b/tests/_utils/test_async_path.py
@@ -1,6 +1,5 @@
import asyncio
import os
-import sys
import tempfile
from pathlib import Path
from unittest.mock import patch
@@ -416,10 +415,6 @@ class TestAsyncPathThreading:
class TestAsyncPathEdgeCases:
- @pytest.mark.skipif(
- sys.version_info < (3, 10),
- reason="Hardlink requires Python 3.10 or higher",
- )
async def test_hardlink_to(self):
"""Test hardlink_to creates hard link."""
with tempfile.TemporaryDirectory() as tmp:
@@ -436,10 +431,6 @@ class TestAsyncPathEdgeCases:
target_stat = await target.stat()
assert source_stat.st_ino == target_stat.st_ino
- @pytest.mark.skipif(
- sys.version_info < (3, 10),
- reason="Hardlink requires Python 3.10 or higher",
- )
async def test_write_text_with_newline(self):
"""Test write_text with custom newline parameter."""
with tempfile.TemporaryDirectory() as tmp:
diff --git a/tests/_utils/test_file_watcher.py b/tests/_utils/test_file_watcher.py
index a719eb335c..278d9d8d78 100644
--- a/tests/_utils/test_file_watcher.py
+++ b/tests/_utils/test_file_watcher.py
@@ -3,12 +3,13 @@ from __future__ import annotations
import asyncio
import os
-import sys
+import shutil
from pathlib import Path
from tempfile import NamedTemporaryFile
import pytest
+from marimo._dependencies.dependencies import DependencyManager
from marimo._utils.file_watcher import FileWatcherManager, PollingFileWatcher
@@ -45,10 +46,6 @@ async def test_polling_file_watcher() -> None:
assert callback_calls[0] == tmp_path
- sys.version_info < (3, 10),
- reason="File watcher tests require Python 3.10+",
-)
async def test_file_watcher_manager() -> None:
# Create two temporary files
with (
@@ -151,3 +148,60 @@ async def test_file_watcher_manager() -> None:
manager.stop_all()
os.remove(tmp_path1)
os.remove(tmp_path2)
+
+
+# This test is not working and watchdog makes CI hang in other areas
+# So we test this manually with `uv run --with=watchdog marimo edit nb.py --watch`
[email protected](reason="Test not working")
+ not DependencyManager.watchdog.has(),
+ reason="watchdog not installed",
+)
+async def test_watchdog_file_moved() -> None:
+ """Test that watchdog detects when a temp file is moved to the target path.
+
+ This simulates editors like Claude Code that save by creating a temp file
+ and then moving it to the target location.
+ """
+ from marimo._utils.file_watcher import _create_watchdog
+
+ with NamedTemporaryFile(delete=False) as tmp_file:
+ tmp_path = Path(tmp_file.name)
+ tmp_file.write(b"initial content")
+
+ callback_calls: list[Path] = []
+
+ async def test_callback(path: Path) -> None:
+ callback_calls.append(path)
+
+ try:
+ # Create watcher
+ loop = asyncio.get_event_loop()
+ watcher = _create_watchdog(tmp_path, test_callback, loop)
+ watcher.start()
+
+ # Wait for watcher to be ready
+ await asyncio.sleep(0.2)
+
+ # Simulate Claude Code save pattern: create temp file and move it
+ temp_save_path = tmp_path.parent / f"{tmp_path.name}.tmp.12345"
+ with open(temp_save_path, "w") as f: # noqa: ASYNC230
+ f.write("modified content")
+
+ # Move temp file to target (simulating atomic save)
+ shutil.move(str(temp_save_path), str(tmp_path))
+
+ # Wait for the watcher to detect the move
+ await asyncio.sleep(0.3)
+
+ # Stop watcher
+ watcher.stop()
+
+ # Assert that the callback was called
+ assert len(callback_calls) >= 1
+ assert callback_calls[0] == tmp_path
+
+ finally:
+ # Cleanup
+ if tmp_path.exists():
+ os.remove(tmp_path)
diff --git a/tests/mocks.py b/tests/mocks.py
index d785fcd0a6..f577dc5e43 100644
--- a/tests/mocks.py
+++ b/tests/mocks.py
@@ -2,7 +2,6 @@ from __future__ import annotations
import difflib
import re
-import sys
from html.parser import HTMLParser
from pathlib import Path
from typing import Callable
@@ -102,15 +101,6 @@ def snapshotter(current_file: str) -> Callable[[str, str], None]:
)
if result != expected:
- # Old versions of markdown are allowed to have different
- # tags and whitespace
- if sys.version_info < (3, 10):
- if ToText.apply(result) == ToText.apply(expected):
- pytest.xfail(
- "Different tags in older markdown versions"
- )
- return
-
write_result()
print("Snapshot updated")