gmethvin / directory-watcher

A cross-platform Java recursive directory watcher, with a JNA macOS watcher and Scala better-files integration
Apache License 2.0
264 stars 34 forks source link

Watcher does not detect (re)moving a parent directory #77

Open PhilGal opened 3 years ago

PhilGal commented 3 years ago

In our project we subscribe for multiple tracked folders, and when any of them is (re)moved, the watcher closes itself, and that can be tracked in a completable future.

Example watched paths: /Users/user/parentDir/watchedDir1 /Users/user/parentDir/watchedDir2

However, when we move the parentDir out of the scope (or simply delete it), the watcher stays silent and keeps watching the basically invalid paths. When the tree is recreated, the changes are detected automatically.

This behaviour looks a bit weird as I'd expect at least some callback from the watcher. Why would it watch the invalid path?

Thank you.

gmethvin commented 3 years ago

That's interesting. It's possible the Java WatchService is not firing any events in that case, because the event occurred on the parent and not on the watched directories.

Which operating systems have you tried on?

I definitely think we need more clear and consistent behavior when roots and parents of roots get deleted.

PhilGal commented 3 years ago

I've tested it on Windows and macos, and there's a bit different behaviour. Initial condition: start watcher on some parentDir/watchedDir

Delete watchedDir

Delete parentDir/watchedDir

So, looks like macos's native library detects when the path is gone, and win's doesn't. I don't know the implementation details in depths but probably a simple periodical check that the watching path is still resolved should be sufficient.