Skip to content

Commit f6840ea

Browse files
authored
fix(errors): Disable wrapping of text lines (#10314)
**Description:** This causes an issue for long error messages. **Related issue:** - https://linear.app/vercel/issue/NDX-967/truncated-read-more-link-for-use-client-error
1 parent 94b4c4f commit f6840ea

File tree

6 files changed

+49
-45
lines changed

6 files changed

+49
-45
lines changed

.changeset/silly-rivers-compete.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc_error_reporters: patch
4+
---
5+
6+
fix(errors): Disable wrapping of text lines

crates/swc_error_reporters/src/handler.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ fn to_miette_reporter(color: ColorConfig) -> GraphicalReportHandler {
7979
ColorConfig::Never => GraphicalReportHandler::default().with_theme(GraphicalTheme::none()),
8080
}
8181
.with_context_lines(3)
82+
.with_wrap_lines(false)
83+
.with_break_words(false)
8284
}
8385

8486
/// Try operation with a [Handler] and prints the errors as a [String] wrapped

crates/swc_error_reporters/tests/fixture.rs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,28 @@
1-
#![deny(warnings)]
2-
3-
use std::{fmt, fmt::Write, fs, path::Path};
1+
use std::{fs, path::Path};
42

3+
use anyhow::bail;
54
use swc_common::{
65
errors::{Handler, Level},
7-
sync::{Lock, Lrc},
6+
sync::Lrc,
87
BytePos, FileName, SourceMap, Span,
98
};
10-
use swc_error_reporters::PrettyEmitter;
11-
12-
#[derive(Clone, Default)]
13-
struct Writer(Lrc<Lock<String>>);
14-
15-
impl Write for Writer {
16-
fn write_str(&mut self, s: &str) -> fmt::Result {
17-
self.0.lock().write_str(s)
18-
}
19-
}
9+
use swc_error_reporters::handler::try_with_handler;
2010

2111
fn output<F>(file: &str, op: F)
2212
where
2313
F: FnOnce(Lrc<SourceMap>, &Handler),
2414
{
2515
let cm = Lrc::new(SourceMap::default());
2616

27-
let wr = Writer::default();
28-
29-
let emitter = PrettyEmitter::new(
30-
cm.clone(),
31-
Box::new(wr.clone()),
32-
Default::default(),
33-
Default::default(),
34-
);
35-
let handler = Handler::with_emitter(true, false, Box::new(emitter));
36-
37-
op(cm, &handler);
17+
let result = try_with_handler(cm.clone(), Default::default(), |handler| -> Result<(), _> {
18+
op(cm.clone(), handler);
19+
bail!("should fail");
20+
})
21+
.expect_err("should fail");
3822

3923
let output = Path::new("tests").join("fixture").join(file);
4024

41-
let s = wr.0.lock().as_str().to_string();
25+
let s = result.to_string();
4226
println!("{}", s);
4327
fs::write(output, &s).expect("failed to write");
4428
}
@@ -76,3 +60,14 @@ fn test_2() {
7660
d.emit();
7761
});
7862
}
63+
64+
#[test]
65+
fn test_long_text_wrap() {
66+
output("long_text_wrap.ans", |cm, h| {
67+
let _fm = cm.new_source_file(FileName::Anon.into(), "123456789".into());
68+
69+
let mut d = h.struct_span_err(span(1, 3), r##"You are attempting to export "metadata" from a component marked with "use client", which is disallowed. Either remove the export, or the "use client" directive. Read more: https://nextjs.org/docs/app/api-reference/directives/use-client"##);
70+
71+
d.emit();
72+
});
73+
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
2-
× test
1+
× test
32
╭────
4-
2 │ 123456789
5-
·  ─┬─
6-
· ╰── label
3+
1 │ 123456789
4+
· ─┬──
5+
· ╰── label
76
╰────
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
2-
× test
1+
× test
32
╭────
4-
2 │ 123456789
5-
·  ─┬─
6-
· ╰── label
3+
1 │ 123456789
4+
· ─┬──
5+
· ╰── label
76
╰────
8-
 help: help
7+
 help: help
98

10-
Error:
11-
⚠ sub1
9+
Warning: ⚠ sub1
1210
╭────
13-
2 │ 123456789
14-
·  ─
11+
1 │ 123456789
12+
·  ─
1513
╰────
16-
Error:
17-
⚠ sub2
14+
Warning: ⚠ sub2
1815
╭────
19-
2 │ 123456789
20-
·  ──
16+
1 │ 123456789
17+
·  ──
2118
╰────
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
× You are attempting to export "metadata" from a component marked with "use client", which is disallowed. Either remove the export, or the "use client" directive. Read more: https://nextjs.org/docs/app/api-reference/directives/use-client
2+
╭────
3+
1 │ 123456789
4+
· ──
5+
╰────

0 commit comments

Comments
 (0)