Skip to content

Commit

Permalink
Ignore non-existent directories (#117)
Browse files Browse the repository at this point in the history
Fixes #12.

---------

Co-authored-by: Adam Johnson <[email protected]>
  • Loading branch information
StevenMapes and adamchainz authored Oct 28, 2024
1 parent c1630a4 commit d7ac0f6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Unreleased

* Support Python 3.13.

* Fix crashing on non-existent directories, which Django sometimes tries to watch.

Thanks to baseplate-admin for the report in `Issue #12 <https://github.com/adamchainz/django-watchfiles/issues/12>`__ and Steven Mapes for the fix in `PR #117 <https://github.com/adamchainz/django-watchfiles/pull/117>`__.

0.2.0 (2024-06-19)
------------------

Expand Down
4 changes: 3 additions & 1 deletion src/django_watchfiles/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ def watched_roots(self, watched_files: Iterable[Path]) -> frozenset[Path]:
extra_directories = self.directory_globs.keys()
watched_file_dirs = {f.parent for f in watched_files}
sys_paths = set(autoreload.sys_path_directories())
return frozenset((*extra_directories, *watched_file_dirs, *sys_paths))
all_dirs = (*extra_directories, *watched_file_dirs, *sys_paths)
existing_dirs = (p for p in all_dirs if p.exists())
return frozenset(existing_dirs)

def tick(self) -> Generator[None]:
self.watched_files_set = set(self.watched_files(include_globs=False))
Expand Down
8 changes: 8 additions & 0 deletions tests/test_django_watchfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ def test_tick(self):
result = self.reloader.file_filter(Change.modified, str(test_txt))
assert result is True

def test_tick_non_existent_directory_watched(self):
does_not_exist = self.temp_path / "nope"
self.reloader.watch_dir(does_not_exist, "*.txt")

iterator = self.reloader.tick()
result = next(iterator)
assert result is None


class ReplacedGetReloaderTests(SimpleTestCase):
def test_replaced_get_reloader(self):
Expand Down

0 comments on commit d7ac0f6

Please sign in to comment.