feat(fix): Migrate from project to package on Edition 2024

This commit is contained in:
Ed Page 2024-04-11 21:35:06 -05:00
parent 98298d4966
commit cbd9def9dc
2 changed files with 38 additions and 10 deletions

View File

@ -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 {

View File

@ -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
"#
);