Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/crash_handler/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,14 @@ mod draft {
pub fn is_main_thread() -> bool {
MAIN_THREAD_ID.load(Ordering::Relaxed) == bun_threading::current_thread_id()
}

/// Zig: `Cli.cmd = command` (cli.zig `createContextData`). `bun_runtime`
/// stores `Command.Tag.char()` here at dispatch so crash-report trace
/// strings encode which subcommand was running instead of `_` (pre-init).
pub fn set_cmd_char(c: u8) {
CMD_CHAR.store(c, Ordering::Relaxed);
}

pub(crate) fn cmd_char() -> Option<u8> {
match CMD_CHAR.load(Ordering::Relaxed) {
0 => None,
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,8 +1162,12 @@ pub mod command {
log: &mut bun_ast::Log,
) -> Result<&'static mut ContextData, bun_core::Error> {
// SAFETY: single-threaded CLI startup — no other thread exists yet.
// `CMD` is read by crash-reporter / debug logging only.
// `CMD` is read by debug logging and `run_command` (feedback dispatch).
unsafe { CMD.write(Some(cmd)) };
// The crash handler can't read `CMD` (lower-tier crate); mirror the
// one-byte command tag into its `cli_state` so crash-report trace
// strings encode the running subcommand (Zig: `Cli.cmd = command`).
bun_crash_handler::cli_state::set_cmd_char(cmd.char());

let ctx = write_context_no_parse(log);

Expand Down
59 changes: 59 additions & 0 deletions test/cli/run/crash-report-command-char.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { describe, expect, test } from "bun:test";
import { bunEnv, bunExe, mergeWindowEnvs, tempDir } from "harness";
import path from "path";

// NOTE: kept separate from run-crash-handler.test.ts on purpose — that file
// is skip-listed in test/expectations.txt (its segfault-reporter test is
// broken), so anything added there never runs in CI.
describe.concurrent("crash report command character", () => {
// Crash while a given subcommand is running and return the command
// character from the trace string printed to stderr:
// {base}/{version}/{platform char}{command char}{remainder}
// Expected characters must stay in sync with `Command.Tag.char()`
// (src/options_types/command_tag.rs) and bun.report's decoder.
async function commandCharFromCrash(args: string[], cwd?: string): Promise<string> {
using server = Bun.serve({ port: 0, fetch: () => new Response("OK") });
const base = new URL(server.url).origin;

await using proc = Bun.spawn({
cmd: [bunExe(), ...args],
env: mergeWindowEnvs([
bunEnv,
{
BUN_CRASH_REPORT_URL: base,
BUN_ENABLE_CRASH_REPORTING: "1",
},
]),
cwd,
stdio: ["ignore", "pipe", "pipe"],
});
const [stderr, exitCode] = await Promise.all([proc.stderr.text(), proc.exited]);
expect(exitCode).not.toBe(0);

const trace = stderr.match(new RegExp(`${base.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}/\\S+`));
expect(trace).not.toBeNull();
const payload = new URL(trace![0]).pathname.split("/")[2];
expect(payload.length).toBeGreaterThan(2);
return payload[1];
}

const fixture = path.join(import.meta.dir, "fixture-crash.js");

test("bun <script> encodes AutoCommand", async () => {
expect(await commandCharFromCrash([fixture, "panic"])).toBe("a");
});

test("bun run <script> encodes RunCommand", async () => {
expect(await commandCharFromCrash(["run", fixture, "panic"])).toBe("r");
});

test("bun test encodes TestCommand", async () => {
using dir = tempDir("crash-report-cmd-char", {
"crash.fixture.test.js": `
import { crash_handler } from "bun:internal-for-testing";
crash_handler.panic();
`,
});
expect(await commandCharFromCrash(["test", "crash.fixture.test.js"], String(dir))).toBe("t");
});
});
Loading