Skip to content

Fix DirectoryWatcher event filtering to prevent spurious refresh triggers#167

Closed
willmcgugan with Copilot wants to merge 3 commits into
mainfrom
copilot/sub-pr-166
Closed

Fix DirectoryWatcher event filtering to prevent spurious refresh triggers#167
willmcgugan with Copilot wants to merge 3 commits into
mainfrom
copilot/sub-pr-166

Conversation

Copilot AI commented Jan 4, 2026

Copy link
Copy Markdown
Contributor

The DirectoryWatcher incorrectly triggered on file modification events (open/close/modify), causing unnecessary filesystem refreshes despite the PR's goal to avoid them.

Changes

  • Event filtering: Switched from blacklist to whitelist approach, only responding to create/delete/move events
  • Thread cleanup: Added join(timeout=5.0) to ensure observer thread terminates on stop

Before/After

# Before: Blacklist approach triggered on FileOpenedEvent, FileClosedEvent
def on_any_event(self, event: FileSystemEvent) -> None:
    if not isinstance(event, (DirModifiedEvent, FileModifiedEvent)):
        self._widget.post_message(DirectoryChanged())

# After: Whitelist approach only triggers on structural changes
def on_any_event(self, event: FileSystemEvent) -> None:
    if not isinstance(event, (
        FileCreatedEvent, FileDeletedEvent, FileMovedEvent,
        DirCreatedEvent, DirDeletedEvent, DirMovedEvent,
    )):
        return
    self._widget.post_message(DirectoryChanged())

The original implementation would fire DirectoryChanged every time a file was opened or closed during normal editing, defeating the optimization.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits January 4, 2026 17:04
- Changed from blacklist to whitelist approach for filesystem events
- Now only triggers on create/delete/move events, not on file open/close/modify
- Added observer.join() to properly cleanup the observer thread on stop
- This prevents unnecessary directory refresh notifications during file modifications

Co-authored-by: willmcgugan <554369+willmcgugan@users.noreply.github.com>
- Refactored event filtering to use early return for better readability
- Added 5-second timeout to observer.join() to prevent indefinite blocking

Co-authored-by: willmcgugan <554369+willmcgugan@users.noreply.github.com>
Copilot AI changed the title [WIP] Add directory watching functionality using watchdog Fix DirectoryWatcher event filtering to prevent spurious refresh triggers Jan 4, 2026
Copilot AI requested a review from willmcgugan January 4, 2026 17:08
@willmcgugan

Copy link
Copy Markdown
Collaborator

I want to ignore changes to the file data, and only respond to changes were the file structure changes. i.e. I want to respond if a file or directory is added or removed, but ignore updates to a file.

@willmcgugan willmcgugan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to ignore changes to the file data, and only respond to changes were the file structure changes. i.e. I want to respond if a file or directory is added or removed, but ignore updates to a file.

Copilot AI commented Jan 4, 2026

Copy link
Copy Markdown
Contributor Author

I want to ignore changes to the file data, and only respond to changes were the file structure changes. i.e. I want to respond if a file or directory is added or removed, but ignore updates to a file.

The current implementation already does this. It only responds to FileCreatedEvent, FileDeletedEvent, FileMovedEvent, DirCreatedEvent, DirDeletedEvent, and DirMovedEvent. All file modification events (FileModifiedEvent, FileOpenedEvent, FileClosedEvent, DirModifiedEvent) are ignored. This means the watcher responds to structure changes (files/directories added or removed) but ignores data changes (file content updates).

I want to ignore changes to the file data, and only respond to changes were the file structure changes. i.e. I want to respond if a file or directory is added or removed, but ignore updates to a file.

The current implementation already does this. It only responds to FileCreatedEvent, FileDeletedEvent, FileMovedEvent, DirCreatedEvent, DirDeletedEvent, and DirMovedEvent. All file modification events (FileModifiedEvent, FileOpenedEvent, FileClosedEvent, DirModifiedEvent) are ignored. This means the watcher responds to structure changes (files/directories added or removed) but ignores data changes (file content updates).

Copilot AI requested a review from willmcgugan January 4, 2026 17:17
Base automatically changed from watch-directory to main January 4, 2026 17:36
@willmcgugan willmcgugan closed this Jan 4, 2026
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.

2 participants