Skip to content

Commit 90b68c8

Browse files
committed
fix(manifest): Show error source to users
Several parts of this are not ideal yet - Frontmatter close should show the open and show the EOF, instead of pointing at the open - Trailing content on close will usually have a newline - Multiple frontmatters should show the original frontmatter - Some content, like supported infostrings, should go in a help - Ideally we try to recover on no closing and instead point out a mismatched open/close But this is still an improvement over nothing!
1 parent 1cd5223 commit 90b68c8

16 files changed

+93
-4
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ pub fn read_manifest(
6767
let mut errors = Default::default();
6868

6969
let is_embedded = is_embedded(path);
70-
let contents = read_toml_string(path, is_embedded, gctx)
71-
.map_err(|err| ManifestError::new(err, path.into()))?;
70+
let contents = read_toml_string(path, is_embedded, gctx)?;
7271
let document = parse_document(&contents)
7372
.map_err(|e| emit_toml_diagnostic(e.into(), &contents, path, gctx))?;
7473
let original_toml = deserialize_toml(&document)
@@ -152,12 +151,13 @@ pub fn read_manifest(
152151

153152
#[tracing::instrument(skip_all)]
154153
fn read_toml_string(path: &Path, is_embedded: bool, gctx: &GlobalContext) -> CargoResult<String> {
155-
let mut contents = paths::read(path)?;
154+
let mut contents = paths::read(path).map_err(|err| ManifestError::new(err, path.into()))?;
156155
if is_embedded {
157156
if !gctx.cli_unstable().script {
158157
anyhow::bail!("parsing `{}` requires `-Zscript`", path.display());
159158
}
160-
contents = embedded::expand_manifest(&contents)?;
159+
contents = embedded::expand_manifest(&contents)
160+
.map_err(|e| emit_frontmatter_diagnostic(e, &contents, path, gctx))?;
161161
}
162162
Ok(contents)
163163
}
@@ -2777,6 +2777,31 @@ fn lints_to_rustflags(lints: &manifest::TomlLints) -> CargoResult<Vec<String>> {
27772777
Ok(rustflags)
27782778
}
27792779

2780+
fn emit_frontmatter_diagnostic(
2781+
e: crate::util::frontmatter::FrontmatterError,
2782+
contents: &str,
2783+
manifest_file: &Path,
2784+
gctx: &GlobalContext,
2785+
) -> anyhow::Error {
2786+
let span = e.span();
2787+
2788+
// Get the path to the manifest, relative to the cwd
2789+
let manifest_path = diff_paths(manifest_file, gctx.cwd())
2790+
.unwrap_or_else(|| manifest_file.to_path_buf())
2791+
.display()
2792+
.to_string();
2793+
let group = Group::with_title(Level::ERROR.primary_title(e.message())).element(
2794+
Snippet::source(contents)
2795+
.path(manifest_path)
2796+
.annotation(AnnotationKind::Primary.span(span)),
2797+
);
2798+
2799+
if let Err(err) = gctx.shell().print_report(&[group], true) {
2800+
return err.into();
2801+
}
2802+
return AlreadyPrintedError::new(e.into()).into();
2803+
}
2804+
27802805
fn emit_toml_diagnostic(
27812806
e: toml::de::Error,
27822807
contents: &str,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
[ERROR] frontmatter infostring `.toml` is unsupported by cargo; specify `cargo` for embedding a manifest
2+
--> script:1:4
3+
|
4+
1 | ---.toml
5+
| ^^^^^
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
[ERROR] frontmatter infostring `Cargo.toml` is unsupported by cargo; specify `cargo` for embedding a manifest
2+
--> script:1:4
3+
|
4+
1 | ---Cargo.toml
5+
| ^^^^^^^^^^
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
[ERROR] trailing characters found after frontmatter close
2+
--> script:2:4
3+
|
4+
2 | ---cargo
5+
| ____^
6+
3 | | //~^ ERROR: extra characters after frontmatter close are not allowed
7+
| |_^
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
[ERROR] no closing `---` found for frontmatter
2+
--> script:1:1
3+
|
4+
1 | ---cargo
5+
| ^^^
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
[ERROR] frontmatter infostring `-toml` is unsupported by cargo; specify `cargo` for embedding a manifest
2+
--> script:1:5
3+
|
4+
1 | --- -toml
5+
| ^^^^^
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
[ERROR] frontmatter infostring `Cargo-toml` is unsupported by cargo; specify `cargo` for embedding a manifest
2+
--> script:1:5
3+
|
4+
1 | --- Cargo-toml
5+
| ^^^^^^^^^^
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
[ERROR] cargo does not support frontmatter infostring attributes like `clippy` at this time
2+
--> script:1:4
3+
|
4+
1 | ---cargo,clippy
5+
| ^^^^^^^^^^^^
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
[ERROR] trailing characters found after frontmatter close
2+
--> script:3:4
3+
|
4+
3 | ----
5+
| ____^
6+
4 | |
7+
| |_^
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
[ERROR] no closing `----` found for frontmatter
2+
--> script:1:1
3+
|
4+
1 | ----cargo
5+
| ^^^^

0 commit comments

Comments
 (0)