Skip to content

Commit

Permalink
Avoid "message too long" issue
Browse files Browse the repository at this point in the history
First, disable color in error output. Though it would be nice to have color for the "Error:" part and not for the rest, I don't think ariadne supports that.
Second, add logic to truncate the diagnostics if they are too long.
  • Loading branch information
mattfbacon committed Jan 30, 2024
1 parent 9ac4650 commit dc78abf
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions worker/src/diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::io::Write as _;
use std::ops::Range;

use ariadne::{Cache, Config, Label, Report};
Expand Down Expand Up @@ -198,12 +199,15 @@ impl ariadne::Span for Span {
}
}

const MAX_LEN: usize = 1950;

pub fn format_diagnostics(sandbox: &WithSource, diagnostics: &[SourceDiagnostic]) -> String {
let mut cache = SourceCache::new(sandbox);

let mut bytes = Vec::new();

for diagnostic in diagnostics {
let mut diagnostics = diagnostics.into_iter();
while let Some(diagnostic) = diagnostics.next() {
let typst_span = diagnostic.span;
let span = typst_span.id().map(|file_id| {
let source = sandbox
Expand All @@ -226,7 +230,7 @@ pub fn format_diagnostics(sandbox: &WithSource, diagnostics: &[SourceDiagnostic]
let report_pos = span.map_or(0, |span| span.char_span_start);

let mut report = Report::build(report_kind, source_id, report_pos)
.with_config(Config::default().with_tab_width(2).with_color(true))
.with_config(Config::default().with_tab_width(2).with_color(false))
.with_message(&diagnostic.message);

if let Some(span) = span {
Expand All @@ -239,10 +243,19 @@ pub fn format_diagnostics(sandbox: &WithSource, diagnostics: &[SourceDiagnostic]

let report = report.finish();

let checkpoint = bytes.len();
// The unwrap will never fail since `Vec`'s `Write` implementation is infallible.
report.write(&mut cache, &mut bytes).unwrap();

bytes.push(b'\n');

if bytes.len() > MAX_LEN {
bytes.truncate(checkpoint);
let more = 1 + diagnostics.count();
let s = if more == 1 { "" } else { "s" };
write!(bytes, "{more} more diagnostic{} omitted", s).unwrap();
break;
}
}

// Remove extra spacing newline.
Expand Down

0 comments on commit dc78abf

Please sign in to comment.