diff --git a/crates/rspack_error/src/error.rs b/crates/rspack_error/src/error.rs index ddf71b358066..866d7bc3eba3 100644 --- a/crates/rspack_error/src/error.rs +++ b/crates/rspack_error/src/error.rs @@ -101,13 +101,15 @@ impl TraceableError { let file_path = normalize_custom_filename(&source_file.name.to_string()).to_string(); let file_path = relative_to_pwd(PathBuf::from(file_path)); let file_src = source_file.src.to_string(); + let start = if start >= file_src.len() { 0 } else { start }; + let end = if end >= file_src.len() { 0 } else { end }; Self { title, kind: Default::default(), message, severity: Default::default(), src: NamedSource::new(file_path.as_os_str().to_string_lossy(), file_src), - label: SourceSpan::new(start.into(), (end - start).into()), + label: SourceSpan::new(start.into(), end.saturating_sub(start).into()), } } @@ -126,7 +128,7 @@ impl TraceableError { message, severity: Default::default(), src: NamedSource::new(file_path.as_os_str().to_string_lossy(), file_src), - label: SourceSpan::new(start.into(), (end - start).into()), + label: SourceSpan::new(start.into(), end.saturating_sub(start).into()), } } @@ -138,6 +140,8 @@ impl TraceableError { message: String, ) -> Result { let file_src = std::fs::read_to_string(path).into_diagnostic()?; + let start = if start >= file_src.len() { 0 } else { start }; + let end = if end >= file_src.len() { 0 } else { end }; Ok(Self::from_file( path.to_string_lossy().into_owned(), file_src, diff --git a/crates/rspack_error/tests/fixtures/html_syntax_error/fixtures__html_syntax_error.snap b/crates/rspack_error/tests/fixtures/html_syntax_error/fixtures__html_syntax_error.snap index edaf78e9104a..a374745184a5 100644 --- a/crates/rspack_error/tests/fixtures/html_syntax_error/fixtures__html_syntax_error.snap +++ b/crates/rspack_error/tests/fixtures/html_syntax_error/fixtures__html_syntax_error.snap @@ -13,7 +13,7 @@ expression: html_syntax_error ╭─[tests/fixtures/html_syntax_error/index.html:1:1] 1 │ 2 │ - · ──────┬───── - · ╰── Stray end tag "something" + · ▲ + · ╰── Stray end tag "something" ╰──── diff --git a/crates/rspack_error/tests/fixtures/js_syntax_error/fixtures__js_syntax_error.snap b/crates/rspack_error/tests/fixtures/js_syntax_error/fixtures__js_syntax_error.snap index d5715c203306..05bc25b8ff5c 100644 --- a/crates/rspack_error/tests/fixtures/js_syntax_error/fixtures__js_syntax_error.snap +++ b/crates/rspack_error/tests/fixtures/js_syntax_error/fixtures__js_syntax_error.snap @@ -14,7 +14,7 @@ expression: js_syntax_error ╭─[tests/fixtures/js_syntax_error/index.js:5:1] 5 │ g = CONST 6 │ } - · ┬ + · ▲ · ╰── Expression expected ╰──── diff --git a/crates/rspack_error/tests/fixtures/jsx_syntax_error/fixtures__jsx_syntax_error.snap b/crates/rspack_error/tests/fixtures/jsx_syntax_error/fixtures__jsx_syntax_error.snap index 778d8fc8a78d..112b29372552 100644 --- a/crates/rspack_error/tests/fixtures/jsx_syntax_error/fixtures__jsx_syntax_error.snap +++ b/crates/rspack_error/tests/fixtures/jsx_syntax_error/fixtures__jsx_syntax_error.snap @@ -11,5 +11,7 @@ expression: jsx_syntax_error × Error[jsx]: JSX parsing error ╭─[tests/fixtures/jsx_syntax_error/index.jsx:1:1] 1 │ let a = /test>; + · ▲ + · ╰── Unexpected eof ╰──── diff --git a/crates/rspack_error/tests/out_of_order/multiple_file_syntax_error/fixtures__multiple_file_syntax_error.snap b/crates/rspack_error/tests/out_of_order/multiple_file_syntax_error/fixtures__multiple_file_syntax_error.snap index d9fed8c9819d..1ea907171677 100644 --- a/crates/rspack_error/tests/out_of_order/multiple_file_syntax_error/fixtures__multiple_file_syntax_error.snap +++ b/crates/rspack_error/tests/out_of_order/multiple_file_syntax_error/fixtures__multiple_file_syntax_error.snap @@ -14,7 +14,7 @@ expression: multiple_file_syntax_error ╭─[tests/out_of_order/multiple_file_syntax_error/a.js:5:1] 5 │ g = CONST 6 │ } - · ┬ + · ▲ · ╰── Expression expected ╰──── × Error[javascript]: JavaScript parsing error @@ -29,7 +29,7 @@ expression: multiple_file_syntax_error ╭─[tests/out_of_order/multiple_file_syntax_error/b.js:5:1] 5 │ g = CONST 6 │ } - · ┬ + · ▲ · ╰── Expression expected ╰──── × Error[javascript]: JavaScript parsing error @@ -44,7 +44,7 @@ expression: multiple_file_syntax_error ╭─[tests/out_of_order/multiple_file_syntax_error/c.js:5:1] 5 │ g = CONST 6 │ } - · ┬ + · ▲ · ╰── Expression expected ╰──── × Error[javascript]: JavaScript parsing error @@ -59,7 +59,7 @@ expression: multiple_file_syntax_error ╭─[tests/out_of_order/multiple_file_syntax_error/d.js:5:1] 5 │ g = CONST 6 │ } - · ┬ + · ▲ · ╰── Expression expected ╰──── × Error[javascript]: JavaScript parsing error @@ -74,7 +74,7 @@ expression: multiple_file_syntax_error ╭─[tests/out_of_order/multiple_file_syntax_error/e.js:5:1] 5 │ g = CONST 6 │ } - · ┬ + · ▲ · ╰── Expression expected ╰──── × Error[javascript]: JavaScript parsing error @@ -89,7 +89,7 @@ expression: multiple_file_syntax_error ╭─[tests/out_of_order/multiple_file_syntax_error/f.js:5:1] 5 │ g = CONST 6 │ } - · ┬ + · ▲ · ╰── Expression expected ╰──── × Error[javascript]: JavaScript parsing error @@ -104,7 +104,7 @@ expression: multiple_file_syntax_error ╭─[tests/out_of_order/multiple_file_syntax_error/g.js:5:1] 5 │ g = CONST 6 │ } - · ┬ + · ▲ · ╰── Expression expected ╰──── diff --git a/packages/rspack/tests/Errors.test.ts b/packages/rspack/tests/Errors.test.ts index 0f57b7e4b222..06664c0f8836 100644 --- a/packages/rspack/tests/Errors.test.ts +++ b/packages/rspack/tests/Errors.test.ts @@ -142,7 +142,7 @@ it("should emit warnings for resolve failure in esm", async () => { Object { "errors": Array [ Object { - "formatted": " × Error[internal]: Resolve error\\n ╭─[tests/fixtures/errors/resolve-fail-esm/index.js:1:1]\\n 1 │ ╭─▶ import { answer } from './answer'\\n ╰────\\n", + "formatted": " × Error[internal]: Resolve error\\n ╭─[tests/fixtures/errors/resolve-fail-esm/index.js:1:1]\\n 1 │ import { answer } from './answer'\\n · ▲\\n · ╰── Failed to resolve ./answer in javascript/esm|/tests/fixtures/errors/resolve-fail-esm/index.js\\n ╰────\\n", "message": "Error[internal]: Resolve error\\nFailed to resolve ./answer in javascript/esm|/tests/fixtures/errors/resolve-fail-esm/index.js", }, ],