Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
37 changes: 34 additions & 3 deletions src/jsc/VirtualMachine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6004,6 +6004,34 @@ impl VirtualMachine {
Ok(())
}
#[inline]
fn write_caret_padding(
w: &mut bun_core::io::Writer,
line: &[u8],
gutter_width: u64,
column: usize,
) -> crate::CrateResult<()> {
use bun_core::strings::{CodepointIterator, Cursor};

splat_space(w, gutter_width)?;

// Preserve tabs at their logical positions. Supplementary codepoints
// occupy two UTF-16 code units, so emit two padding cells for them.
let codepoints = CodepointIterator::init(line);
let mut cursor = Cursor::default();
let mut codepoints_written = 0;
while codepoints_written < column && codepoints.next(&mut cursor) {
let codepoint_width = 1 + usize::from(cursor.c > 0xFFFF);
if cursor.c == '\t' as i32 {
w.write_all(b"\t")?;
} else {
splat_space(w, codepoint_width as u64)?;
}
codepoints_written += 1;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

splat_space(w, column.saturating_sub(codepoints_written) as u64)
}
#[inline]
fn count_digits(n: i32) -> u64 {
bun_core::fmt::digit_count(n) as u64
}
Expand Down Expand Up @@ -6200,9 +6228,12 @@ impl VirtualMachine {
if clamped.len() < MAX_LINE_LENGTH_WITH_DIVOT
|| (col as usize) > MAX_LINE_LENGTH_WITH_DIVOT
{
let indent =
max_line_number_pad + b" | ".len() as u64 + col.max(0) as u64;
splat_space(writer, indent)?;
write_caret_padding(
writer,
clamped,
max_line_number_pad + b" | ".len() as u64,
col.max(0) as usize,
)?;
pretty_write!(writer, "<red><b>^<r>\n")?;
} else {
writer.write_all(b"\n")?;
Expand Down
32 changes: 32 additions & 0 deletions test/js/bun/test/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,38 @@ test("err.line and err.column are set", async () => {
);
});

const supplementaryCharacter = String.fromCodePoint(0x10000);

test.concurrent.each([
["\t123 + error()", "1 | \t123 + error()", " \t ^"],
[
`"${supplementaryCharacter}"\t+ error()`,
`1 | "${supplementaryCharacter}"\t+ error()`,
" \t ^",
],
])("runtime error caret preserves source padding (#10857)", async (script, expectedSource, expectedCaret) => {
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", script],
env: { ...bunEnv, NO_COLOR: "1" },
stdout: "pipe",
stderr: "pipe",
});

const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
const lines = stderr.split(/\r?\n/);

expect({
stdout,
source: lines.find(line => line.startsWith("1 | ")),
caret: lines.find(line => line.trimEnd().endsWith("^")),
}).toEqual({
stdout: "",
source: expectedSource,
caret: expectedCaret,
});
expect(exitCode).toBe(1);
});

test("throwing inside an error suppresses the error and prints the stack", async () => {
$.throws(false);
$.env(bunEnv);
Expand Down