Repository navigation
fix(windows): coalesce the deletion reported before a case-only rename - #1203
MohammedAlkindi wants to merge 1 commit into
Conversation
Renaming a file to a different case, `file` to `FILE`, makes ReadDirectoryChangesW report FILE_ACTION_REMOVED for the old name on top of the usual RENAMED_OLD_NAME/RENAMED_NEW_NAME pair. Watchdog already turned that pair into a move event, so a case-only rename surfaced as a FileDeletedEvent followed by a FileMovedEvent, and a directory renamed the same way as a FileDeletedEvent followed by a DirMovedEvent. Consumers had no way to tell those deletions from real ones. DirectoryChangeReader.get_events() now drops a deletion when the very next record renames that same path. A rename that also changes the name reports the pair alone, and nothing can rename a path that was genuinely deleted, so the condition cannot swallow a real deletion; a deletion ending the batch is left untouched. Cross-directory moves are deliberately unchanged: Windows reports those as a plain delete/create carrying no rename records at all, whether or not the case differs, so they cannot be recovered here. tests/test_emitter.py gains test_case_only_rename_on_windows, which fails on the spurious FileDeletedEvent before the fix and passes after it. tests/test_observers_winapi.py pins the negative cases directly. Closes gorakhargosh#752
|
CI note, so the red X isn't misread: the 2 failing jobs are 🐧 Linux 3.13t / 3.14t, both on The 24 other jobs pass, including every Windows one. On 🪟 Windows 3.14 the new tests all ran green: Happy to rebase if you land a fix for the debounce test and want a clean run. |
|
Superseded by #1204 — same commit and same diff. I closed this one by accident renaming the source branch; GitHub deletes the old head ref, which auto-closes the PR, and it can't be reopened once the ref is gone. Sorry for the noise. |
Closes #752
Symptom
Renaming a file to a different case —
filetoFILE— emits two events on Windows instead of one:A directory renamed the same way emits
FileDeletedEventfollowed byDirMovedEvent. Since the deletion is indistinguishable from a real one, consumers see a file vanish and then move.Cause
The move event was never the problem — watchdog already builds it correctly from the rename pair. The extra deletion comes straight from the OS. Dumping the raw
ReadDirectoryChangesWrecords on Windows 11 (3 runs each, identical every time):file→FILEREMOVED(file),RENAMED_OLD_NAME(file),RENAMED_NEW_NAME(FILE)a→bRENAMED_OLD_NAME(a),RENAMED_NEW_NAME(b)sub→SUB(dir)REMOVED(sub),RENAMED_OLD_NAME(sub),RENAMED_NEW_NAME(SUB)Windows prepends a
FILE_ACTION_REMOVEDfor the old name only when the rename changes nothing but case.queue_events()translates each record independently, so that record became a deletion event.Fix
DirectoryChangeReader.get_events()drops a deletion when the very next record renames that same path. Two properties make that condition safe rather than heuristic:CREATEDrecord sits between them and the deletion is kept.A deletion that ends the batch is also kept, so a rename split across two
ReadDirectoryChangesWbuffers degrades to today's behaviour rather than losing an event.Deliberately unchanged
Cross-directory moves.
tests/test_emitter.py::test_case_change— the test linked from the issue — movesdir1/filetodir2/FILE, and Windows reports that asREMOVED+CREATED+MODIFIEDwith no rename records at all. It reportsdir1/a→dir2/bidentically, so this is not case-related: the information needed to emit a move is simply absent, and recovering it would mean correlating unrelated deletions with later creations. That test is left exactly as it is, and this PR does not change cross-directory behaviour.Testing
Run on Windows 11, Python 3.13.13.
tests/test_emitter.py::test_case_only_rename_on_windows— new. Renames a file and a directory by case only and asserts exactly one move event each. Fails before the fix withAssertionError: wrong number of events, having foundFileDeletedEvent+FileMovedEvent; passes after.tests/test_observers_winapi.py— new unit tests pinning the condition, including the three negative cases that must keep their deletion: deleted-recreated-then-renamed, a deletion followed by a rename of a different path, and a deletion ending the batch.mastercheckout, 163 passed / 2 failed with this branch. The same two —test_renaming_top_level_directoryandtest_move_nested_subdirectories_on_windows— fail identically before and after (verified by stashing the change and re-running); they are pre-existing on Windows and untouched here.ruff format --check,ruff check, andsphinx-buildare clean.mypy src docs/source/examplesreports 11 errors both before and after — the same set, unusedtype: ignorecomments plus POSIX-only attributes flagged when run on Windows. No new errors introduced.I could not run the Linux or macOS jobs locally, so I left the new emitter test Windows-gated rather than assert behaviour I had not observed — the bug is a Windows-API artefact and the fix is confined to
winapi.py. CI is the authority for the other two platforms.