Skip to content

Commit f8468ab

Browse files
committed
fix(fix): Migrate workspace dependencies
1 parent ccd193a commit f8468ab

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/cargo/ops/fix.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,59 @@ fn check_version_control(gctx: &GlobalContext, opts: &FixOptions) -> CargoResult
252252
}
253253

254254
fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> {
255+
// HACK: Duplicate workspace migration logic between virtual manifests and real manifests to
256+
// reduce multiple Migrating messages being reported for the same file to the user
257+
if matches!(ws.root_maybe(), MaybePackage::Virtual(_)) {
258+
// Warning: workspaces do not have an edition so this should only include changes needed by
259+
// packages that preserve the behavior of the workspace on all editions
260+
let highest_edition = pkgs
261+
.iter()
262+
.map(|p| p.manifest().edition())
263+
.max()
264+
.unwrap_or_default();
265+
let prepare_for_edition = highest_edition.saturating_next();
266+
if highest_edition == prepare_for_edition
267+
|| (!prepare_for_edition.is_stable() && !ws.gctx().nightly_features_allowed)
268+
{
269+
//
270+
} else {
271+
let mut manifest_mut = LocalManifest::try_new(ws.root_manifest())?;
272+
let document = &mut manifest_mut.data;
273+
let mut fixes = 0;
274+
275+
if Edition::Edition2024 <= prepare_for_edition {
276+
let root = document.as_table_mut();
277+
278+
if let Some(workspace) = root
279+
.get_mut("workspace")
280+
.and_then(|t| t.as_table_like_mut())
281+
{
282+
// strictly speaking, the edition doesn't apply to this table but it should be safe
283+
// enough
284+
fixes += rename_dep_fields_2024(workspace, "dependencies");
285+
}
286+
}
287+
288+
if 0 < fixes {
289+
// HACK: As workspace migration is a special case, only report it if something
290+
// happened
291+
let file = ws.root_manifest();
292+
let file = file.strip_prefix(ws.root()).unwrap_or(file);
293+
let file = file.display();
294+
ws.gctx().shell().status(
295+
"Migrating",
296+
format!("{file} from {highest_edition} edition to {prepare_for_edition}"),
297+
)?;
298+
299+
let verb = if fixes == 1 { "fix" } else { "fixes" };
300+
let msg = format!("{file} ({fixes} {verb})");
301+
ws.gctx().shell().status("Fixed", msg)?;
302+
303+
manifest_mut.write()?;
304+
}
305+
}
306+
}
307+
255308
for pkg in pkgs {
256309
let existing_edition = pkg.manifest().edition();
257310
let prepare_for_edition = existing_edition.saturating_next();

tests/testsuite/fix.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2682,6 +2682,8 @@ edition = "2021"
26822682

26832683
p.cargo("fix --edition --allow-no-vcs")
26842684
.with_stderr_data(str![[r#"
2685+
[MIGRATING] Cargo.toml from 2021 edition to 2024
2686+
[FIXED] Cargo.toml (1 fix)
26852687
[MIGRATING] foo/Cargo.toml from 2021 edition to 2024
26862688
[CHECKING] foo v0.0.0 ([ROOT]/foo/foo)
26872689
[MIGRATING] foo/src/lib.rs from 2021 edition to 2024
@@ -2699,7 +2701,7 @@ resolver = "2"
26992701
27002702
[workspace.dependencies]
27012703
# Before default_features
2702-
a = {path = "a", default_features = false} # After default_features value
2704+
a = {path = "a", default-features = false} # After default_features value
27032705
# After default_features line
27042706
27052707
"#]],

0 commit comments

Comments
 (0)