feat: print path of external file if it isn't valid utf-8

This commit is contained in:
Tobias Schmitz 2025-05-20 17:02:17 +02:00
parent 4cb04fd41c
commit 1c08683248
No known key found for this signature in database
2 changed files with 26 additions and 11 deletions

View File

@ -653,7 +653,7 @@ impl Loaded {
}; };
eco_vec![error] eco_vec![error]
} }
(_, Ok(lines)) => { (LoadSource::Bytes, Ok(lines)) => {
let error = if let Some(pair) = pos.line_col(&lines) { let error = if let Some(pair) = pos.line_col(&lines) {
let (line, col) = pair.numbers(); let (line, col) = pair.numbers();
error!(self.source.span, "{msg} ({error} at {line}:{col})") error!(self.source.span, "{msg} ({error} at {line}:{col})")
@ -673,12 +673,27 @@ impl Loaded {
msg: impl std::fmt::Display, msg: impl std::fmt::Display,
error: impl std::fmt::Display, error: impl std::fmt::Display,
) -> EcoVec<SourceDiagnostic> { ) -> EcoVec<SourceDiagnostic> {
let pos = pos.into(); let line_col = pos.into().try_line_col(&self.bytes).map(|p| p.numbers());
let error = if let Some(pair) = pos.try_line_col(&self.bytes) { let error = match (self.source.v, line_col) {
let (line, col) = pair.numbers(); (LoadSource::Path(file), _) => {
error!(self.source.span, "{msg} ({error} at {line}:{col})") let path = if let Some(package) = file.package() {
format!("{package}{}", file.vpath().as_rooted_path().display())
} else { } else {
format!("{}", file.vpath().as_rootless_path().display())
};
if let Some((line, col)) = line_col {
error!(self.source.span, "{msg} ({error} in {path}:{line}:{col})")
} else {
error!(self.source.span, "{msg} ({error} in {path})")
}
}
(LoadSource::Bytes, Some((line, col))) => {
error!(self.source.span, "{msg} ({error} at {line}:{col})")
}
(LoadSource::Bytes, None) => {
error!(self.source.span, "{msg} ({error})") error!(self.source.span, "{msg} ({error})")
}
}; };
eco_vec![error] eco_vec![error]
} }
@ -804,8 +819,8 @@ impl LineCol {
/// Format a user-facing error message for an XML-like file format. /// Format a user-facing error message for an XML-like file format.
pub fn format_xml_like_error(format: &str, error: roxmltree::Error) -> LoadError { pub fn format_xml_like_error(format: &str, error: roxmltree::Error) -> LoadError {
let pos = LineCol::one_based(error.pos().row as usize, error.pos().col as usize); let pos = LineCol::one_based(error.pos().row as usize, error.pos().col as usize);
let msg = eco_format!("failed to parse {format}"); let message = eco_format!("failed to parse {format}");
let err = match error { let error = match error {
roxmltree::Error::UnexpectedCloseTag(expected, actual, _) => { roxmltree::Error::UnexpectedCloseTag(expected, actual, _) => {
eco_format!("found closing tag '{actual}' instead of '{expected}'") eco_format!("found closing tag '{actual}' instead of '{expected}'")
} }
@ -821,5 +836,5 @@ pub fn format_xml_like_error(format: &str, error: roxmltree::Error) -> LoadError
err => eco_format!("{err}"), err => eco_format!("{err}"),
}; };
LoadError::new(pos, msg, err) LoadError { pos: pos.into(), message, error }
} }

View File

@ -8,5 +8,5 @@
#let data = read("/assets/text/missing.txt") #let data = read("/assets/text/missing.txt")
--- read-invalid-utf-8 --- --- read-invalid-utf-8 ---
// Error: 18-40 failed to convert to string (file is not valid utf-8 at 1:1) // Error: 18-40 failed to convert to string (file is not valid utf-8 in assets/text/bad.txt:1:1)
#let data = read("/assets/text/bad.txt") #let data = read("/assets/text/bad.txt")