beancount/fava

WatchfilesWatcher stops watching a file that is replaced

josephw opened this issue · 1 comments

The new WatchfilesWatcher implementation (fb59849) has the issue reported in samuelcolvin/watchfiles#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.

Fixed in #1781, thanks @josephw