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
Interface
Assistant
Let me start by exploring the repository structure to understand the codebase.
Assistant
Now I need to understand the file manager and how it handles file loading/reloading. Let me check the file_manager.py:
Assistant
Now let me look at the
load.load_appfunction:Assistant
Now let me check the existing tests for file_manager.py:
Assistant
Let me look at the rest of the file_change_handler.py and how reload works:
Assistant
Let me look at the file_change_handler in more detail:
Assistant
Now I understand the structure. The issue is that when a file contains git conflict markers (
<<<<<<<), the file manager tries to load it, which parses those conflict markers asapp._unparsable_cell, which can mess things up.Let me first reproduce the issue:
Assistant
Now let me look at the file watcher integration to understand the whole flow:
Assistant
Now let me look at how the session creation works to understand the file watching:
Assistant
Now I understand the flow. Let me reproduce the issue first:
Assistant
I've reproduced the issue. The file with conflict markers cannot even be loaded due to a syntax error. Let me look at what happens in
reload:Assistant
Now let me try with a properly structured notebook that has conflict markers in cells (more realistic scenario):
Assistant
I've reproduced both cases of the issue. The fix should be to skip the reload when conflict markers are detected. Let me look at where to make this fix:
Assistant
Now let me look at how the file content matching check works to find the best place to add this:
Assistant
Now let me look at the storage class to understand how to read files:
Assistant
Now I have enough understanding. Let me design the fix. The best approach is to:
- Add a check for git conflict markers (
<<<<<<<) at the beginning of a line in the file before reloading - If conflict markers are detected, skip the reload
Let me see where this check should go. Looking at the issue, the most logical place is in
file_change_handler.pywhere the file change is being processed:- Add a check for git conflict markers (
Assistant
Now let me design my fix. The approach is to:
- Add a helper function to detect git conflict markers
- Call this function in
file_change_handler.pyto skip the reload if markers are found
Let me look at how existing checks are done for context:
Assistant
No existing git conflict detection. Now let me implement the fix. Let me add the check in
file_change_handler.py:Assistant
Now let me see the structure of the test files for
file_change_handler.py:Assistant
Let me look at the test for
_handle_file_change_locked:Assistant
Now let me check the parse module to see how the unparseable_cell check is normally done, since the issue mentions
app._unparsable_cell:Assistant
Now I understand the issue. Let me implement the fix. I'll:
- Add a helper function
_has_merge_conflict_markersthat checks for git conflict markers at the beginning of a line in file content - Call this function in
file_change_handler.pyto skip the reload when markers are detected
- Add a helper function
Assistant
Now I'll make my fix. Let me first design it cleanly:
The approach:
- Add a helper function
_has_merge_conflict_markersto detect git conflict markers in file content - Add this check to the
_handle_file_change_lockedmethod infile_change_handler.py - The check should look at the file content, since reading the file is what we'll need to do anyway to detect the markers
Let me also consider edge cases:
- The check should detect
<<<<<<<at the start of a line (this is the standard git conflict marker) - It should also probably handle the case where the file might not be readable
Let me implement the fix:
- Add a helper function
Assistant
Now let me check what the existing tests for the file_change_handler expect:
Assistant
All tests pass. Now let me make the fix.
Let me look at the structure to see what would be the best place to add this check:
Assistant
Now let me think about this more carefully. The check should happen before we try to reload. The approach:
- Read the file content
- Check if it contains merge conflict markers (
<<<<<<<at the beginning of a line) - If yes, skip the reload
But wait - the user mentioned that "This gets in the way of resolving the conflicts using conflict resolution tools like git-mediate". So when there's a conflict, the file watcher shouldn't try to reload the file because the file content is in an invalid state and trying to parse it would mess up the cell contents.
The fix should be in the file_change_handler.py. Let me also look at how
file_content_matches_last_savereads the file, so I can model my fix similarly:Assistant
Now I have enough context. Let me design the fix. The cleanest approach is to:
- Add a helper function to check for git conflict markers (
<<<<<<<) at the beginning of a line in file content - Use this check in
file_change_handler.pyto skip the reload when markers are detected
Let me first add a method to
AppFileManagerto check for merge conflict markers, similar to the existingfile_content_matches_last_savemethod:- Add a helper function to check for git conflict markers (
Assistant
Now let me look at how the file is being read in the
FileChangeCoordinatorand where the appropriate place to add this check is:Assistant
Now let me implement the fix. I'll:
- Add a helper function
_has_merge_conflict_markersat the module level infile_change_handler.py - Use this check in
_handle_file_change_lockedbefore reloading
- Add a helper function
Assistant
The pattern works. Now let me implement the fix:
Assistant
Now I'll implement the fix. Let me modify the file_change_handler.py:
Assistant
Now let me apply the fix to the actual file. I'll modify
file_change_handler.pyto add the helper and use it in the locked handler:Assistant
Let me use
replaceinstead:Assistant
Now let me modify the file. I'll use the
replacecommand to do the replacement. First, let me use Python to do the modification since it's complex:Assistant
The fix is applied. Now let me check that the existing tests still pass:
Assistant
I made a mistake - the
_handle_file_change_lockedis synchronous but I tried to useawait. Let me look at the structure and refactor:Assistant
The
_handle_file_change_lockedis a synchronous method called from the asynchandle_change. Let me read the file to understand and fix this properly:Assistant
I see. I need to read the content in
handle_changeand pass it to_handle_file_change_locked. Let me restructure the fix:Assistant
Wait - there's a potential issue. The
file_content_matches_last_savealso reads the file. Reading the file twice is inefficient. Let me also think about another issue: between reading the content and reloading, the file could change. So we might want to either:- Read once and reuse, OR
- Read twice (which is what the current code does for
file_content_matches_last_save)
Since the file watcher event has already been debounced, the file should be relatively stable at this point. Let me reconsider whether I should:
- Pass the content into
file_content_matches_last_savetoo (more refactoring) - Or just live with the slight inefficiency of reading twice
Assistant
All existing tests pass. Now let me also verify the fix actually works by re-running my reproduce script:
Assistant
Let me check the actual code and verify the issue:
Assistant
Let me check what's actually happening:
Assistant
The conflict detection works and
file_content_matches_last_savereturns False. But the result still showshandled=True. That means my conflict check is not being hit! Let me look at the code again more carefully.Oh wait, I see the issue! The conflict check returns False because... let me look at the code oncemore:
Assistant
I see -
read_textis a method onAsyncPathclass, not a module-level function. Letme check how it's called:Assistant
Hmm, the file_change_handler.py uses
async_path.read_text(abs_file_path)but it seems read_text doesn't exist at the module level. Letme check this:Assistant
Indeed,
async_path.read_textdoesn't exist at module level. But my code callsawait async_path.read_text(abs_file_path)which would fail at runtime!Wait, but the test ran without error and detected the conflict markers... let me look again.
Looking at the test output:
codehas_merge_conflict_markers: True file_content_matches_last_save returned: False ... Result: handled=True, error=NoneThe conflict was detected (true), but the result was handled=True. So the conflict check returned early but the result says handled=True...
Wait, maybe my call to
async_path.read_textis actually working somehow. Let me check if it's an AsyncPath class method or module function. Actuallyread_textis a method on AsyncPath class. But the test passed... maybe Python is auto-handling it somehow?Let me check by running justthe read_text call:
Assistant
So
async_path.read_textdoesn't exist at module level! That means my code fails silently (the exception is caught). Let me check this:Assistant
Yes! That's exactly what's happening. The
async_path.read_textis failing with AttributeError, but it's caught silently andcurrent_contentbecomes None, so my conflict check is never triggered.I need to use
AsyncPath(path).read_text()instead. Let me fix this:Assistant
Now let me run the test again:
Assistant
The fix is working now. The test failure is because the strategy was already called in the previous test (the same MagicMock is being reused). Let me update the test:
Assistant
The conflict at module level test is failing because
AppFileManager(filename=str(notebook_path))fails to initialize because the file has conflict markers at the top level which makes it invalid Python. This is actually expected behavior - the file watcher scenario is different (the AppFileManager was created when the file was valid).Let me update my test to better reflect the actual flow:
Assistant
All my tests pass. Let me also run the existing tests to make sure nothing broke:
Assistant
Good, the test was already failing before my changes. Let me restore my changes:
Assistant
Good. The failing test was pre-existing and not related to my changes. Let me also run a broader test to make sure nothing else is broken:
Assistant
Another pre-existing failure (kernel process test). Let me skip those and run only the file_change_handler tests:
Assistant
All relevant tests pass. Let me also run a broader subset of related tests:
Assistant
Let me check the full file_change_handler.py to make sure my fix is clean:
Assistant
Let me also look for other places where the file might be reloaded to make sure my fix covers all cases:
Assistant
Let me also check if there's a CLI command that opens files (since the issue mentions
marimo edit --watch). The CLI might also have itsown logic:Assistant
The export watch flow is different - it just re-runs the export, which would naturally fail if the file has syntax errors. So the issue is specifically about the edit session reloading. My fix is sufficient.
Let me also check the
marimo edit --watchcommand:Assistant
The
--watchoption is formarimo edit. Looking at the flow, the file watch triggersFileChangeCoordinator.handle_change, which is exactly where I added the fix. Let me also look at thelintersince the issue mentioned_unparsable_cell:Assistant
Let me also think about whether there's any case where the file might be reloaded without going through
FileChangeCoordinator.handle_change. Let me search:Assistant
Good - the
trigger_file_changein session_manager.py also goes through theFileChangeCoordinator.handle_change, so my fix covers all reload scenarios from the file watcher.Let me also check the file_watcher_integration:
Assistant
The file watcher just calls callbacks. The callbacks are set up in
SessionFileWatcherExtension._handle_file_changewhich calls_on_change_callbackwhich is_handle_file_changeinsession_manager.pywhich calls_file_change_coordinator.handle_change. So my fix is at the right place.Now let me also think about the case where the user resolves the conflict - my fix should still allow reloading after the conflict is resolved. Let me verify this in my test:
Assistant
The fix correctly:
- Skips the reload when conflict markers are detected
- Properly applies the change once the conflict is resolved
Let me also test the case where the conflict markers are inside a string literal (which shouldn't trigger):
Assistant
All edge cases pass. Now let me also make sure the changes I made work well with the existing tests:
Assistant
All tests pass. Let me also check that the existing tests for the syntax error handling still pass:
Assistant
Let me also check the syntax error test in detail. The current test has syntax errors that aren't conflict markers, so it should still be handled correctly:
Assistant
The existing test for syntax errors is interesting - it expects
result.handled = Trueeven when the file has syntax errors. This is because marimo has a best-effort scanner fallback that can still parse partially.But with my change, if the file contains
<<<<<<<at the start of a line, the conflict check would skip the reload. The test above doesn't include conflict markers, so it should still pass.Let me verify that the existing test passes correctly:
Assistant
Now let me also make sure my fix handles the case where the file path passed to
handle_changeis aPathobject (since the original signature saysfile_path: Path):Assistant
Both Path and string paths work. Now let me also think about how my fix interacts with the conflict at the module level (which makes the file invalid Python):
Assistant
Excellent! The fix works correctly for both in-cell and module-level conflicts. Now let me also make sure all related tests pass:
Assistant
The only failure is the pre-existing kernel test that has nothing to do with my changes. Let me verify this is pre-existing by stashing my changes and running again:
Assistant
Good - that test is pre-existing and fails regardless of my changes. It's a flaky test about process killing.
Let me now also check that my changes look clean:
Assistant
Let me also look at the diff one more time to see if there are any optimizations to make:
Assistant
The double-read is acceptable since it's not in a hot path. Let me clean up my temporary test files:
Assistant
Now let me verify my changes one more time by looking at the complete diff:
Assistant
The diff looks clean. Let me do a final reproduction of the original issue:
Assistant
The
_unparsableis on the inner App, not InternalApp. Let me fix my test:Assistant
The fix works exactly as expected. Let me also test what happens WITHOUT the fix (by reverting briefly):
Assistant
The bug is confirmed: WITHOUT the fix, the conflict markers are embedded in cells as code, exactly as described in the issue. WITH the fix, the reload is correctly skipped.
Let me also run the test one more time to make sure everything is good:
Assistant
Let me also clean up the temporary file:
Assistant
Let me also check if there's any documentation in the repository that mentions this behavior:
Assistant
No documentation about git conflicts. Let me look at the final state of the file:
Assistant
Let me check if there are any tests that might be affected by my changes:
Assistant
All 23 tests pass.
Now let me go through the checklist:
Checklist Review
-
If an error was reported in the issue description, I have successfully reproduced it.
-
Assistant
Good - no temp files left. Let me check the final state of the repository:
Assistant
The fix is complete and the diff is clean. Let me do one final verification by running all the relevant tests:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.