From 2835ff2729d01ad7acb364a1d5854811691c1e25 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Tue, 2 Jun 2026 02:43:01 +0000 Subject: [PATCH 1/6] Record the running CLI command in crash report trace strings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The crash handler's cli_state module had storage for the one-byte command tag (CMD_CHAR) and a reader used by the trace-string encoder, but no setter existed and nothing ever stored to it — every crash report encoded '_' (pre-init) regardless of which subcommand was running. Add cli_state::set_cmd_char and call it from create_context_data, the single dispatch point every subcommand passes through, mirroring Zig's 'Cli.cmd = command' in createContextData. The Tag::char() mapping already matches bun.report's decoder byte-for-byte. Also sync Cargo.lock with the bstr dependency added to bun_bin in 90f334a46b. --- src/crash_handler/lib.rs | 8 ++++ src/runtime/cli/mod.rs | 6 ++- test/cli/run/run-crash-handler.test.ts | 57 +++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/crash_handler/lib.rs b/src/crash_handler/lib.rs index 8563ff1b3943..55008cdbcd1e 100644 --- a/src/crash_handler/lib.rs +++ b/src/crash_handler/lib.rs @@ -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 { match CMD_CHAR.load(Ordering::Relaxed) { 0 => None, diff --git a/src/runtime/cli/mod.rs b/src/runtime/cli/mod.rs index 1956fffa88d4..1e1fcbf2e300 100644 --- a/src/runtime/cli/mod.rs +++ b/src/runtime/cli/mod.rs @@ -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 only. 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); diff --git a/test/cli/run/run-crash-handler.test.ts b/test/cli/run/run-crash-handler.test.ts index 7a5d648a5977..b0fece96de0c 100644 --- a/test/cli/run/run-crash-handler.test.ts +++ b/test/cli/run/run-crash-handler.test.ts @@ -1,6 +1,6 @@ import { crash_handler } from "bun:internal-for-testing"; import { describe, expect, test } from "bun:test"; -import { bunEnv, bunExe, isDebug, isLinux, mergeWindowEnvs } from "harness"; +import { bunEnv, bunExe, isDebug, isLinux, mergeWindowEnvs, tempDir } from "harness"; import path from "path"; const { getMachOImageZeroOffset } = crash_handler; @@ -76,6 +76,61 @@ test("raise ignoring panic handler does not trigger the panic handler", async () expect(sent).toBe(false); }); +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 { + 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", + GITHUB_ACTIONS: undefined, + CI: undefined, + }, + ]), + 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