-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add BUN_WATCHER_TRACE environment variable for debugging file watcher events #23533
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
Changes from 3 commits
18ff16d
54465ee
481cabc
5f743ef
c6e028c
003a419
a458d37
96babf4
326af97
decc48d
6cc4e90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /// Optional trace file for debugging watcher events | ||
| var trace_file: ?bun.FileDescriptor = null; | ||
|
|
||
| /// Initialize trace file if BUN_WATCHER_TRACE env var is set. | ||
| /// Only checks once on first call. | ||
| pub fn init() void { | ||
| if (trace_file != null) return; | ||
|
|
||
| if (bun.getenvZ("BUN_WATCHER_TRACE")) |trace_path| { | ||
| if (trace_path.len > 0) { | ||
| const flags = bun.O.WRONLY | bun.O.CREAT | bun.O.APPEND; | ||
| const mode = 0o644; | ||
| switch (bun.sys.openA(trace_path, flags, mode)) { | ||
| .result => |fd| { | ||
| trace_file = fd; | ||
| }, | ||
| .err => { | ||
| // Silently ignore errors opening trace file | ||
| }, | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Write trace events to the trace file if enabled. | ||
| /// This is called from the watcher thread, so no locking is needed. | ||
| pub fn writeEvents(watcher: *Watcher, events: []Watcher.WatchEvent, changed_files: []?[:0]u8) void { | ||
| const fd = trace_file orelse return; | ||
|
|
||
| var buf: [16384]u8 = undefined; | ||
| var stream = std.io.fixedBufferStream(&buf); | ||
| const writer = stream.writer(); | ||
|
|
||
| // Get current timestamp | ||
| const timestamp = std.time.milliTimestamp(); | ||
|
|
||
| for (events) |event| { | ||
| stream.reset(); | ||
|
|
||
| const watchlist_slice = watcher.watchlist.slice(); | ||
| const file_paths = watchlist_slice.items(.file_path); | ||
| const file_path = if (event.index < file_paths.len) file_paths[event.index] else "(unknown)"; | ||
|
|
||
| // Write JSON manually for each event | ||
| writer.writeAll("{\"timestamp\":") catch return; | ||
| writer.print("{d}", .{timestamp}) catch return; | ||
| writer.writeAll(",\"index\":") catch return; | ||
| writer.print("{d}", .{event.index}) catch return; | ||
| writer.writeAll(",\"path\":") catch return; | ||
| writer.print("{}", .{bun.fmt.formatJSONStringUTF8(file_path, .{})}) catch return; | ||
|
|
||
| // Write individual operation flags | ||
| writer.writeAll(",\"delete\":") catch return; | ||
| writer.writeAll(if (event.op.delete) "true" else "false") catch return; | ||
| writer.writeAll(",\"write\":") catch return; | ||
| writer.writeAll(if (event.op.write) "true" else "false") catch return; | ||
| writer.writeAll(",\"rename\":") catch return; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of a bunch of booleans it should just be an array of events that happened |
||
| writer.writeAll(if (event.op.rename) "true" else "false") catch return; | ||
| writer.writeAll(",\"metadata\":") catch return; | ||
| writer.writeAll(if (event.op.metadata) "true" else "false") catch return; | ||
| writer.writeAll(",\"move_to\":") catch return; | ||
| writer.writeAll(if (event.op.move_to) "true" else "false") catch return; | ||
|
|
||
| // Add changed file names if any | ||
| const names = event.names(changed_files); | ||
| writer.writeAll(",\"changed_files\":[") catch return; | ||
| var first = true; | ||
| for (names) |name_opt| { | ||
| if (name_opt) |name| { | ||
| if (!first) writer.writeAll(",") catch return; | ||
| first = false; | ||
| writer.print("{}", .{bun.fmt.formatJSONStringUTF8(name, .{})}) catch return; | ||
| } | ||
| } | ||
| writer.writeAll("]}\n") catch return; | ||
|
|
||
| const written = stream.getWritten(); | ||
| _ = bun.sys.write(fd, written); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a BufferedWriter instead and defer flush at the end of the function scope
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| /// Close the trace file if open | ||
| pub fn deinit() void { | ||
| if (trace_file) |fd| { | ||
| fd.close(); | ||
| trace_file = null; | ||
| } | ||
| } | ||
|
|
||
| const Watcher = @import("../Watcher.zig"); | ||
| const bun = @import("../bun.zig"); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| const std = @import("std"); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete this slop and use a buffered writer with bun.sys.File