gorakhargosh / watchdog

Python library and shell utilities to monitor filesystem events.
http://packages.python.org/watchdog/
Apache License 2.0
6.49k stars 695 forks source link

silently fails if path is file #1034

Open avelican opened 6 months ago

avelican commented 6 months ago

Having written my code in a rush, I set the path to be a file rather than a directory. The code runs without warnings or errors, but fails to report modifications to the file. It seems watchdog supports only monitoring directories, not individual files.

I suggest watchdog should either: A) support events on a single file (perhaps this can be done simply by monitoring its directory and ignoring changes on everything that isn't that file) This solution seems more ergonomic from user POV. Alternatively, B) If given path is file, throw an exception and say it must be a directory

(Please forgive me if I've overlooked something, I haven't had time to take a proper look at how watchdog works yet.)

BoboTiG commented 6 months ago

What OS?

LeStahL commented 6 months ago

I see this behavior on Windows 11 as well.

prshir commented 5 months ago

Could you post your code to reproduce the silent fail? I tried to reproduce specifying file rather than a directory and then changing it. Watchdog once printed Deleted directory:and my path to file. Not sure if it's a correct behavior but at least it doesn't fail. When I changed the file observed nothing happened.

handle1337 commented 1 month ago

I tried to reproduce specifying a file rather than a directory and then change it. Watchdog once printed Deleted directory: and my path to file. Not sure if it's a correct behavior but at least it doesn't fail. When I changed the file observed nothing happened.

I was able to reproduce this with the same results on Windows 11, DirDeletedEvent(src_path='file_path\\somefile.txt', dest_path='', event_type='deleted', is_directory=True, is_synthetic=False), No events triggered after changing the target file.

I like the idea of supporting events on single files, is that something that would be in line with the project's goals?

update: clarification, in the test I am specifying a file, not dir

BoboTiG commented 1 month ago

I like the idea of supporting events on single files, is that something that would be in line with the project's goals?

Yes! I see you already wrote some code :) If you mind opening a PR, please add tests 🙏

TobiasRzepka commented 1 month ago

handle is someone else - we were just overlapped with the responses. DirDeletedEvent is triggered because CreateFileW can only monitor directories. You told it to scan a directory but now it's a file! Therefore raise an DirDeletedEvent. I will look for some tests...

TobiasRzepka commented 1 month ago

Need some more time because of vacation. Was still looking for some end to end test but didn't found anything. Am I right?

TobiasRzepka commented 1 day ago

Testcases are now added. Let me know if it's okay. Run on Win 10 and Ubuntu 24.04 without problems.