From f8468abc763ec3f931df42081d8a96680e765f0b Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 3 Dec 2024 15:06:53 -0600 Subject: [PATCH] fix(fix): Migrate workspace dependencies --- src/cargo/ops/fix.rs | 53 ++++++++++++++++++++++++++++++++++++++++++ tests/testsuite/fix.rs | 4 +++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/cargo/ops/fix.rs b/src/cargo/ops/fix.rs index 86bb66396bf..1530e77f48e 100644 --- a/src/cargo/ops/fix.rs +++ b/src/cargo/ops/fix.rs @@ -252,6 +252,59 @@ fn check_version_control(gctx: &GlobalContext, opts: &FixOptions) -> CargoResult } fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> { + // HACK: Duplicate workspace migration logic between virtual manifests and real manifests to + // reduce multiple Migrating messages being reported for the same file to the user + if matches!(ws.root_maybe(), MaybePackage::Virtual(_)) { + // Warning: workspaces do not have an edition so this should only include changes needed by + // packages that preserve the behavior of the workspace on all editions + let highest_edition = pkgs + .iter() + .map(|p| p.manifest().edition()) + .max() + .unwrap_or_default(); + let prepare_for_edition = highest_edition.saturating_next(); + if highest_edition == prepare_for_edition + || (!prepare_for_edition.is_stable() && !ws.gctx().nightly_features_allowed) + { + // + } else { + let mut manifest_mut = LocalManifest::try_new(ws.root_manifest())?; + let document = &mut manifest_mut.data; + let mut fixes = 0; + + if Edition::Edition2024 <= prepare_for_edition { + let root = document.as_table_mut(); + + if let Some(workspace) = root + .get_mut("workspace") + .and_then(|t| t.as_table_like_mut()) + { + // strictly speaking, the edition doesn't apply to this table but it should be safe + // enough + fixes += rename_dep_fields_2024(workspace, "dependencies"); + } + } + + if 0 < fixes { + // HACK: As workspace migration is a special case, only report it if something + // happened + let file = ws.root_manifest(); + let file = file.strip_prefix(ws.root()).unwrap_or(file); + let file = file.display(); + ws.gctx().shell().status( + "Migrating", + format!("{file} from {highest_edition} edition to {prepare_for_edition}"), + )?; + + let verb = if fixes == 1 { "fix" } else { "fixes" }; + let msg = format!("{file} ({fixes} {verb})"); + ws.gctx().shell().status("Fixed", msg)?; + + manifest_mut.write()?; + } + } + } + for pkg in pkgs { let existing_edition = pkg.manifest().edition(); let prepare_for_edition = existing_edition.saturating_next(); diff --git a/tests/testsuite/fix.rs b/tests/testsuite/fix.rs index 11349338c8b..f475fe5ba21 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -2682,6 +2682,8 @@ edition = "2021" p.cargo("fix --edition --allow-no-vcs") .with_stderr_data(str![[r#" +[MIGRATING] Cargo.toml from 2021 edition to 2024 +[FIXED] Cargo.toml (1 fix) [MIGRATING] foo/Cargo.toml from 2021 edition to 2024 [CHECKING] foo v0.0.0 ([ROOT]/foo/foo) [MIGRATING] foo/src/lib.rs from 2021 edition to 2024 @@ -2699,7 +2701,7 @@ resolver = "2" [workspace.dependencies] # Before default_features -a = {path = "a", default_features = false} # After default_features value +a = {path = "a", default-features = false} # After default_features value # After default_features line "#]],