Fix off-by-one error when future-incompat report is cached

This fixes a problem introduced by
https://github.com/rust-lang/cargo/pull/11648 where the future-incompat
report will tell you to run with an `--id` flag with the wrong value
if the report is already cached.

The solution is to add a method to determine which ID to use for the
suggestions *before* attempting to save the report.
This commit is contained in:
Eric Huss 2025-03-21 15:10:02 -07:00
parent afdfd9263e
commit 3fb97ecf28
2 changed files with 24 additions and 18 deletions

View File

@ -140,16 +140,10 @@ impl OnDiskReports {
mut self,
ws: &Workspace<'_>,
suggestion_message: String,
per_package_reports: &[FutureIncompatReportPackage],
per_package: BTreeMap<String, String>,
) -> u32 {
let per_package = render_report(per_package_reports);
if let Some(existing_report) = self
.reports
.iter()
.find(|existing| existing.per_package == per_package)
{
return existing_report.id;
if let Some(existing_id) = self.has_report(&per_package) {
return existing_id;
}
let report = OnDiskReport {
@ -189,6 +183,14 @@ impl OnDiskReports {
saved_id
}
/// Returns the ID of a report if it is already on disk.
fn has_report(&self, rendered_per_package: &BTreeMap<String, String>) -> Option<u32> {
self.reports
.iter()
.find(|existing| &existing.per_package == rendered_per_package)
.map(|report| report.id)
}
/// Loads the on-disk reports.
pub fn load(ws: &Workspace<'_>) -> CargoResult<OnDiskReports> {
let report_file = match ws.build_dir().open_ro_shared(
@ -408,7 +410,14 @@ pub fn save_and_display_report(
OnDiskReports::default()
}
};
let report_id = current_reports.next_id;
let rendered_report = render_report(per_package_future_incompat_reports);
// If the report is already on disk, then it will reuse the same ID,
// otherwise prepare for the next ID.
let report_id = current_reports
.has_report(&rendered_report)
.unwrap_or(current_reports.next_id);
// Get a list of unique and sorted package name/versions.
let package_ids: BTreeSet<_> = per_package_future_incompat_reports
@ -481,11 +490,8 @@ https://doc.rust-lang.org/cargo/reference/overriding-dependencies.html#the-patch
update_message = update_message,
);
let saved_report_id = current_reports.save_report(
bcx.ws,
suggestion_message.clone(),
per_package_future_incompat_reports,
);
let saved_report_id =
current_reports.save_report(bcx.ws, suggestion_message.clone(), rendered_report);
if bcx.build_config.future_incompat_report {
drop(bcx.gctx.shell().note(&suggestion_message));

View File

@ -102,7 +102,7 @@ fix to the maintainers (e.g. by creating a pull request):
- foo@0.0.0
- Repository: <not found>
- Detailed warning command: `cargo report future-incompatibilities --id 2 --package foo@0.0.0`
- Detailed warning command: `cargo report future-incompatibilities --id 1 --package foo@0.0.0`
- If waiting for an upstream fix is not an option, you can use the `[patch]`
section in `Cargo.toml` to use your own version of the dependency. For more
@ -192,7 +192,7 @@ fix to the maintainers (e.g. by creating a pull request):
- bar@1.0.0
- Repository: https://example.com/
- Detailed warning command: `cargo report future-incompatibilities --id 2 --package bar@1.0.0`
- Detailed warning command: `cargo report future-incompatibilities --id 1 --package bar@1.0.0`
- If waiting for an upstream fix is not an option, you can use the `[patch]`
section in `Cargo.toml` to use your own version of the dependency. For more
@ -708,7 +708,7 @@ fix to the maintainers (e.g. by creating a pull request):
- bar@1.0.0
- Repository: https://example.com/
- Detailed warning command: `cargo report future-incompatibilities --id 2 --package bar@1.0.0`
- Detailed warning command: `cargo report future-incompatibilities --id 1 --package bar@1.0.0`
- If waiting for an upstream fix is not an option, you can use the `[patch]`
section in `Cargo.toml` to use your own version of the dependency. For more