Skip to content

Build Runner: Implement File System Watching for kqueue #20599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
andrewrk opened this issue Jul 13, 2024 · 4 comments · Fixed by #21795
Closed

Build Runner: Implement File System Watching for kqueue #20599

andrewrk opened this issue Jul 13, 2024 · 4 comments · Fixed by #21795
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-freebsd os-macos os-netbsd os-openbsd zig build system std.Build, the build runner, `zig build` subcommand, package management
Milestone

Comments

@andrewrk
Copy link
Member

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.

else => @compileError("unimplemented"),

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

Related:

@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. contributor friendly This issue is limited in scope and/or knowledge of Zig internals. os-macos os-freebsd os-netbsd os-openbsd zig build system std.Build, the build runner, `zig build` subcommand, package management labels Jul 13, 2024
@andrewrk andrewrk added this to the 0.14.0 milestone Jul 13, 2024
@tiehuis
Copy link
Member

tiehuis commented Jul 13, 2024

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
Copy link
Member Author

andrewrk commented Jul 13, 2024

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
Copy link

ghostiam commented Aug 6, 2024

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

@andrewrk
Copy link
Member Author

andrewrk commented Sep 24, 2024

Based on that Go code, kqueue indeed allows watching directories, however those events do not provide the names of changed files within the watched directory.

https://github.com/fsnotify/fsnotify/blob/c1467c02fba575afdb5f4201072ab8403bbf00f4/backend_kqueue.go#L524-L525

This is fine for our purposes here. It will just trigger a rebuild if any files are changed when any sibling files are watched. The Go code noted above simulates file system watching by scanning watched directories when their events fire. The Zig code will not need that hack since the rebuild will already do such a scan.

This is a pretty straightforward patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-freebsd os-macos os-netbsd os-openbsd zig build system std.Build, the build runner, `zig build` subcommand, package management
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants