Tweak crash handler for linux

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Jarred Sumner
2025-08-04 23:30:34 -07:00
parent 1cbdb4c443
commit e3b2017e20
3 changed files with 51 additions and 27 deletions

View File

@@ -59,8 +59,8 @@ test("my feature", async () => {
});
const [stdout, stderr, exitCode] = await Promise.all([
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
proc.stdout.text(),
proc.stderr.text(),
proc.exited,
]);
@@ -69,6 +69,8 @@ test("my feature", async () => {
});
```
- Always use `port: 0`. Do not hardcode ports. Do not use your own random port number function.
## Code Architecture
### Language Structure
@@ -219,16 +221,16 @@ bun ci
## Important Development Notes
1. **Never use `bun test` or `bun <file>` directly** - always use `bun bd test` or `bun bd <command>`. `bun bd` compiles & runs the debug build.
2. **Use `await using`** for proper resource cleanup with Bun APIs (Bun.spawn, Bun.serve, Bun.connect, etc.)
3. **Follow existing code style** - check neighboring files for patterns
4. **Create tests in the right folder** in `test/` and the test must end in `.test.ts` or `.test.tsx`
5. **Use absolute paths** - Always use absolute paths in file operations
6. **Avoid shell commands** - Don't use `find` or `grep` in tests; use Bun's Glob and built-in tools
7. **Memory management** - In Zig code, be careful with allocators and use defer for cleanup
8. **Cross-platform** - Run `bun run zig:check-all` to compile the Zig code on all platforms when making platform-specific changes
9. **Debug builds** - Use `BUN_DEBUG_QUIET_LOGS=1` to disable debug logging, or `BUN_DEBUG_<scope>=1` to enable specific scopes
10. **Be humble & honest** - NEVER overstate what you got done or what actually works in commits, PRs or in messages to the user.
11. **Transpiled source** - Find transpiled files in `/tmp/bun-debug-src/` for debugging
2. **All changes must be tested** - if you're not testing your changes, you're not done.
3. **Get your tests to pass**. If you didn't run the tests, your code does not work.
4. **Follow existing code style** - check neighboring files for patterns
5. **Create tests in the right folder** in `test/` and the test must end in `.test.ts` or `.test.tsx`
6. **Use absolute paths** - Always use absolute paths in file operations
7. **Avoid shell commands** - Don't use `find` or `grep` in tests; use Bun's Glob and built-in tools
8. **Memory management** - In Zig code, be careful with allocators and use defer for cleanup
9. **Cross-platform** - Run `bun run zig:check-all` to compile the Zig code on all platforms when making platform-specific changes
10. **Debug builds** - Use `BUN_DEBUG_QUIET_LOGS=1` to disable debug logging, or `BUN_DEBUG_<scope>=1` to enable specific scopes
11. **Be humble & honest** - NEVER overstate what you got done or what actually works in commits, PRs or in messages to the user.
## Key APIs and Features

View File

@@ -903,6 +903,12 @@ extern "c" fn gnu_get_libc_version() ?[*:0]const u8;
export var Bun__reported_memory_size: usize = 0;
pub fn printMetadata(writer: anytype) !void {
if (comptime bun.Environment.isDebug) {
if (Output.isAIAgent()) {
return;
}
}
if (Output.enable_ansi_colors) {
try writer.writeAll(Output.prettyFmt("<r><d>", true));
}
@@ -1629,10 +1635,12 @@ pub fn dumpStackTrace(trace: std.builtin.StackTrace, limits: WriteStackTraceLimi
return;
},
.linux => {
// Linux doesnt seem to be able to decode it's own debug info.
// TODO(@paperclover): see if zig 0.14 fixes this
WTF__DumpStackTrace(trace.instruction_addresses.ptr, trace.instruction_addresses.len);
return;
if (!bun.Environment.isDebug) {
// Linux doesnt seem to be able to decode it's own debug info.
// TODO(@paperclover): see if zig 0.14 fixes this
WTF__DumpStackTrace(trace.instruction_addresses.ptr, trace.instruction_addresses.len);
return;
}
},
else => {
// Assume debug symbol tooling is reliable.
@@ -1682,25 +1690,31 @@ pub fn dumpStackTrace(trace: std.builtin.StackTrace, limits: WriteStackTraceLimi
argv.append(std.fmt.allocPrint(alloc, "0x{X}", .{line.address}) catch return) catch return;
}
// std.process is used here because bun.spawnSync with libuv does not work within
// the crash handler.
const proc = std.process.Child.run(.{
.allocator = alloc,
.argv = argv.items,
}) catch {
var child = std.process.Child.init(argv.items, alloc);
child.stdin_behavior = .Inherit;
child.stdout_behavior = .Inherit;
child.stderr_behavior = .Inherit;
child.expand_arg0 = .expand;
child.progress_node = std.Progress.Node.none;
child.spawn() catch {
stderr.print("Failed to invoke command: {s}\n", .{bun.fmt.fmtSlice(argv.items, " ")}) catch return;
if (bun.Environment.isWindows) {
stderr.print("(You can compile pdb-addr2line from https://github.com/oven-sh/bun.report, cd pdb-addr2line && cargo build)\n", .{}) catch return;
}
return;
};
if (proc.term != .Exited or proc.term.Exited != 0) {
const result = child.spawnAndWait() catch {
stderr.print("Failed to invoke command: {s}\n", .{bun.fmt.fmtSlice(argv.items, " ")}) catch return;
return;
};
if (result.term != .Exited or result.term.Exited != 0) {
stderr.print("Failed to invoke command: {s}\n", .{bun.fmt.fmtSlice(argv.items, " ")}) catch return;
return;
}
defer alloc.free(proc.stderr);
defer alloc.free(proc.stdout);
stderr.writeAll(proc.stdout) catch return;
stderr.writeAll(proc.stderr) catch return;
}
pub fn dumpCurrentStackTrace(first_address: ?usize, limits: WriteStackTraceLimits) void {

View File

@@ -81,6 +81,14 @@ await promise;
If it's several callbacks, it's okay to use callbacks. We aren't a stickler for this.
### No timeouts
**CRITICAL**: Do not set a timeout on tests. Bun already has timeouts.
### Use port 0 to get a random port
Most APIs in Bun support `port: 0` to get a random port. Never hardcode ports. Avoid using your own random port number function.
### Creating temporary files
Use `tempDirWithFiles` to create a temporary directory with files.