ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
33.6k stars 2.46k forks source link

Build Runner: Implement File System Watching for kqueue #20599

Open andrewrk opened 1 month ago

andrewrk commented 1 month ago

Extracted from #20580.

Most of the file system watching mechanism is abstracted across operating systems, however, the parts that deal with receiving events from file system changes require OS-specific implementations.

https://github.com/ziglang/zig/blob/1d20ff11d7efc1f07de9a300abc15ad6ae9d6d6f/lib/std/Build/Watch.zig#L324

This issue is for the kqueue implementation, which covers most of the BSDs, including macOS.

Related:

tiehuis commented 1 month ago

Based on some initial reading, kqueue requires a fd for each file being watched and not simply on the parent directory fd like can be done with inotify, which will cause issues if many files need to be watched individually.

An alternative for Mac is to use FSEvents which looks to be suitable for the workflow. I am not sure if this included by default in the core libraries available to use from zig.

andrewrk commented 1 month ago

I heard from @Jarred-Sumner today that you can dlopen frameworks such as FSEvents. That might be an option to explore. It's not really a good solution though because we want something that works for all the BSDs.

I'll double check the kqueue API too. We really need directory events, it's not really possible to implement correct behavior by only watching files. I bet it's possible.

ghostiam commented 1 month ago

I found the implementation on kqueue. Think this can help us in implementation. freebsd || openbsd || netbsd || dragonfly || darwin https://github.com/fsnotify/fsnotify/blob/main/backend_kqueue.go