From 71c830ccef7a808c367942c3a0cdf9e330793ff6 Mon Sep 17 00:00:00 2001 From: yichi170 Date: Thu, 26 Sep 2024 23:07:44 -0500 Subject: [PATCH] fix: correct error count for `cargo check --message-format json` --- src/cargo/core/compiler/mod.rs | 13 +++++++++++-- tests/testsuite/build.rs | 2 +- tests/testsuite/pkgid.rs | 6 ------ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 83c918a0332..4592fdfa3b4 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -1867,10 +1867,19 @@ fn on_stderr_line_inner( #[derive(serde::Deserialize)] struct CompilerMessage { + message: String, 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/build.rs b/tests/testsuite/build.rs index 398f1d84ae0..3182f626124 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -95,7 +95,7 @@ fn cargo_fail_with_no_stderr() { .with_status(101) .with_stderr_data(str![[r#" [COMPILING] foo v0.5.0 ([ROOT]/foo) -[ERROR] could not compile `foo` (bin "foo") due to 2 previous errors +[ERROR] could not compile `foo` (bin "foo") due to 1 previous error "#]]) .run(); diff --git a/tests/testsuite/pkgid.rs b/tests/testsuite/pkgid.rs index bdfc4a4bfc3..02656031658 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",