fix(watch): drop events from directories no longer watched (#5033)

A directory moved out of the root can still deliver an event after its
watch is removed. Drop events whose parent is not in the watched map,
and stale file paths whose parent no longer exists.

(cherry picked from commit cf36ca04d4)
This commit is contained in:
Deepjyoti-Sarmah authored and safishamsi committed 2026-11-09 14:09:19 +00:00
1 parent 549ae57159
commit 73a1e861a0
1 file changed
+4
+4
View File
@@ -3615,6 +3615,8 @@ def watch(watch_path: Path, debounce: float = 3.0) -> None:
if event.is_directory:
if not _is_read_only_event(event):
path = Path(os.fsdecode(event.src_path)).resolve()
if path.parent not in watched:
return
if event.event_type == "created":
add_tree(path, emit_existing=True)
elif event.event_type == "deleted":
@@ -3628,6 +3630,8 @@ def watch(watch_path: Path, debounce: float = 3.0) -> None:
if _is_read_only_event(event):
return
path = Path(os.fsdecode(event.src_path))
if path.parent not in watched or not path.parent.exists():
return
# Atomic replacement reports the new root config in dest_path.
dest_raw = getattr(event, "dest_path", None)
dest_path = Path(os.fsdecode(dest_raw)) if dest_raw else None