ISSUE DESCRIPTION marimo edit --watch messes with merge conflict markers Describe the…
- User
ISSUE DESCRIPTION
marimo edit --watch messes with merge conflict markers
Describe the bug
When cherry-picking commits, or
git stash poping, sometimes merge conflicts occur.If a notebook is open using
marimo edit --watchduring that time, it parses the git conflict markers asapp._unparsable_cell. This gets in the way of resolving the conflicts using conflict resolution tools likegit-mediateSuggestion: Avoid updating the notebook if the file contains conflict markers, recognizable by
<<<<<<<at the beginning of a line.Will you submit a PR?
- Yes
Environment
code{ "marimo": "0.23.6", "editable": false, "location": "/Users/yairchu/dev/soundradix/Drum3D/.venv/lib/python3.13/site-packages/marimo", "OS": "Darwin", "OS Version": "25.2.0", "Processor": "arm", "Python Version": "3.13.1", "Locale": "C/en_US", "Binaries": { "Browser": "140.0.7339.134", "Node": "v23.11.0", "uv": "0.7.12 (dc3fd4647 2025-06-06)" }, "Dependencies": { "click": "8.4.0", "docutils": "0.22.4", "itsdangerous": "2.2.0", "jedi": "0.19.2", "markdown": "3.10.2", "narwhals": "2.21.2", "packaging": "26.2", "psutil": "7.2.2", "pygments": "2.20.0", "pymdown-extensions": "10.21.3", "pyyaml": "6.0.3", "starlette": "1.0.0", "tomlkit": "0.15.0", "typing-extensions": "4.15.0", "uvicorn": "0.47.0", "websockets": "16.0" }, "Optional Dependencies": { "altair": "6.1.0", "anywidget": "0.11.0", "loro": "1.10.3", "pandas": "3.0.3" }, "Experimental Flags": {} }Code to reproduce
No response
ADDITIONAL ADVICE
- Assistant
I'll start by exploring the repository structure to understand the codebase. Let's first look at the directory.
- Assistant
Let me understand the issue better and find the relevant files. The issue is about
marimo edit --watchmessing with merge conflict markers. The suggestion is to avoid updating the notebook if the file contains conflict markers.Let me look at the structure more carefully.
- Assistant
Let me search for the watch-related code that processes file changes.
- Assistant
Let me look at the watch-related files, particularly the file watcher integration and the file change handler.
- Assistant
Let me look at the file watcher integration code, which is likely where the fix should be applied.
- Assistant
Let me look at the file change handler and the
--watchflag implementation. - Assistant
Now I see the file change handling code. The key method is
_handle_file_change_lockedinFileChangeCoordinator. When a file changes, it:- Checks if session path matches
- Checks if content matches last save (to avoid reloading own writes)
- Reloads the file manager
- Sends changes to the frontend
The issue is that when merge conflict markers are present in the file, the reload operation treats them as code, which messes things up. Let me look at the reload method to understand what happens.
Let me look at the file manager's reload method.
- Assistant
Let me look at the CLI code for
marimo edit --watchand find where the watch callback processes file changes: - Assistant
Let me search for where the file watcher is set up for
marimo edit --watch. - Assistant
Let me look at how the CLI handles the
--watchflag formarimo edit: - Assistant
Let me look at the start.py file to understand how the watch mode is used.
- Assistant
Let me look at the
_load_appmethod to understand the flow better. - Assistant
Let me look at the
_load_appmethod: - Assistant
Now let me look at the
load.load_appfunction to see how it handles parsing: - Assistant
No existing merge conflict handling. Let me understand the flow better.
The key flow is:
- When
--watchis enabled and a file is edited externally (e.g., git merge), the file watcher detects the change FileChangeCoordinator._handle_file_change_lockedis called- It calls
session.app_file_manager.reload() - This loads the file which may contain merge conflict markers
- The conflict markers get parsed and potentially corrupt the notebook state
The suggestion is to check for merge conflict markers (
<<<<<<<) before reloading and skip the update if found.Let me trace through the flow to find the best place to add this check:
SessionFileWatcherExtension._handle_file_change→FileChangeCoordinator.handle_change→_handle_file_change_locked- In
_handle_file_change_locked, before callingsession.app_file_manager.reload(), we should check if the file contains merge conflict markers
The best place to add this check would be in the
FileChangeCoordinator._handle_file_change_lockedmethod, or perhaps inAppFileManager.reload().Let me check if there's a good utility function or if I need to create one.
- When
- Assistant
Now I understand the flow. When
marimo edit --watchruns and a file with merge conflict markers is saved (e.g., duringgit stash pop), the file watcher detects the change, callsreload(), and the conflict markers get parsed as part of the notebook cells, creating_unparsable_cellentries.The fix should:
- Check for merge conflict markers (
<<<<<<<) before attempting to reload the notebook - Skip the reload if conflict markers are found, logging a warning
Let me find the best place to add this check. The
FileChangeCoordinator._handle_file_change_lockedmethod is where file changes are processed: - Check for merge conflict markers (
- Assistant
The fix should be straightforward: we need to detect if a file contains merge conflict markers (
<<<<<<<) before reloading it in the watch mode. Let me check the_handle_file_change_lockedmethod infile_change_handler.pyto see where to add the check.The best approach is to:
- Read the file content before reloading
- Check if it contains merge conflict markers (
<<<<<<<at the beginning of a line) - If it does, skip the reload and log a warning
Let me also check the
_maybe_contentsfunction inload.pyto see how the file is read. - Assistant
Let me look at the file_content_matches_last_save function to see how it reads the file:
- Assistant
Now I understand the flow. The
_handle_file_change_lockedmethod infile_change_handler.pyis where file changes are handled. It already:- Checks if the file content matches the last saved content (to avoid reloading our own writes)
- Calls
session.app_file_manager.reload()to reload the notebook
The fix should add a check for merge conflict markers between step 1 and step 2. If the file contains merge conflict markers, we should skip the reload and log a warning.
Let me implement this:
- Assistant
Now I have a good understanding of the flow and how to fix it. Let me look at where the file change handler does the actual loading. The key method is
_handle_file_change_lockedinfile_change_handler.py. It checksfile_content_matches_last_save()and then callssession.app_file_manager.reload().The best place to add the check is in
_handle_file_change_locked, right after thefile_content_matches_last_save()check, but before callingreload(). We need to read the file content and check for merge conflict markers.Let me look at the exact code:
- Assistant
Let me see the rest of the method:
- Assistant
Now I understand the flow. The best place to add the merge conflict check is in
_handle_file_change_locked, after the content-match check but before the reload call.I'll add a check that reads the file content and looks for merge conflict markers (
<<<<<<<). If found, we skip the reload and log a warning.Let me look at how to read the file in this context. I need to read the file using the session's file manager's storage.
- Assistant
Now I need to look at how the file is read. The
storage.read()method is used. Let me check what that returns and how to check for merge conflict markers.The approach should be:
- Read the file content
- Check if it contains merge conflict markers
- If it does, skip the reload and return a FileChangeResult with handled=False
Let me implement this. I'll add the check in
_handle_file_change_lockedafter the content-match check. - Assistant
Now I understand the issue. The best place to add the merge conflict check is in
_handle_file_change_locked, right after checkingfile_content_matches_last_save()and before callingreload(). We need to read the file content and check for merge conflict markers before attempting to reload.Let me look at the relevant code more carefully:
- Assistant
Now let me look at how the watch/file watcher actually reads and watches the file:
- Assistant
Now I understand the full flow. The issue is in the
_handle_file_change_lockedmethod infile_change_handler.py. When a file changes (detected by the watcher), it reloads the notebook. If the file contains merge conflict markers, this causes problems.The fix should add a check for merge conflict markers between the "content matches last save" check and the
reload()call. Let me implement this: - Assistant
Let me check how the session and file_manager provide access to the file content:
- Assistant
Let me check if there are other places where file watching triggers reloads:
- Assistant
Now I have a clear understanding of the issue and where to fix it. The issue is:
- When
marimo edit --watchis running, it watches the notebook file for changes - When a merge conflict occurs (e.g., from
git stash pop), the file gets conflict markers like<<<<<<<,=======,>>>>>>> - The file watcher picks up the change and tries to reload the notebook
- The conflict markers get parsed as part of the notebook cells, corrupting the notebook state
The fix should be to add a check for merge conflict markers in
_handle_file_change_lockedmethod, right after checking if the file content matches the last save but before callingreload(). If the file contains conflict markers, we should skip the reload and log a warning.Let me also check how the file content is read to understand how to access it:
- When
- Assistant
Let me look at the storage interface to see how to read file content:
- Assistant
Now I have a clear understanding of the codebase. Let me also look at the
_messaging/notebook/document.pyfile that was mentioned as having "conflict" references: - Assistant
Let me look at the
AppFileManager.read_filemethod: - Assistant
Now I have a clear understanding. Let me implement the fix. The key change needs to be in
_handle_file_change_lockedinfile_change_handler.py. I need to:- After checking if the file content matches the last save, check if the file contains merge conflict markers
- If it does, skip the reload and log a warning
Let me add the check:
- Assistant
Now I understand the full flow. Let me think about where to add the merge conflict detection. The best place is in
_handle_file_change_lockedinfile_change_handler.py, after checking if content matches last save but before callingreload(). This way, if the file contains merge conflict markers, we skip the reload entirely and log a warning.Let me also check if there are any tests related to this:
- Assistant
Now I understand the issue and the codebase. Let me implement the fix:
-
The issue is that when
marimo edit --watchis running and a merge conflict occurs in a notebook file (e.g., fromgit stash pop), the file watcher picks up the change and tries to parse the file, which contains merge conflict markers (<<<<<<<,=======,>>>>>>>). This causes the conflict markers to be parsed as cells, which interferes with conflict resolution tools. -
The fix should check if the file content contains merge conflict markers before attempting to reload. If it does, we should skip the reload and log a warning.
-
The best place to add this check is in
_handle_file_change_lockedinfile_change_handler.py, after checking if the file content matches the last save but before callingreload().
Let me implement this:
-
- Assistant
Now I have a clear picture of the issue and the codebase. Let me implement the fix:
The fix should be in
_handle_file_change_lockedinfile_change_handler.py. After checking if the file content matches the last save, we need to check if the file contains merge conflict markers (<<<<<<<). If it does, we should skip the reload and log a warning.Let me implement the fix:
- Assistant
Let me use the venv:
- Assistant
Now I have a clear understanding. Let me implement the fix. I'll add a check for merge conflict markers in the
_handle_file_change_lockedmethod, right after the content matches last save check: - Assistant
Now let me go to line 216 which is where the content match check happens:
- Assistant
Now I can see the exact code. I need to add a check for merge conflict markers between line 222 and line 224. The check should:
- Read the file content
- Check if it contains merge conflict markers (
<<<<<<<) - If it does, log a warning and return
FileChangeResult(handled=False)
Let me implement the fix:
- Assistant
I see there's a duplicate comment. Let me fix that and add the
_has_merge_conflict_markersfunction: - Assistant
Good, the duplicate is fixed. Now I need to add the
_has_merge_conflict_markersfunction. Let me add it near the top of the file, after the imports and before theFileChangeResultclass: - Assistant
I need to add a blank line before the function. Let me fix the formatting:
- Assistant
The blank line is missing between the TYPE_CHECKING block and the function. Let me fix it:
- Assistant
There needs to be a blank line between the TYPE_CHECKING block and the function. Let me add it:
- Assistant
There's a duplicate import line. Let me fix that:
- Assistant
Good, the duplicate is fixed. Now let me also check if
read_file()is the right method to use. Let me look at the file manager to see what's available: - Assistant
Good,
read_file()is the right method to use. Let me now view the full file to verify the changes look correct: - Assistant
I see there's a missing blank line. Let me fix that:
- Assistant
The changes look correct. Let me now run the existing tests to make sure they still pass:
- Assistant
All existing tests pass. Now let me verify the implementation by reviewing the full file:
- Assistant
Good. Now let me also check the other file watching paths. The
watch_and_exportfunction inexport/commands.pyalso watches files for changes. Let me check if that needs the same fix: - Assistant
The
watch_and_exportfunction inexport/commands.pyalso watches files for changes. Let me check if that needs the same fix. This function is used formarimo export html --watch,marimo export script --watch, etc. It also reads and parses the file. However, the issue description specifically mentionsmarimo edit --watch, which is the primary use case.Let me also run a quick test to verify my implementation works. Let me first check if there are any other places where file watching could be affected:
- Assistant
The fix is in the right place. The
_handle_file_change_lockedmethod is called byhandle_changewhich is called by_handle_file_changeinfile_watcher_integration.py. Now let me also check thewatch_and_exportfunction inexport/commands.pyto see if it also needs the same fix. Looking at the code, thewatch_and_exportfunction has its ownon_file_changedcallback that reads the file and tries to export it. However, the issue description specifically mentionsmarimo edit --watch, so I'll focus on that for now.Let me run the existing tests to make sure everything still works:
- Assistant
All tests pass. Now let me also verify that the
watch_and_exportpath might need the same fix. Let me check if we should also protect the export watch path: - Assistant
The
watch_and_exportfunction inexport/commands.pyhas its ownon_file_changedcallback. This is formarimo export html --watch, etc. It reads the file and tries to export it. If the file has merge conflict markers, the export might fail or produce unexpected results. However, the issue description specifically mentionsmarimo edit --watch. Thewatch_and_exportpath is a different use case. Let me focus on the main fix for now.Let me verify the fix by looking at the final state of the file.
- Assistant
The changes look correct. Now let me write a quick test to verify the merge conflict detection works correctly:
- Assistant
All test cases pass. Now let me run the full test suite for the file change handler to make sure everything still works:
- Assistant
All tests pass. Now let me also check if there are any other places where merge conflict markers could cause issues. Let me also check the
watch_and_exportfunction inexport/commands.pyto see if it also needs the same fix: - Assistant
The
watch_and_exportfunction inexport/commands.pyalso watches files for changes. Looking at the code, when a file changes, it callsexport_callbackwhich tries to parse the file. If the file has merge conflict markers, the export might fail or produce unexpected results. However, the issue description specifically mentionsmarimo edit --watch, which is the primary use case. Thewatch_and_exportfunction is formarimo export html --watch, etc. These are less common use cases for merge conflicts, but it would be nice to handle them too.For now, let me focus on the main fix for
marimo edit --watch. The fix I've made should resolve the reported issue.Let me also run a broader set of tests to make sure nothing is broken:
- Assistant
The one failure is unrelated to our change — it's a pre-existing issue in
test_try_kill_process_and_group_kills_grandchild(a process management test). All 115 other tests passed, including all file_change_handler tests.Let me also check if there are other places where merge conflicts could cause issues, specifically in the file_watcher_integration:
- Assistant
Good. The
_handle_file_changeinfile_watcher_integration.pydelegates toself._on_change_callback, which ultimately leads toFileChangeCoordinator.handle_change→_handle_file_change_locked. So our fix covers this path.Let me also consider: should we also add the check to
AppFileManager.reload()itself? That would protect against any code path that calls reload, not just the file watcher. But the issue specifically mentions--watch, and adding it toreload()would prevent legitimate reloads when conflict markers are resolved. The file change handler is the right place since it's the entry point for watch-triggered reloads.Let me now also run a broader test to check for any import issues:
- Assistant
Good. The import works fine. Let me now review the full diff of my changes:
- Assistant
The diff looks clean and correct. Let me also check if the
watch_and_exportpath inexport/commands.pycould also benefit from the same check. Let me check: - Assistant
The fix works correctly end-to-end. Let me now review the checklist and run any related tests.
Now let me also check if the
watch_and_exportpath in the CLI needs a similar fix. That's a separate path used bymarimo export html --watch,marimo export script --watch, etc. Let me also add protection there: - Assistant
The
watch_and_exportfunction is used formarimo export --watch(exporting to HTML/MD/etc.), which is a different use case frommarimo edit --watch. The export path just reads the file and exports it; it doesn't modify the notebook state. However, the issue is specifically aboutmarimo edit --watchcorrupting the notebook state by parsing conflict markers asapp._unparsable_cell. The export path could also benefit from the same check, but it's a separate issue. Let me focus on the main fix for now.Let me run the full test suite for the file change handler one more time to make sure everything is correct:
- Assistant
All 23 tests pass. Let me now review the complete changes and checklist:
Checklist
-
If an error was reported in the issue description, I have successfully reproduced it.
- The issue describes that
marimo edit --watchmesses with merge conflict markers. I traced the code path throughFileChangeCoordinator._handle_file_change_locked→session.app_file_manager.reload()which would parse conflict markers as notebook cells.
- The issue describes that
-
If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I ran an end-to-end integration test showing that the file change coordinator correctly skips reload when the file contains merge conflict markers, and still reloads when the file has valid content.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.