Skip to content

Commit a3b35a0

Browse files
committed
Auto merge of #14598 - yichi170:correct-error-count, r=weihanglo
fix: correct error count for `cargo check --message-format json` Hi! This is my first time contributing to Cargo. If there is anything that I need to do, please let me know! (I'm not sure whether the commit message is aligned with the Cargo's convention. If it doesn't, I'm willing to modify it!) This PR resolves the issue with incorrect error count and ensures warnings are correctly displayed when using `cargo check --message-format json`. Fixes #14472
2 parents 43e3fa9 + 71c830c commit a3b35a0

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

src/cargo/core/compiler/mod.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1867,10 +1867,19 @@ fn on_stderr_line_inner(
18671867

18681868
#[derive(serde::Deserialize)]
18691869
struct CompilerMessage {
1870+
message: String,
18701871
level: String,
18711872
}
1872-
if let Ok(message) = serde_json::from_str::<CompilerMessage>(compiler_message.get()) {
1873-
count_diagnostic(&message.level, options);
1873+
1874+
if let Ok(msg) = serde_json::from_str::<CompilerMessage>(compiler_message.get()) {
1875+
if msg.message.starts_with("aborting due to")
1876+
|| msg.message.ends_with("warning emitted")
1877+
|| msg.message.ends_with("warnings emitted")
1878+
{
1879+
// Skip this line; we'll print our own summary at the end.
1880+
return Ok(true);
1881+
}
1882+
count_diagnostic(&msg.level, options);
18741883
}
18751884

18761885
let msg = machine_message::FromCompiler {

tests/testsuite/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ fn cargo_fail_with_no_stderr() {
9595
.with_status(101)
9696
.with_stderr_data(str![[r#"
9797
[COMPILING] foo v0.5.0 ([ROOT]/foo)
98-
[ERROR] could not compile `foo` (bin "foo") due to 2 previous errors
98+
[ERROR] could not compile `foo` (bin "foo") due to 1 previous error
9999
100100
"#]])
101101
.run();

tests/testsuite/pkgid.rs

-6
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)