watchfiles icon indicating copy to clipboard operation
watchfiles copied to clipboard

Update paths from within loop?

Open t-mart opened this issue 1 year ago • 2 comments

Hi. I'm curious if anyone else would find it desirable or feasible to be able to update the list of paths watched during iteration.

For example, I'm writing a build tool that transforms source files into output files. The tool is itself configured by a file, and also has a --watch mode that triggers rebuilds on file changes in an infinite loop (to be terminated with Ctrl-C when the user is done with their development session). If you have used Javascript tools like rollup or parcel, then you are already familiar.

The issue is that the configuration file can change the paths that the watcher should watch. Therefore, I have this kinda complex outer-loop/inner-loop structure, approximated by the following:

while True:
  all_watch_paths = {**build().watch_paths, config_path}
  for changes in watchfiles.watch(*all_watch_paths):
    if config_path in changes:
      break  # so all_watch_paths can be updated
    build()

Perhaps instead, there'd be a way to yield an updater function in addition to the changed files:

all_watch_paths = {**build().watch_paths, config_path}
for changes, update in watchfiles.watch(*all_watch_paths):
  rebuild = build()
  if config_path in changes:
    update(**rebuild.watch_files)

This would allow for dynamic updates to watched files in subsequent iterations. These details would need to be ironed out, but internally, the update function would ensure any further yields are still valid with the new list. The new files could replace the old ones or be added to them.

To not break compatibility, a new function could be exposed.

I appreciate your thoughts and comments. Thank you!

t-mart avatar Sep 18 '22 20:09 t-mart

I don't think it's possible, notify (the underlying rust crate) doesn't AFAIK have an API for this, and I imagine that's because the OS frameworks for filesystem notifications don't allow you to change the list of directories you've subscribed for notifications about mid way through.

However:

  1. Starting a new watcher should be very fast
  2. Watching the full directory, and filtering out changes you don't care about should be very fast

That said, if you can find away to do what you propose with notify, I'd be happy to review a PR.

samuelcolvin avatar Sep 18 '22 21:09 samuelcolvin

Alright, I trust you're right about notify's API.

Watching the full directory

Ah, that's actually a really good idea that will work for my use case. Thanks!

t-mart avatar Sep 18 '22 22:09 t-mart