Skip to content

Commit 7d96821

Browse files
committed
watcher_mgmt: declare explicit error sets to survive comptime branch removal
CI ReleaseFast caught this: declaring `discoverWatchers`, `getActiveCwds`, and `stopWatcher` with an inferred error set (`!T`) on a non-Windows target caused the Windows-only `error.WatcherNotSupportedOnPlatform` branch to be eliminated at type-analysis time. The error then wasn't in the function's error set, so the catch-and-switch handlers in `main.zig` failed compile with: expected ... error_set, found 'error{WatcherNotSupportedOnPlatform}' The local `zig build test` (Debug) compiled fine because the catch sites are only reached on the failure path, and the Debug pipeline doesn't run the same dead-branch analysis. ReleaseFast does. Fix: declare explicit error sets at the top of `watcher_mgmt.zig`: - `PlatformError = error{WatcherNotSupportedOnPlatform}` - `StopError = error{WatcherNotSupportedOnPlatform, SignalFailed}` Then: - `discoverWatchers(...) (PlatformError || anyerror)!...` - `getActiveCwds(...) (PlatformError || anyerror)!...` - `stopWatcher(pid) StopError!void` `anyerror` on the first two covers the `runCommand` and downstream errors without needing to enumerate them. Both Debug and ReleaseFast build clean now.
1 parent d0c2110 commit 7d96821

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

src/watcher_mgmt.zig

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,16 @@ pub fn hasActiveSessions(watcher_pid: std.posix.pid_t, watcher_root: []const u8,
116116
return false;
117117
}
118118

119+
/// Errors that the Windows-unsupported watcher-management functions can
120+
/// surface to callers. Declared explicitly so the type signature still names
121+
/// `WatcherNotSupportedOnPlatform` when compiling for a non-Windows target —
122+
/// Zig's inferred error sets prune the comptime-eliminated branch otherwise,
123+
/// breaking switch-on-err blocks in callers (CI ReleaseFast caught this).
124+
pub const PlatformError = error{WatcherNotSupportedOnPlatform};
125+
pub const StopError = error{ WatcherNotSupportedOnPlatform, SignalFailed };
126+
119127
/// Run /bin/ps and discover all running codescan watchers.
120-
pub fn discoverWatchers(allocator: std.mem.Allocator) !std.ArrayListUnmanaged(WatcherInfo) {
128+
pub fn discoverWatchers(allocator: std.mem.Allocator) (PlatformError || anyerror)!std.ArrayListUnmanaged(WatcherInfo) {
121129
if (comptime builtin.os.tag == .windows) return error.WatcherNotSupportedOnPlatform;
122130

123131
const ps_output = try runCommand(allocator, &.{ "/bin/ps", "-eo", "pid,pcpu,etime,args" });
@@ -140,7 +148,7 @@ pub fn discoverWatchers(allocator: std.mem.Allocator) !std.ArrayListUnmanaged(Wa
140148
}
141149

142150
/// Get all process cwds via lsof.
143-
pub fn getActiveCwds(allocator: std.mem.Allocator) !std.ArrayListUnmanaged(LsofEntry) {
151+
pub fn getActiveCwds(allocator: std.mem.Allocator) (PlatformError || anyerror)!std.ArrayListUnmanaged(LsofEntry) {
144152
if (comptime builtin.os.tag == .windows) return error.WatcherNotSupportedOnPlatform;
145153

146154
const lsof_output = try runCommand(allocator, &.{ "/usr/sbin/lsof", "-d", "cwd" }); defer allocator.free(lsof_output);
@@ -157,7 +165,7 @@ pub fn markActiveWatchers(watchers: []WatcherInfo, cwds: []const LsofEntry) void
157165

158166
/// Stop a watcher by sending SIGTERM. Returns `error.WatcherNotSupportedOnPlatform`
159167
/// on Windows and `error.SignalFailed` if `kill(2)` returned nonzero.
160-
pub fn stopWatcher(pid: std.posix.pid_t) !void {
168+
pub fn stopWatcher(pid: std.posix.pid_t) StopError!void {
161169
if (comptime builtin.os.tag == .windows) return error.WatcherNotSupportedOnPlatform;
162170
if (std.c.kill(pid, std.posix.SIG.TERM) != 0) return error.SignalFailed;
163171
}

0 commit comments

Comments
 (0)