Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set a better error msg for non uft8 encoded files #5982

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions crates/reporting/src/error/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3205,21 +3205,31 @@ fn to_header_report<'a>(
let surroundings = Region::new(start, *pos);
let region = LineColumnRegion::from_pos(lines.convert_pos(*pos));

let doc = alloc.stack([
alloc.reflow(r"I am expecting a header, but got stuck here:"),
alloc.region_with_subregion(lines.convert_region(surroundings), region),
alloc.concat([
alloc.reflow("I am expecting a module keyword next, one of "),
alloc.keyword("interface"),
alloc.reflow(", "),
alloc.keyword("app"),
alloc.reflow(", "),
alloc.keyword("package"),
alloc.reflow(" or "),
alloc.keyword("platform"),
alloc.reflow("."),
]),
]);
let is_utf8 = alloc
.src_lines
.iter()
.all(|line| std::str::from_utf8(line.as_bytes()).is_ok());

let preamble = if is_utf8 {
vec![
alloc.reflow(r"I am expecting a header, but got stuck here:"),
alloc.region_with_subregion(lines.convert_region(surroundings), region),
]
} else {
vec![alloc.reflow(r"I am expecting a header, but the file is not UTF-8 encoded.")]
};

let doc = alloc.stack(preamble.into_iter().chain([alloc.concat([
alloc.reflow("I am expecting a module keyword next, one of "),
alloc.keyword("interface"),
alloc.reflow(", "),
alloc.keyword("app"),
alloc.reflow(", "),
alloc.keyword("package"),
alloc.reflow(" or "),
alloc.keyword("platform"),
alloc.reflow("."),
])]));

Report {
filename,
Expand Down