beancount / fava

Fava - web interface for Beancount
https://beancount.github.io/fava/
MIT License
1.98k stars 288 forks source link

WatchfilesWatcher stops watching a file that is replaced #1778

Closed josephw closed 7 months ago

josephw commented 8 months ago

The new WatchfilesWatcher implementation (fb59849ccd45) has the issue reported in https://github.com/samuelcolvin/watchfiles/issues/235 . If the file is recreated, as some editors will do on saving, it will no longer be watched.

On saving a Beancount file using vi, a single event of <Change.deleted: 3> shows up, and causes the reload indicator to show in the UI, but nothing will show after that.

The workaround suggested in that report:

I would watch the entire directory with recursive=False, then check which file changes in your own logic.

works fine. e.g.


files = ['some-file', 'another-file']

watch_dirs = set([Path(p).parent for p in files])

for changes in watch(*watch_dirs, recursive=False):
  for c in changes:
    if c[0] in [Change.added, Change.modified]:
      for f in files:
        try:
          if Path(f).samefile(c[1]):
            print(f)
        except FileNotFoundError:
          pass

Adding this approach to the existing behaviour isn't a trivial change, due to WatchfilesWatcher taking both files: Iterable[Path] and folders: Iterable[Path]. The implementation would need to determine the effective set of directories to watch, then filter the events to check for an exact match for targets specified by files, but accept anything that was specified by folder, which may require more state in _WatchfilesThread.

yagebu commented 7 months ago

Fixed in #1781, thanks @josephw