Subject: bug#51146: 29.0.50; file-notify-add-watch not reporting changes of files

From: Carlos Pita <[email-masked]>
Date: Tue, 12 Oct 2021 11:41:36 -0300

Hi Eli,

> IMO, this is a bug in macOS: the kqueue feature is incomplete and
> unreliable.  I suggest to file a bug report with kqueue developers
> about that.  I don't see how Emacs can do anything to improve the
> situation on macOS regarding file notifications, as long as macOS
> doesn't get their act together and fix this.

I agree there's not a lot that Emacs can do here. I disagree in than
it's a bug in macOS. Although I'm not an expert in the matter from what
I have been reading this is a fact about watchers implemented on top of
kqueue. See for example this remark in the documentation of fswatch [1]:

    The kqueue monitor requires a file descriptor to be opened for every
    file being watched. As a result, this monitor scales badly with the
    number of files being observed and may begin to misbehave as soon as
    the fswatch process runs out of file descriptors. In this case,
    fswatch dumps one error on standard error for every file that cannot
    be opened so that users are notified and can take action, including
    terminating the fswatch session. Beware that on some systems the
    maximum number of file descriptors that can be opened by a process
    is set to a very low value (values as low as 256 are not uncommon),
    even if the operating system may allow a much larger value.

They also present a number of alternatives:

    - Consider raising the number of maximum open file descriptors.
    - Consider using the (--directories, -d) option.
    - Consider using another monitor.

The --directories option:

    Request the monitor to watch directories only during a recursive
    scan. This feature helps reducing the number of open file
    descriptors if a generic change event for a directory is acceptable
    instead of events on directory children.

So it's not a strategy that fully addresses the problem and, moreover,
it will miss directory changes that are part of the contract of
file-notify-add-watch anyway.

Besides the aforementioned problems of scalability, watching each file
would lead to a more complex implementation that updates the list of
watched files each time an add/remove event is detected.

Is there is no other option I sympathize with the idea of leaving emacs
alone, but the documentation is still lacking and should be very clear
about what to expect in the case of kqueue (as the documentation quoted
above is). Also the documentation in the manual and in the docstring
should converge.

Best regards,
Carlos

---

[1] http://emcrisostomo.github.io/fswatch/doc/1.16.0/fswatch.html/Monitors.html#Peculiarities-1