-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
18ff16d
Add BUN_WATCHER_TRACE environment variable for debugging file watcher…
54465ee
[autofix.ci] apply automated fixes
autofix-ci[bot] 481cabc
Use bun.fmt for JSON escaping instead of manual implementation
5f743ef
Use BufferedWriter and events array in BUN_WATCHER_TRACE
c6e028c
Restructure trace JSON to group events by file path
003a419
Fix race condition in watcher trace test
a458d37
Add explicit Array.isArray checks and clarify comment
96babf4
Rename 'line' to 'chunk' for consistency across tests
326af97
Use comptime reflection to serialize event types
decc48d
Fix import and log flush errors
6cc4e90
[autofix.ci] apply automated fixes
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| const std = @import("std"); | ||
| const bun = @import("../bun.zig"); | ||
| const Watcher = @import("../Watcher.zig"); | ||
|
|
||
| /// Optional trace file for debugging watcher events | ||
| var trace_file: ?bun.sys.File = 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 = bun.sys.File{ .handle = 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. | ||
| /// Events are assumed to be already deduped by path. | ||
| pub fn writeEvents(watcher: *Watcher, events: []Watcher.WatchEvent, changed_files: []?[:0]u8) void { | ||
| const file = trace_file orelse return; | ||
|
|
||
| var buffered = std.io.bufferedWriter(file.writer()); | ||
| defer buffered.flush() catch {}; | ||
| const writer = buffered.writer(); | ||
|
|
||
| // Get current timestamp | ||
| const timestamp = std.time.milliTimestamp(); | ||
|
|
||
| // Write: { "timestamp": number, "files": { ... } } | ||
| writer.writeAll("{\"timestamp\":") catch return; | ||
| writer.print("{d}", .{timestamp}) catch return; | ||
| writer.writeAll(",\"files\":{") catch return; | ||
|
|
||
| const watchlist_slice = watcher.watchlist.slice(); | ||
| const file_paths = watchlist_slice.items(.file_path); | ||
|
|
||
| var first_file = true; | ||
| for (events) |event| { | ||
| const file_path = if (event.index < file_paths.len) file_paths[event.index] else "(unknown)"; | ||
| const names = event.names(changed_files); | ||
|
|
||
| if (!first_file) writer.writeAll(",") catch return; | ||
| first_file = false; | ||
|
|
||
| // Write path as key | ||
| writer.print("{}", .{bun.fmt.formatJSONStringUTF8(file_path, .{})}) catch return; | ||
| writer.writeAll(":{\"events\":[") catch return; | ||
|
|
||
| // Write array of event types | ||
| var first = true; | ||
| if (event.op.delete) { | ||
| if (!first) writer.writeAll(",") catch return; | ||
| writer.writeAll("\"delete\"") catch return; | ||
| first = false; | ||
| } | ||
| if (event.op.write) { | ||
| if (!first) writer.writeAll(",") catch return; | ||
| writer.writeAll("\"write\"") catch return; | ||
| first = false; | ||
| } | ||
| if (event.op.rename) { | ||
| if (!first) writer.writeAll(",") catch return; | ||
| writer.writeAll("\"rename\"") catch return; | ||
| first = false; | ||
| } | ||
| if (event.op.metadata) { | ||
| if (!first) writer.writeAll(",") catch return; | ||
| writer.writeAll("\"metadata\"") catch return; | ||
| first = false; | ||
| } | ||
| if (event.op.move_to) { | ||
| if (!first) writer.writeAll(",") catch return; | ||
| writer.writeAll("\"move_to\"") catch return; | ||
| first = false; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| writer.writeAll("]") catch return; | ||
|
|
||
| // Only write "changed" field if there are changed files | ||
| var has_changed = false; | ||
| for (names) |name_opt| { | ||
| if (name_opt != null) { | ||
| has_changed = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (has_changed) { | ||
| writer.writeAll(",\"changed\":[") catch return; | ||
| 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("]") catch return; | ||
| } | ||
|
|
||
| writer.writeAll("}") catch return; | ||
| } | ||
|
|
||
| writer.writeAll("}}\n") catch return; | ||
| } | ||
|
|
||
| /// Close the trace file if open | ||
| pub fn deinit() void { | ||
| if (trace_file) |file| { | ||
| file.close(); | ||
| trace_file = null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.