Skip to content

Commit d9f9619

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

File tree

1 file changed

+51
-33
lines changed

1 file changed

+51
-33
lines changed

src/cargo/core/compiler/mod.rs

+51-33
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,30 @@ 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+
let rendered = msg.rendered;
1877+
if options.show_diagnostics {
1878+
let machine_applicable: bool = msg
1879+
.children
1880+
.iter()
1881+
.map(|child| {
1882+
child
1883+
.spans
1884+
.iter()
1885+
.filter_map(|span| span.suggestion_applicability)
1886+
.any(|app| app == Applicability::MachineApplicable)
1887+
})
1888+
.any(|b| b);
1889+
count_diagnostic(&msg.level, options);
1890+
state.emit_diag(msg.level, rendered, machine_applicable)?;
1891+
}
18741892
}
18751893

18761894
let msg = machine_message::FromCompiler {

0 commit comments

Comments
 (0)