Skip to content

Commit 701860b

Browse files
committed
refactor(future): Separate out suggestions from rendering
This will make it easier later to put each in their own message/title in annotate snippets. The hack will be removed later in this series.
1 parent 52e3690 commit 701860b

File tree

1 file changed

+49
-25
lines changed

1 file changed

+49
-25
lines changed

src/cargo/core/compiler/future_incompat.rs

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,10 @@ fn get_updates(ws: &Workspace<'_>, package_ids: &BTreeSet<PackageId>) -> Option<
359359

360360
if !updated_versions.is_empty() {
361361
let updated_versions = itertools::join(updated_versions, ", ");
362-
writeln!(
362+
write!(
363363
updates,
364-
"{} has the following newer versions available: {}",
364+
"
365+
{} has the following newer versions available: {}",
365366
pkg_id, updated_versions
366367
)
367368
.unwrap();
@@ -432,11 +433,10 @@ pub fn save_and_display_report(
432433

433434
let update_message = if !updated_versions.is_empty() {
434435
format!(
435-
"
436-
- Some affected dependencies have newer versions available.
436+
"\
437+
Some affected dependencies have newer versions available.
437438
You may want to consider updating them to a newer version to see if the issue has been fixed.
438-
439-
{updated_versions}\n",
439+
{updated_versions}",
440440
updated_versions = updated_versions
441441
)
442442
} else {
@@ -448,8 +448,7 @@ You may want to consider updating them to a newer version to see if the issue ha
448448
.map(|package_id| {
449449
let manifest = bcx.packages.get_one(*package_id).unwrap().manifest();
450450
format!(
451-
"
452-
- {package_spec}
451+
" - {package_spec}
453452
- Repository: {url}
454453
- Detailed warning command: `cargo report future-incompatibilities --id {id} --package {package_spec}`",
455454
package_spec = format!("{}@{}", package_id.name(), package_id.version()),
@@ -462,36 +461,61 @@ You may want to consider updating them to a newer version to see if the issue ha
462461
)
463462
})
464463
.collect::<Vec<_>>()
465-
.join("\n");
464+
.join("\n\n");
466465

467466
let all_is_local = per_package_future_incompat_reports
468467
.iter()
469468
.all(|report| report.is_local);
470469

471-
let suggestion_message = if all_is_local {
472-
String::new()
473-
} else {
474-
format!(
475-
"
476-
To solve this problem, you can try the following approaches:
477-
478-
{update_message}\
479-
- If the issue is not solved by updating the dependencies, a fix has to be
470+
let suggestion_header = "To solve this problem, you can try the following approaches:";
471+
let mut suggestions = Vec::new();
472+
if !all_is_local {
473+
if !update_message.is_empty() {
474+
suggestions.push(update_message);
475+
}
476+
suggestions.push(format!(
477+
"\
478+
If the issue is not solved by updating the dependencies, a fix has to be
480479
implemented by those dependencies. You can help with that by notifying the
481480
maintainers of this problem (e.g. by creating a bug report) or by proposing a
482481
fix to the maintainers (e.g. by creating a pull request):
483-
{upstream_info}
484482
485-
- If waiting for an upstream fix is not an option, you can use the `[patch]`
483+
{upstream_info}"
484+
));
485+
suggestions.push(
486+
"\
487+
If waiting for an upstream fix is not an option, you can use the `[patch]`
486488
section in `Cargo.toml` to use your own version of the dependency. For more
487489
information, see:
488-
https://doc.rust-lang.org/cargo/reference/overriding-dependencies.html#the-patch-section
489-
",
490-
upstream_info = upstream_info,
491-
update_message = update_message,
490+
https://doc.rust-lang.org/cargo/reference/overriding-dependencies.html#the-patch-section"
491+
.to_owned(),
492+
);
493+
}
494+
495+
let suggestion_message = if suggestions.is_empty() {
496+
String::new()
497+
} else {
498+
let mut suggestion_message = String::new();
499+
writeln!(
500+
&mut suggestion_message,
501+
"
502+
{suggestion_header}"
492503
)
504+
.unwrap();
505+
if suggestions.len() == 3 {
506+
// HACK: there is an inconsistent leading line in this case
507+
writeln!(&mut suggestion_message).unwrap();
508+
}
509+
for suggestion in &suggestions {
510+
writeln!(
511+
&mut suggestion_message,
512+
"
513+
- {suggestion}"
514+
)
515+
.unwrap();
516+
}
517+
suggestion_message
493518
};
494-
495519
let saved_report_id =
496520
current_reports.save_report(bcx.ws, suggestion_message.clone(), rendered_report);
497521

0 commit comments

Comments
 (0)