From 505b9efeb8e4093e1512152e0788951d61199cbd Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Sat, 14 Mar 2026 18:13:26 +0100 Subject: [PATCH] fix: propagate unwatch() errors if backend.remove_watch can fail --- src/nightwatch.zig | 11 +++++++++-- src/nightwatch_test.zig | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/nightwatch.zig b/src/nightwatch.zig index 038c1db..17cb5a3 100644 --- a/src/nightwatch.zig +++ b/src/nightwatch.zig @@ -175,11 +175,18 @@ pub fn Create(comptime variant: Variant) type { } } + const UnwatchReturnType = @typeInfo(@TypeOf(Backend.remove_watch)).@"fn".return_type orelse void; + pub const UnwatchError = switch (@typeInfo(UnwatchReturnType)) { + .error_union => |info| info.error_set, + .void => error{}, + else => @compileError("invalid remove_watch return type: " ++ @typeName(UnwatchReturnType)), + }; + /// Stop watching a previously watched path. Has no effect if `path` /// was never watched. Does not unwatch subdirectories that were /// added automatically as a result of watching `path`. - pub fn unwatch(self: *@This(), path: []const u8) void { - self.interceptor.backend.remove_watch(self.allocator, path); + pub fn unwatch(self: *@This(), path: []const u8) UnwatchError!void { + return self.interceptor.backend.remove_watch(self.allocator, path); } /// Read pending events from the backend fd and deliver them to the handler. diff --git a/src/nightwatch_test.zig b/src/nightwatch_test.zig index f4256c5..785413d 100644 --- a/src/nightwatch_test.zig +++ b/src/nightwatch_test.zig @@ -395,7 +395,7 @@ fn testUnwatch(comptime Watcher: type, allocator: std.mem.Allocator) !void { try drainEvents(Watcher, &watcher); try std.testing.expect(th.hasChange(file1, .created, .file)); - watcher.unwatch(tmp); + watcher.unwatch(tmp) catch return error.TestUnexpectedResult; const count_before = th.events.items.len; const file2 = try std.fs.path.join(allocator, &.{ tmp, "after_unwatch.txt" });