diff --git a/src/cargo/ops/fix.rs b/src/cargo/ops/fix.rs index dc4223f31b9..aaf731b4e85 100644 --- a/src/cargo/ops/fix.rs +++ b/src/cargo/ops/fix.rs @@ -247,11 +247,46 @@ fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> { "Migrating", format!("{file} from {existing_edition} edition to {prepare_for_edition}"), )?; + + if Edition::Edition2024 <= prepare_for_edition { + let mut document = pkg.manifest().document().clone().into_mut(); + let mut fixes = 0; + + let root = document.as_table_mut(); + if rename_table(root, "project", "package") { + fixes += 1; + } + + if 0 < fixes { + let verb = if fixes == 1 { "fix" } else { "fixes" }; + let msg = format!("{file} ({fixes} {verb})"); + ws.gctx().shell().status("Fixed", msg)?; + + let s = document.to_string(); + let new_contents_bytes = s.as_bytes(); + cargo_util::paths::write_atomic(pkg.manifest_path(), new_contents_bytes)?; + } + } } Ok(()) } +fn rename_table(parent: &mut dyn toml_edit::TableLike, old: &str, new: &str) -> bool { + let Some(old_key) = parent.key(old).cloned() else { + return false; + }; + + let project = parent.remove(old).expect("returned early"); + if !parent.contains_key(new) { + parent.insert(new, project); + let mut new_key = parent.key_mut(new).expect("just inserted"); + *new_key.dotted_decor_mut() = old_key.dotted_decor().clone(); + *new_key.leaf_decor_mut() = old_key.leaf_decor().clone(); + } + true +} + 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 916f1d1519c..485511ab764 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -1975,7 +1975,7 @@ edition = "2021" .with_stderr( "\ [MIGRATING] Cargo.toml from 2021 edition to 2024 -[WARNING] `[project]` is deprecated in favor of `[package]` +[FIXED] Cargo.toml (1 fix) [CHECKING] foo v0.0.0 ([CWD]) [MIGRATING] src/lib.rs from 2021 edition to 2024 [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..]s @@ -1988,7 +1988,7 @@ edition = "2021" cargo-features = ["edition2024"] # Before project -[ project ] # After project header +[ package ] # After project header # After project header line name = "foo" edition = "2021" @@ -2028,7 +2028,7 @@ edition = "2021" .with_stderr( "\ [MIGRATING] Cargo.toml from 2021 edition to 2024 -[WARNING] `[project]` is deprecated in favor of `[package]` +[FIXED] Cargo.toml (1 fix) [CHECKING] foo v0.0.0 ([CWD]) [MIGRATING] src/lib.rs from 2021 edition to 2024 [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..]s @@ -2045,13 +2045,6 @@ cargo-features = ["edition2024"] # After package header line name = "foo" edition = "2021" -# After package table - -# Before project -[ project ] # After project header -# After project header line -name = "foo" -edition = "2021" # After project table "# );