Skip to content

Commit f3f1749

Browse files
committed
fix(fix): Switch from ad-hoc to structured warnings
1 parent d916207 commit f3f1749

3 files changed

Lines changed: 92 additions & 139 deletions

File tree

src/cargo/util/diagnostic_server.rs

Lines changed: 48 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
//! cross-platform way for the `cargo fix` command.
33
44
use std::collections::HashSet;
5-
use std::fmt::Write as _;
65
use std::io::{BufReader, Read, Write};
76
use std::net::{Shutdown, SocketAddr, TcpListener, TcpStream};
87
use std::path::PathBuf;
98
use std::sync::Arc;
109
use std::sync::atomic::{AtomicBool, Ordering};
1110
use std::thread::{self, JoinHandle};
1211

12+
use annotate_snippets::Group;
13+
use annotate_snippets::Level;
14+
use annotate_snippets::Origin;
1315
use anyhow::{Context as _, Error};
1416
use cargo_util::ProcessBuilder;
1517
use serde::{Deserialize, Serialize};
@@ -128,15 +130,15 @@ impl<'a> DiagnosticPrinter<'a> {
128130
Message::ReplaceFailed { file, message } => {
129131
let issue_link = get_bug_report_url(self.workspace_wrapper);
130132

131-
let mut msg = format!("error applying suggestions to `{file}`\n");
132-
writeln!(&mut msg)?;
133-
writeln!(&mut msg, "The full error message was:")?;
134-
writeln!(&mut msg)?;
135-
writeln!(&mut msg, "> {message}")?;
136-
writeln!(&mut msg)?;
137-
writeln!(&mut msg, "{}", gen_please_report_this_bug_text(issue_link))?;
138-
writeln!(&mut msg, "{}", gen_suggest_broken_code())?;
139-
self.gctx.shell().warn(&msg)?;
133+
let report = &[
134+
Level::ERROR
135+
.secondary_title("error applying suggestions")
136+
.element(Origin::path(file))
137+
.element(Level::ERROR.with_name("cause").message(message)),
138+
gen_please_report_this_bug_group(issue_link),
139+
gen_suggest_broken_code_group(),
140+
];
141+
self.gctx.shell().print_report(report, false)?;
140142
Ok(())
141143
}
142144
Message::FixFailed {
@@ -152,42 +154,34 @@ impl<'a> DiagnosticPrinter<'a> {
152154
};
153155
let issue_link = get_bug_report_url(self.workspace_wrapper);
154156

155-
let mut msg =
156-
format!("failed to automatically apply fixes suggested by rustc{to_crate}\n");
157-
if !files.is_empty() {
158-
writeln!(&mut msg)?;
159-
writeln!(
160-
&mut msg,
161-
"after fixes were automatically applied the compiler \
162-
reported errors within these files:"
163-
)?;
164-
writeln!(&mut msg)?;
165-
for file in files {
166-
writeln!(&mut msg, " * {file}")?;
167-
}
168-
writeln!(&mut msg)?;
169-
}
170-
write!(
171-
&mut msg,
172-
"{}\n",
173-
gen_please_report_this_bug_text(issue_link)
174-
)?;
175-
write!(&mut msg, "{}\n\n", gen_suggest_broken_code())?;
176-
if !errors.is_empty() {
177-
writeln!(&mut msg, "The following errors were reported:")?;
178-
for error in errors {
179-
write!(&mut msg, "{error}")?;
180-
if !error.ends_with('\n') {
181-
writeln!(&mut msg)?;
182-
}
183-
}
184-
}
185-
if let Some(exit) = abnormal_exit {
186-
writeln!(&mut msg, "rustc exited abnormally: {exit}")?;
187-
}
188-
writeln!(&mut msg, "Original diagnostics will follow.")?;
157+
let cause_message = if !errors.is_empty() {
158+
Some(errors.join("\n").trim().to_owned())
159+
} else {
160+
None
161+
};
162+
163+
let report = &[
164+
Level::ERROR
165+
.secondary_title(format!("errors present after applying fixes{to_crate}"))
166+
.elements(files.iter().map(|f| Origin::path(f)))
167+
.elements(
168+
cause_message
169+
.into_iter()
170+
.map(|err| Level::ERROR.with_name("cause").message(err)),
171+
)
172+
.elements(abnormal_exit.iter().map(|exit| {
173+
Level::ERROR
174+
.with_name("cause")
175+
.message(format!("rustc exited abnormally: {exit}"))
176+
})),
177+
gen_please_report_this_bug_group(issue_link),
178+
gen_suggest_broken_code_group(),
179+
Group::with_title(
180+
Level::NOTE.secondary_title("original diagnostics will follow:"),
181+
),
182+
];
189183

190-
self.gctx.shell().warn(&msg)?;
184+
self.gctx.shell().print_report(report, false)?;
191185
Ok(())
192186
}
193187
Message::EditionAlreadyEnabled { message, edition } => {
@@ -223,22 +217,17 @@ https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-proje
223217
}
224218
}
225219

