Skip to content

Commit 5a48d41

Browse files
authored
fix(cli/fmt_errors): don't panic on source line formatting errors (#12449)
Returns empty values in case of errors, source lines are non-essential anyway. These errors can happen e.g. when source files change at runtime. A warning is also printed to help us track when it happens in unexpected cases besides this.
1 parent 0a7ba33 commit 5a48d41

6 files changed

+36
-2
lines changed

cli/fmt_errors.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,23 @@ fn format_maybe_source_line(
177177
if source_line.is_empty() || source_line.len() > SOURCE_ABBREV_THRESHOLD {
178178
return "".to_string();
179179
}
180+
if source_line.contains("Couldn't format source line: ") {
181+
return format!("\n{}", source_line);
182+
}
180183

181184
assert!(start_column.is_some());
182185
assert!(end_column.is_some());
183186
let mut s = String::new();
184187
let start_column = start_column.unwrap();
185188
let end_column = end_column.unwrap();
189+
190+
if start_column as usize >= source_line.len() {
191+
return format!(
192+
"\n{} Couldn't format source line: Column {} is out of bounds (source may have changed at runtime)",
193+
crate::colors::yellow("Warning"), start_column + 1,
194+
);
195+
}
196+
186197
// TypeScript uses `~` always, but V8 would utilise `^` always, even when
187198
// doing ranges, so here, if we only have one marker (very common with V8
188199
// errors) we will use `^` instead.

cli/proc_state.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,14 @@ impl SourceMapGetter for ProcState {
622622
// Do NOT use .lines(): it skips the terminating empty line.
623623
// (due to internally using .split_terminator() instead of .split())
624624
let lines: Vec<&str> = out.source.split('\n').collect();
625-
assert!(lines.len() > line_number);
626-
lines[line_number].to_string()
625+
if line_number >= lines.len() {
626+
format!(
627+
"{} Couldn't format source line: Line {} is out of bounds (source may have changed at runtime)",
628+
crate::colors::yellow("Warning"), line_number + 1,
629+
)
630+
} else {
631+
lines[line_number].to_string()
632+
}
627633
})
628634
} else {
629635
None

cli/tests/integration/run_tests.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,3 +1916,9 @@ itest!(long_data_url_formatting {
19161916
output: "long_data_url_formatting.ts.out",
19171917
exit_code: 1,
19181918
});
1919+
1920+
itest!(eval_context_throw_with_conflicting_source {
1921+
args: "run eval_context_throw_with_conflicting_source.ts",
1922+
output: "eval_context_throw_with_conflicting_source.ts.out",
1923+
exit_code: 1,
1924+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw new Error("foo");
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// deno-lint-ignore no-explicit-any
2+
const [, errorInfo] = (Deno as any).core.evalContext(
3+
'/* aaaaaaaaaaaaaaaaa */ throw new Error("foo")',
4+
new URL("eval_context_conflicting_source.ts", import.meta.url).href,
5+
);
6+
throw errorInfo.thrown;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[WILDCARD]error: Uncaught Error: foo
2+
Warning Couldn't format source line: Column 31 is out of bounds (source may have changed at runtime)
3+
at file:///[WILDCARD]/eval_context_conflicting_source.ts:1:31
4+
at file:///[WILDCARD]/eval_context_throw_with_conflicting_source.ts:[WILDCARD]

0 commit comments

Comments
 (0)