Skip to content

Commit

Permalink
fix(fix): Migrate workspace dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 3, 2024
1 parent ccd193a commit 96c43e9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 duplicate messages being reported to the user
if matches!(ws.root_maybe(), MaybePackage::Virtual(_)) {
// Warning: workspace's 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();
Expand Down
4 changes: 3 additions & 1 deletion tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
"#]],
Expand Down

0 comments on commit 96c43e9

Please sign in to comment.