From 058c2e00fca4d9aea2f989bcdd6998d22109def7 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 12 Apr 2024 09:37:26 -0500 Subject: [PATCH] feat(fix): Report manifest migrations --- src/cargo/ops/fix.rs | 31 ++++++++++++++++++++++++++++++- tests/testsuite/fix.rs | 14 ++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/cargo/ops/fix.rs b/src/cargo/ops/fix.rs index f0e8d4ff1209..ffd46b4e8db1 100644 --- a/src/cargo/ops/fix.rs +++ b/src/cargo/ops/fix.rs @@ -53,7 +53,8 @@ use tracing::{debug, trace, warn}; use crate::core::compiler::RustcTargetData; use crate::core::resolver::features::{DiffMap, FeatureOpts, FeatureResolver, FeaturesFor}; use crate::core::resolver::{HasDevUnits, Resolve, ResolveBehavior}; -use crate::core::{Edition, MaybePackage, PackageId, Workspace}; +use crate::core::PackageIdSpecQuery as _; +use crate::core::{Edition, MaybePackage, Package, PackageId, Workspace}; use crate::ops::resolve::WorkspaceResolve; use crate::ops::{self, CompileOptions}; use crate::util::diagnostic_server::{Message, RustfixDiagnosticServer}; @@ -96,6 +97,13 @@ pub fn fix( check_version_control(gctx, opts)?; if opts.edition { + let specs = opts.compile_opts.spec.to_package_id_specs(&original_ws)?; + let members: Vec<&Package> = original_ws + .members() + .filter(|m| specs.iter().any(|spec| spec.matches(m.package_id()))) + .collect(); + migrate_manifests(original_ws, &members)?; + check_resolver_change(&original_ws, opts)?; } let ws = Workspace::new(&root_manifest, gctx)?; @@ -222,6 +230,27 @@ fn check_version_control(gctx: &GlobalContext, opts: &FixOptions) -> CargoResult ); } +fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> { + for pkg in pkgs { + let existing_edition = pkg.manifest().edition(); + let prepare_for_edition = existing_edition.saturating_next(); + if existing_edition == prepare_for_edition + || (!prepare_for_edition.is_stable() && !ws.gctx().nightly_features_allowed) + { + continue; + } + let file = pkg.manifest_path(); + let file = file.strip_prefix(ws.root()).unwrap_or(file); + let file = file.display(); + ws.gctx().shell().status( + "Migrating", + format!("{file} from {existing_edition} edition to {prepare_for_edition}"), + )?; + } + + Ok(()) +} + fn check_resolver_change(ws: &Workspace<'_>, opts: &FixOptions) -> CargoResult<()> { let root = ws.root_maybe(); match root { diff --git a/tests/testsuite/fix.rs b/tests/testsuite/fix.rs index a9d0cf0904a2..ac99a56c7f47 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -172,6 +172,7 @@ fn prepare_for_2018() { .build(); let stderr = "\ +[MIGRATING] Cargo.toml from 2015 edition to 2018 [CHECKING] foo v0.0.1 ([..]) [MIGRATING] src/lib.rs from 2015 edition to 2018 [FIXED] src/lib.rs (2 fixes) @@ -211,6 +212,7 @@ fn local_paths() { p.cargo("fix --edition --allow-no-vcs") .with_stderr( "\ +[MIGRATING] Cargo.toml from 2015 edition to 2018 [CHECKING] foo v0.0.1 ([..]) [MIGRATING] src/lib.rs from 2015 edition to 2018 [FIXED] src/lib.rs (1 fix) @@ -298,6 +300,7 @@ fn specify_rustflags() { .env("RUSTFLAGS", "-C linker=cc") .with_stderr( "\ +[MIGRATING] Cargo.toml from 2015 edition to 2018 [CHECKING] foo v0.0.1 ([..]) [MIGRATING] src/lib.rs from 2015 edition to 2018 [FIXED] src/lib.rs (1 fix) @@ -770,6 +773,7 @@ https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-proje .masquerade_as_nightly_cargo(&["always_nightly"]) .with_stderr(&format!( "\ +[MIGRATING] Cargo.toml from {latest_stable} edition to {next} [CHECKING] foo [..] [MIGRATING] src/lib.rs from {latest_stable} edition to {next} [FINISHED] [..] @@ -804,11 +808,11 @@ fn prepare_for_latest_stable() { p.cargo("fix --edition --allow-no-vcs") .with_stderr(&format!( "\ +[MIGRATING] Cargo.toml from {previous} edition to {latest_stable} [CHECKING] foo [..] -[MIGRATING] src/lib.rs from {} edition to {} +[MIGRATING] src/lib.rs from {previous} edition to {latest_stable} [FINISHED] [..] ", - previous, latest_stable )) .run(); } @@ -911,6 +915,7 @@ fn fix_overlapping() { p.cargo("fix --allow-no-vcs --edition --lib") .with_stderr( "\ +[MIGRATING] Cargo.toml from 2015 edition to 2018 [CHECKING] foo [..] [MIGRATING] src/lib.rs from 2015 edition to 2018 [FIXED] src/lib.rs (2 fixes) @@ -1202,6 +1207,7 @@ fn only_warn_for_relevant_crates() { p.cargo("fix --allow-no-vcs --edition") .with_stderr( "\ +[MIGRATING] Cargo.toml from 2015 edition to 2018 [LOCKING] 2 packages [CHECKING] a v0.1.0 ([..]) [CHECKING] foo v0.1.0 ([..]) @@ -1398,6 +1404,7 @@ fn edition_v2_resolver_report() { p.cargo("fix --edition --allow-no-vcs") .with_stderr_unordered("\ +[MIGRATING] Cargo.toml from 2018 edition to 2021 [UPDATING] [..] [LOCKING] 4 packages [DOWNLOADING] crates ... @@ -1477,6 +1484,7 @@ fn fix_edition_2021() { p.cargo("fix --edition --allow-no-vcs") .with_stderr( "\ +[MIGRATING] Cargo.toml from 2018 edition to 2021 [CHECKING] foo v0.1.0 [..] [MIGRATING] src/lib.rs from 2018 edition to 2021 [FIXED] src/lib.rs (1 fix) @@ -1966,6 +1974,7 @@ edition = "2021" .masquerade_as_nightly_cargo(&["edition2024"]) .with_stderr( "\ +[MIGRATING] Cargo.toml from 2021 edition to 2024 [WARNING] `[project]` is deprecated in favor of `[package]` [CHECKING] foo v0.0.0 ([CWD]) [MIGRATING] src/lib.rs from 2021 edition to 2024 @@ -2018,6 +2027,7 @@ edition = "2021" .masquerade_as_nightly_cargo(&["edition2024"]) .with_stderr( "\ +[MIGRATING] Cargo.toml from 2021 edition to 2024 [WARNING] `[project]` is deprecated in favor of `[package]` [CHECKING] foo v0.0.0 ([CWD]) [MIGRATING] src/lib.rs from 2021 edition to 2024