226-
fn gen_please_report_this_bug_text(url: &str) -> String {
227-
format!(
228-
"This likely indicates a bug in either rustc or cargo itself,\n\
229-
and we would appreciate a bug report! You're likely to see\n\
230-
a number of compiler warnings after this message which cargo\n\
231-
attempted to fix but failed. If you could open an issue at\n\
232-
{url}\n\
233-
quoting the full output of this command we'd be very appreciative!\
234-
",
235-
)
220+
fn gen_please_report_this_bug_group(url: &str) -> Group<'static> {
221+
Group::with_title(Level::HELP.secondary_title(format!(
222+
"to report this as a bug, open an issue at {url}, quoting the full output of this command"
223+
)))
236224
}
237225

238-
fn gen_suggest_broken_code() -> &'static str {
239-
"Note that you may be able to make some more progress in the near-term\n\
240-
fixing code with the `--broken-code` flag\
241-
"
226+
fn gen_suggest_broken_code_group() -> Group<'static> {
227+
Group::with_title(
228+
Level::HELP
229+
.secondary_title("to possibly apply more fixes, pass in the `--broken-code` flag"),
230+
)
242231
}
243232

244233
fn get_bug_report_url(rustc_workspace_wrapper: &Option<PathBuf>) -> &str {

tests/testsuite/fix.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,9 +1429,17 @@ fn fix_to_broken_code() {
14291429
.cwd("bar")
14301430
.env("RUSTC", p.root().join("foo/target/debug/foo"))
14311431
.with_stderr_data(str![[r#"
1432+
[COMPILING] bar v0.1.0 ([ROOT]/foo/bar)
1433+
[ERROR] errors present after applying fixes to crate `bar`
1434+
|
1435+
= cause: thread 'main' ([..]) panicked at src/main.rs:23:29:
1436+
explicit panic
1437+
[NOTE] run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1438+
[HELP] to report this as a bug, open an issue at https://github.com/rust-lang/rust/issues, quoting the full output of this command
1439+
[HELP] to possibly apply more fixes, pass in the `--broken-code` flag
1440+
[NOTE] original diagnostics will follow:
14321441
...
1433-
[WARNING] failed to automatically apply fixes suggested by rustc to crate `bar`
1434-
...
1442+
14351443
"#]])
14361444
.run();
14371445

@@ -1772,13 +1780,18 @@ fn abnormal_exit() {
17721780
)
17731781
// "signal: 6, SIGABRT: process abort signal" on some platforms
17741782
.with_stderr_data(str![[r#"
1783+
[LOCKING] 1 package to latest compatible version
1784+
[COMPILING] pm v0.1.0 ([ROOT]/foo/pm)
1785+
[CHECKING] foo v0.1.0 ([ROOT]/foo)
1786+
[ERROR] errors present after applying fixes to crate `foo`
1787+
|
1788+
= cause: I'm not a diagnostic.
1789+
= cause: rustc exited abnormally: [..]
1790+
[HELP] to report this as a bug, open an issue at https://github.com/rust-lang/rust/issues, quoting the full output of this command
1791+
[HELP] to possibly apply more fixes, pass in the `--broken-code` flag
1792+
[NOTE] original diagnostics will follow:
17751793
...
1776-
[WARNING] failed to automatically apply fixes suggested by rustc to crate `foo`
1777-
...
1778-
I'm not a diagnostic.
1779-
rustc exited abnormally: [..]
1780-
Original diagnostics will follow.
1781-
...
1794+
17821795
"#]])
17831796
.run();
17841797
}

tests/testsuite/fix_n_times.rs

Lines changed: 23 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -327,21 +327,11 @@ fn fix_overlapping_max() {
327327
|_execs| {},
328328
str![[r#"
329329
[CHECKING] foo v0.0.1 ([ROOT]/foo)
330-
[WARNING] error applying suggestions to `src/lib.rs`
331-
332-
The full error message was:
333-
334-
> cannot replace slice of data that was already replaced
335-
336-
This likely indicates a bug in either rustc or cargo itself,
337-
and we would appreciate a bug report! You're likely to see
338-
a number of compiler warnings after this message which cargo
339-
attempted to fix but failed. If you could open an issue at
340-
https://github.com/rust-lang/rust/issues
341-
quoting the full output of this command we'd be very appreciative!
342-
Note that you may be able to make some more progress in the near-term
343-
fixing code with the `--broken-code` flag
344-
330+
[ERROR] error applying suggestions
331+
--> src/lib.rs
332+
= cause: cannot replace slice of data that was already replaced
333+
[HELP] to report this as a bug, open an issue at https://github.com/rust-lang/rust/issues, quoting the full output of this command
334+
[HELP] to possibly apply more fixes, pass in the `--broken-code` flag
345335
[FIXED] src/lib.rs (4 fixes)
346336
rustc fix shim comment 5
347337
rustc fix shim comment 6
@@ -362,25 +352,12 @@ fn fix_verification_failed() {
362352
|_execs| {},
363353
str![[r#"
364354
[CHECKING] foo v0.0.1 ([ROOT]/foo)
365-
[WARNING] failed to automatically apply fixes suggested by rustc to crate `foo`
366-
367-
after fixes were automatically applied the compiler reported errors within these files:
368-
369-
* src/lib.rs
370-
371-
This likely indicates a bug in either rustc or cargo itself,
372-
and we would appreciate a bug report! You're likely to see
373-
a number of compiler warnings after this message which cargo
374-
attempted to fix but failed. If you could open an issue at
375-
https://github.com/rust-lang/rust/issues
376-
quoting the full output of this command we'd be very appreciative!
377-
Note that you may be able to make some more progress in the near-term
378-
fixing code with the `--broken-code` flag
379-
380-
The following errors were reported:
381-
rustc fix shim error count=2
382-
Original diagnostics will follow.
383-
355+
[ERROR] errors present after applying fixes to crate `foo`
356+
--> src/lib.rs
357+
= cause: rustc fix shim error count=2
358+
[HELP] to report this as a bug, open an issue at https://github.com/rust-lang/rust/issues, quoting the full output of this command
359+
[HELP] to possibly apply more fixes, pass in the `--broken-code` flag
360+
[NOTE] original diagnostics will follow:
384361
rustc fix shim comment 1
385362
[WARNING] `foo` (lib) generated 1 warning (run `cargo fix --lib -p foo` to apply 1 suggestion)
386363
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
@@ -402,25 +379,12 @@ fn fix_verification_failed_clippy() {
402379
},
403380
str![[r#"
404381
[CHECKING] foo v0.0.1 ([ROOT]/foo)
405-
[WARNING] failed to automatically apply fixes suggested by rustc to crate `foo`
406-
407-
after fixes were automatically applied the compiler reported errors within these files:
408-
409-
* src/lib.rs
410-
411-
This likely indicates a bug in either rustc or cargo itself,
412-
and we would appreciate a bug report! You're likely to see
413-
a number of compiler warnings after this message which cargo
414-
attempted to fix but failed. If you could open an issue at
415-
https://github.com/rust-lang/rust-clippy/issues
416-
quoting the full output of this command we'd be very appreciative!
417-
Note that you may be able to make some more progress in the near-term
418-
fixing code with the `--broken-code` flag
419-
420-
The following errors were reported:
421-
rustc fix shim error count=2
422-
Original diagnostics will follow.
423-
382+
[ERROR] errors present after applying fixes to crate `foo`
383+
--> src/lib.rs
384+
= cause: rustc fix shim error count=2
385+
[HELP] to report this as a bug, open an issue at https://github.com/rust-lang/rust-clippy/issues, quoting the full output of this command
386+
[HELP] to possibly apply more fixes, pass in the `--broken-code` flag
387+
[NOTE] original diagnostics will follow:
424388
rustc fix shim comment 1
425389
[WARNING] `foo` (lib) generated 1 warning (run `cargo clippy --fix --lib -p foo` to apply 1 suggestion)
426390
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
@@ -493,25 +457,12 @@ fn broken_code_one_suggestion() {
493457
},
494458
str![[r#"
495459
[CHECKING] foo v0.0.1 ([ROOT]/foo)
496-
[WARNING] failed to automatically apply fixes suggested by rustc to crate `foo`
497-
498-
after fixes were automatically applied the compiler reported errors within these files:
499-
500-
* src/lib.rs
501-
502-
This likely indicates a bug in either rustc or cargo itself,
503-
and we would appreciate a bug report! You're likely to see
504-
a number of compiler warnings after this message which cargo
505-
attempted to fix but failed. If you could open an issue at
506-
https://github.com/rust-lang/rust/issues
507-
quoting the full output of this command we'd be very appreciative!
508-
Note that you may be able to make some more progress in the near-term
509-
fixing code with the `--broken-code` flag
510-
511-
The following errors were reported:
512-
rustc fix shim error count=2
513-
Original diagnostics will follow.
514-
460+
[ERROR] errors present after applying fixes to crate `foo`
461+
--> src/lib.rs
462+
= cause: rustc fix shim error count=2
463+
[HELP] to report this as a bug, open an issue at https://github.com/rust-lang/rust/issues, quoting the full output of this command
464+
[HELP] to possibly apply more fixes, pass in the `--broken-code` flag
465+
[NOTE] original diagnostics will follow:
515466
rustc fix shim comment 1
516467
rustc fix shim error count=2
517468
[WARNING] `foo` (lib) generated 1 warning

0 commit comments

Comments
 (0)