mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
This adds a configuration option to disable source code previews in error stack traces via bunfig.toml: [runtime] sourceCodePreview = false When disabled, error stack traces will still show file paths and line numbers, but will not include the source code snippets with line numbers and caret indicators that are normally displayed. Changes: - Add source_code_preview field to RuntimeOptions in cli.zig - Add parsing for [runtime].sourceCodePreview in bunfig.zig - Add source_code_preview field to VirtualMachine struct and Options - Wire up the config to disable collectSourceLines() in remapZigException - Add test verifying source code preview can be disabled Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
77 lines
2.2 KiB
TypeScript
77 lines
2.2 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { bunEnv, bunExe, tempDir } from "harness";
|
|
|
|
describe("sourceCodePreview config option", () => {
|
|
test("default behavior shows source code in error stack traces", async () => {
|
|
using dir = tempDir("source-code-preview-default", {
|
|
"test.js": `
|
|
function foo() {
|
|
console.log(new Error().stack);
|
|
}
|
|
foo();
|
|
`,
|
|
});
|
|
|
|
const proc = Bun.spawn({
|
|
cmd: [bunExe(), "test.js"],
|
|
cwd: String(dir),
|
|
env: bunEnv,
|
|
stdout: "pipe",
|
|
stderr: "pipe",
|
|
});
|
|
const stdout = await proc.stdout.text();
|
|
const exitCode = await proc.exited;
|
|
|
|
expect(exitCode).toBe(0);
|
|
// Should contain line numbers and source code
|
|
expect(stdout).toContain("test.js:");
|
|
// Should contain the function call location with source code or line number
|
|
expect(stdout.length).toBeGreaterThan(10);
|
|
});
|
|
|
|
test("sourceCodePreview=false disables source code in error stack traces", async () => {
|
|
using dir = tempDir("source-code-preview-disabled", {
|
|
"bunfig.toml": `
|
|
[runtime]
|
|
sourceCodePreview = false
|
|
`,
|
|
"test.fixture.ts": `
|
|
function foo() {
|
|
console.log(new Error().stack);
|
|
}
|
|
foo();
|
|
`,
|
|
});
|
|
|
|
const proc2 = Bun.spawn({
|
|
cmd: [bunExe(), "test.fixture.ts"],
|
|
cwd: String(dir),
|
|
env: bunEnv,
|
|
stdout: "pipe",
|
|
stderr: "pipe",
|
|
});
|
|
const stdout = await proc2.stdout.text();
|
|
const stderr = await proc2.stderr.text();
|
|
const exitCode = await proc2.exited;
|
|
|
|
expect(exitCode).toBe(0);
|
|
|
|
const output = stdout + stderr;
|
|
// Should still contain file path and line numbers
|
|
expect(output).toContain("test.fixture.ts:");
|
|
|
|
// Should NOT contain source code snippets (no pipe characters from source display)
|
|
// The source code preview typically shows lines like:
|
|
// 3 | console.log(new Error().stack);
|
|
// ^
|
|
// We check that these formatted source lines are not present
|
|
const lines = output.split("\n");
|
|
const hasSourceCodeDisplay = lines.some(
|
|
line =>
|
|
/^\s*\d+\s+\|/.test(line) || // Lines with line numbers and pipe
|
|
/^\s*\^/.test(line), // Caret indicators
|
|
);
|
|
expect(hasSourceCodeDisplay).toBe(false);
|
|
});
|
|
});
|