Merge pull request #57 from zkat/main

Fix multifile miette crash
This commit is contained in:
JT 2021-09-22 13:02:43 +12:00 committed by GitHub
commit 673137be8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 9 deletions

5
Cargo.lock generated
View File

@ -339,7 +339,7 @@ checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
[[package]]
name = "miette"
version = "3.0.1-alpha.0"
source = "git+https://github.com/zkat/miette?rev=183ecb9#183ecb9b78a1c22d832e979db5054dcac36d8b7a"
source = "git+https://github.com/zkat/miette?branch=release/3.0.0#491ce7c0ce1f04c9b6fc09c250f188c1ec77df53"
dependencies = [
"atty",
"backtrace",
@ -354,13 +354,12 @@ dependencies = [
"term_size",
"textwrap",
"thiserror",
"unicode-width",
]
[[package]]
name = "miette-derive"
version = "3.0.0-alpha.0"
source = "git+https://github.com/zkat/miette?rev=183ecb9#183ecb9b78a1c22d832e979db5054dcac36d8b7a"
source = "git+https://github.com/zkat/miette?branch=release/3.0.0#491ce7c0ce1f04c9b6fc09c250f188c1ec77df53"
dependencies = [
"proc-macro2",
"quote",

View File

@ -16,7 +16,7 @@ nu-engine = { path="./crates/nu-engine" }
nu-parser = { path="./crates/nu-parser" }
nu-protocol = { path = "./crates/nu-protocol" }
nu-table = { path = "./crates/nu-table" }
miette = { git = "https://github.com/zkat/miette", rev = "183ecb9" }
miette = { git = "https://github.com/zkat/miette", branch = "release/3.0.0" }
# miette = { path = "../miette" }
# mimalloc = { version = "*", default-features = false }

View File

@ -7,7 +7,7 @@ edition = "2018"
nu-engine = { path = "../nu-engine" }
nu-parser = { path = "../nu-parser" }
nu-protocol = { path = "../nu-protocol" }
miette = { git = "https://github.com/zkat/miette", rev = "183ecb9", features = ["fancy"] }
miette = { git = "https://github.com/zkat/miette", branch = "release/3.0.0", features = ["fancy"] }
# miette = { path = "../../../miette", features = ["fancy"] }
thiserror = "1.0.29"
nu-ansi-term = "0.36.0"

View File

@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2018"
[dependencies]
miette = { git = "https://github.com/zkat/miette", rev = "183ecb9" }
miette = { git = "https://github.com/zkat/miette", branch = "release/3.0.0" }
# miette = { path = "../../../miette" }
thiserror = "1.0.29"
nu-protocol = { path = "../nu-protocol"}

View File

@ -7,5 +7,5 @@ edition = "2018"
[dependencies]
thiserror = "1.0.29"
miette = { git = "https://github.com/zkat/miette", rev = "183ecb9" }
miette = { git = "https://github.com/zkat/miette", branch = "release/3.0.0" }
# miette = { path = "../../../miette" }

View File

@ -556,8 +556,20 @@ impl<'a> miette::SourceCode for &StateWorkingSet<'a> {
context_lines_before: usize,
context_lines_after: usize,
) -> Result<Box<dyn miette::SpanContents + 'b>, miette::MietteError> {
let debugging = std::env::var("MIETTE_DEBUG").is_ok();
if debugging {
let finding_span = "Finding span in StateWorkingSet";
dbg!(finding_span, span);
}
for (filename, start, end) in self.files() {
if debugging {
dbg!(&filename, start, end);
}
if span.offset() >= *start && span.offset() + span.len() <= *end {
if debugging {
let found_file = "Found matching file";
dbg!(found_file);
}
let our_span = Span {
start: *start,
end: *end,
@ -565,7 +577,13 @@ impl<'a> miette::SourceCode for &StateWorkingSet<'a> {
// We need to move to a local span because we're only reading
// the specific file contents via self.get_span_contents.
let local_span = (span.offset() - *start, span.len()).into();
if debugging {
dbg!(&local_span);
}
let span_contents = self.get_span_contents(our_span);
if debugging {
dbg!(String::from_utf8_lossy(span_contents));
}
let span_contents = span_contents.read_span(
&local_span,
context_lines_before,
@ -574,19 +592,31 @@ impl<'a> miette::SourceCode for &StateWorkingSet<'a> {
let content_span = span_contents.span();
// Back to "global" indexing
let retranslated = (content_span.offset() + start, content_span.len()).into();
if debugging {
dbg!(&retranslated);
}
let data = span_contents.data();
if filename == "<cli>" {
if debugging {
let success_cli = "Successfully read CLI span";
dbg!(success_cli, String::from_utf8_lossy(data));
}
return Ok(Box::new(miette::MietteSpanContents::new(
span_contents.data(),
data,
retranslated,
span_contents.line(),
span_contents.column(),
span_contents.line_count(),
)));
} else {
if debugging {
let success_file = "Successfully read file span";
dbg!(success_file);
}
return Ok(Box::new(miette::MietteSpanContents::new_named(
filename.clone(),
span_contents.data(),
data,
retranslated,
span_contents.line(),
span_contents.column(),