Skip to content

fix(windows): coalesce the deletion reported before a case-only rename - #1203

Closed
MohammedAlkindi wants to merge 1 commit into
gorakhargosh:masterfrom
MohammedAlkindi:claude/fix-windows-case-only-rename
Closed

MohammedAlkindi wants to merge 1 commit into
gorakhargosh:masterfrom
MohammedAlkindi:claude/fix-windows-case-only-rename

Conversation

@MohammedAlkindi

Copy link
Copy Markdown
Contributor

Closes #752

Symptom

Renaming a file to a different case — file to FILE — emits two events on Windows instead of one:

FileDeletedEvent(src_path='...\file')
FileMovedEvent(src_path='...\file', dest_path='...\FILE')

A directory renamed the same way emits FileDeletedEvent followed by DirMovedEvent. 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 ReadDirectoryChangesW records on Windows 11 (3 runs each, identical every time):

operation records reported
file → FILE REMOVED(file), RENAMED_OLD_NAME(file), RENAMED_NEW_NAME(FILE)
a → b RENAMED_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_REMOVED for 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:

  • A rename that also changes the name reports the pair alone, so ordinary renames are untouched.
  • Nothing can rename a path that was genuinely deleted. For the records to be adjacent and share a path, the "deletion" cannot have been real — if a path is deleted, recreated and only then renamed, a CREATED record sits between them and the deletion is kept.

A deletion that ends the batch is also kept, so a rename split across two ReadDirectoryChangesW buffers 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 — moves dir1/file to dir2/FILE, and Windows reports that as REMOVED + CREATED + MODIFIED with no rename records at all. It reports dir1/a → dir2/b identically, 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 with AssertionError: wrong number of events, having found FileDeletedEvent + 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.
  • Full suite: 158 passed / 2 failed on a clean master checkout, 163 passed / 2 failed with this branch. The same two — test_renaming_top_level_directory and test_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, and sphinx-build are clean.
  • mypy src docs/source/examples reports 11 errors both before and after — the same set, unused type: ignore comments 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.

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
@MohammedAlkindi

Copy link
Copy Markdown
Contributor Author

CI note, so the red X isn't misread: the 2 failing jobs are 🐧 Linux 3.13t / 3.14t, both on tests/test_0_watchmedo.py::test_auto_restart_on_file_change_debounce - assert 2 == 3. That is pre-existing free-threaded flakiness, not this change — the latest master run (a0bb46c, the commit this branch is based on) fails the same test in the same job, and Tests has been red on 5 of the last 6 master runs. This PR touches only winapi.py, which is never imported on Linux.

The 24 other jobs pass, including every Windows one. On 🪟 Windows 3.14 the new tests all ran green:

tests/test_emitter.py::test_case_only_rename_on_windows PASSED
tests/test_observers_winapi.py::test_drop_case_only_rename_deletions PASSED
tests/test_observers_winapi.py::test_drop_case_only_rename_deletions_keeps_real_deletions[events0] PASSED
tests/test_observers_winapi.py::test_drop_case_only_rename_deletions_keeps_real_deletions[events1] PASSED
tests/test_observers_winapi.py::test_drop_case_only_rename_deletions_keeps_real_deletions[events2] PASSED
165 passed, 13 skipped, 1 xfailed, 1 xpassed

Happy to rebase if you land a fix for the debounce test and want a clean run.

@MohammedAlkindi

Copy link
Copy Markdown
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Windows] Generate a FileMovedEvent when changing the case of a file

1 participant