Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct error count for cargo check --message-format json #14598

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/cargo/core/compiler/mod.rs
weihanglo marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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::<CompilerMessage>(compiler_message.get()) {
count_diagnostic(&message.level, options);

if let Ok(msg) = serde_json::from_str::<CompilerMessage>(compiler_message.get()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of reusing a larger struct definition, should we just add an extra field message, and change count_diagnostic to accept three args (level, message, options) ?

Although having no data, my intuition tells me that skipping unused fields is generally more performant.

{
  "rendered": "error: expected one of `!` or `::`, found `<eof>`\n --> src/main.rs:3:1\n  |\n3 | xxx\n  | ^^^ expected one of `!` or `::`\n\n",
  "children": [], // may contain nested diagnostics
  "level": "error",
  "message": "expected one of `!` or `::`, found `<eof>`",
}

compares to

{
  "level": "error",
  "message": "expected one of `!` or `::`, found `<eof>`",
}

Copy link
Contributor Author

@yichi170 yichi170 Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just add an extra field message, and change count_diagnostic to accept three args (level, message, options) ?

Do you mean we should check the message in count_diagnostic instead of checking before passing the (level, options) to count_diagnostic?

Copy link
Member

@weihanglo weihanglo Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore the count_diagnostic part. I have no idea what I was talking about.

I meant that we may want to keep a separate CompilerMessage struct definition, since the current patch deserializes more fields than needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed!

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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 0 additions & 6 deletions tests/testsuite/pkgid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down