From 90f84e3c62207e67c61139aacbceae98a32e042f Mon Sep 17 00:00:00 2001 From: yichi170 Date: Wed, 25 Sep 2024 18:46:15 -0500 Subject: [PATCH] fix: correct error count for `cargo check --message-format json` --- src/cargo/core/compiler/mod.rs | 69 ++++++++++++++++++---------------- tests/testsuite/pkgid.rs | 6 --- 2 files changed, 36 insertions(+), 39 deletions(-) diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 83c918a0332e..98bfb2663ea5 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -1742,6 +1742,33 @@ fn on_stderr_line_inner( return Ok(true); } + #[derive(serde::Deserialize)] + struct CompilerMessage { + rendered: String, + message: String, + level: String, + children: Vec, + } + + // A partial rustfix::diagnostics::Diagnostic. We deserialize only a + // subset of the fields because rustc's output can be extremely + // deeply nested JSON in pathological cases involving macro + // expansion. Rustfix's Diagnostic struct is recursive containing a + // field `children: Vec`, and it can cause deserialization to + // hit serde_json's default recursion limit, or overflow the stack + // if we turn that off. Cargo only cares about the 1 field listed + // here. + #[derive(serde::Deserialize)] + struct PartialDiagnostic { + spans: Vec, + } + + // A partial rustfix::diagnostics::DiagnosticSpan. + #[derive(serde::Deserialize)] + struct PartialDiagnosticSpan { + suggestion_applicability: Option, + } + // Depending on what we're emitting from Cargo itself, we figure out what to // do with this JSON message. match options.format { @@ -1755,33 +1782,6 @@ fn on_stderr_line_inner( render_diagnostics: true, .. } => { - #[derive(serde::Deserialize)] - struct CompilerMessage { - rendered: String, - message: String, - level: String, - children: Vec, - } - - // A partial rustfix::diagnostics::Diagnostic. We deserialize only a - // subset of the fields because rustc's output can be extremely - // deeply nested JSON in pathological cases involving macro - // expansion. Rustfix's Diagnostic struct is recursive containing a - // field `children: Vec`, and it can cause deserialization to - // hit serde_json's default recursion limit, or overflow the stack - // if we turn that off. Cargo only cares about the 1 field listed - // here. - #[derive(serde::Deserialize)] - struct PartialDiagnostic { - spans: Vec, - } - - // A partial rustfix::diagnostics::DiagnosticSpan. - #[derive(serde::Deserialize)] - struct PartialDiagnosticSpan { - suggestion_applicability: Option, - } - if let Ok(mut msg) = serde_json::from_str::(compiler_message.get()) { if msg.message.starts_with("aborting due to") || msg.message.ends_with("warning emitted") @@ -1865,12 +1865,15 @@ fn on_stderr_line_inner( return Ok(true); } - #[derive(serde::Deserialize)] - struct CompilerMessage { - level: String, - } - if let Ok(message) = serde_json::from_str::(compiler_message.get()) { - count_diagnostic(&message.level, options); + if let Ok(msg) = serde_json::from_str::(compiler_message.get()) { + if msg.message.starts_with("aborting due to") + || msg.message.ends_with("warning emitted") + || msg.message.ends_with("warnings emitted") + { + // Skip this line; we'll print our own summary at the end. + return Ok(true); + } + count_diagnostic(&msg.level, options); } let msg = machine_message::FromCompiler { diff --git a/tests/testsuite/pkgid.rs b/tests/testsuite/pkgid.rs index bdfc4a4bfc35..02656031658c 100644 --- a/tests/testsuite/pkgid.rs +++ b/tests/testsuite/pkgid.rs @@ -327,12 +327,6 @@ fn pkgid_json_message_metadata_consistency() { "reason": "compiler-message", "...": "{...}" }, - { - "manifest_path": "[ROOT]/foo/Cargo.toml", - "package_id": "path+[ROOTURL]/foo#0.5.0", - "reason": "compiler-message", - "...": "{...}" - }, { "manifest_path": "[ROOT]/foo/Cargo.toml", "package_id": "path+[ROOTURL]/foo#0.5.0",