Skip to content

Commit

Permalink
feat(fix): Migrate from project to package on Edition 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Apr 15, 2024
1 parent 98298d4 commit cbd9def
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
35 changes: 35 additions & 0 deletions src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 3 additions & 10 deletions tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
"#
);
Expand Down

0 comments on commit cbd9def

Please sign in to comment.