Skip to content

Commit 90f84e3

Browse files
committed
fix: correct error count for cargo check --message-format json
1 parent 01e1ab5 commit 90f84e3

File tree

2 files changed

+36
-39
lines changed

2 files changed

+36
-39
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,33 @@ fn on_stderr_line_inner(
17421742
return Ok(true);
17431743
}
17441744

1745+
#[derive(serde::Deserialize)]
1746+
struct CompilerMessage {
1747+
rendered: String,
1748+
message: String,
1749+
level: String,
1750+
children: Vec<PartialDiagnostic>,
1751+
}
1752+
1753+
// A partial rustfix::diagnostics::Diagnostic. We deserialize only a
1754+
// subset of the fields because rustc's output can be extremely
1755+
// deeply nested JSON in pathological cases involving macro
1756+
// expansion. Rustfix's Diagnostic struct is recursive containing a
1757+
// field `children: Vec<Self>`, and it can cause deserialization to
1758+
// hit serde_json's default recursion limit, or overflow the stack
1759+
// if we turn that off. Cargo only cares about the 1 field listed
1760+
// here.
1761+
#[derive(serde::Deserialize)]
1762+
struct PartialDiagnostic {
1763+
spans: Vec<PartialDiagnosticSpan>,
1764+
}
1765+
1766+
// A partial rustfix::diagnostics::DiagnosticSpan.
1767+
#[derive(serde::Deserialize)]
1768+
struct PartialDiagnosticSpan {
1769+
suggestion_applicability: Option<Applicability>,
1770+
}
1771+
17451772
// Depending on what we're emitting from Cargo itself, we figure out what to
17461773
// do with this JSON message.
17471774
match options.format {
@@ -1755,33 +1782,6 @@ fn on_stderr_line_inner(
17551782
render_diagnostics: true,
17561783
..
17571784
} => {
1758-
#[derive(serde::Deserialize)]
1759-
struct CompilerMessage {
1760-
rendered: String,
1761-
message: String,
1762-
level: String,
1763-
children: Vec<PartialDiagnostic>,
1764-
}
1765-
1766-
// A partial rustfix::diagnostics::Diagnostic. We deserialize only a
1767-
// subset of the fields because rustc's output can be extremely
1768-
// deeply nested JSON in pathological cases involving macro
1769-
// expansion. Rustfix's Diagnostic struct is recursive containing a
1770-
// field `children: Vec<Self>`, and it can cause deserialization to
1771-
// hit serde_json's default recursion limit, or overflow the stack
1772-
// if we turn that off. Cargo only cares about the 1 field listed
1773-
// here.
1774-
#[derive(serde::Deserialize)]
1775-
struct PartialDiagnostic {
1776-
spans: Vec<PartialDiagnosticSpan>,
1777-
}
1778-
1779-
// A partial rustfix::diagnostics::DiagnosticSpan.
1780-
#[derive(serde::Deserialize)]
1781-
struct PartialDiagnosticSpan {
1782-
suggestion_applicability: Option<Applicability>,
1783-
}
1784-
17851785
if let Ok(mut msg) = serde_json::from_str::<CompilerMessage>(compiler_message.get()) {
17861786
if msg.message.starts_with("aborting due to")
17871787
|| msg.message.ends_with("warning emitted")
@@ -1865,12 +1865,15 @@ fn on_stderr_line_inner(
18651865
return Ok(true);
18661866
}
18671867

1868-
#[derive(serde::Deserialize)]
1869-
struct CompilerMessage {
1870-
level: String,
1871-
}
1872-
if let Ok(message) = serde_json::from_str::<CompilerMessage>(compiler_message.get()) {
1873-
count_diagnostic(&message.level, options);
1868+
if let Ok(msg) = serde_json::from_str::<CompilerMessage>(compiler_message.get()) {
1869+
if msg.message.starts_with("aborting due to")
1870+
|| msg.message.ends_with("warning emitted")
1871+
|| msg.message.ends_with("warnings emitted")
1872+
{
1873+
// Skip this line; we'll print our own summary at the end.
1874+
return Ok(true);
1875+
}
1876+
count_diagnostic(&msg.level, options);
18741877
}
18751878

18761879
let msg = machine_message::FromCompiler {

tests/testsuite/pkgid.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,6 @@ fn pkgid_json_message_metadata_consistency() {
327327
"reason": "compiler-message",
328328
"...": "{...}"
329329
},
330-
{
331-
"manifest_path": "[ROOT]/foo/Cargo.toml",
332-
"package_id": "path+[ROOTURL]/foo#0.5.0",
333-
"reason": "compiler-message",
334-
"...": "{...}"
335-
},
336330
{
337331
"manifest_path": "[ROOT]/foo/Cargo.toml",
338332
"package_id": "path+[ROOTURL]/foo#0.5.0",

0 commit comments

Comments
 (0)