Skip to content

Commit 98298d4

Browse files
committed
feat(fix): Report manifest migrations
1 parent d5606b2 commit 98298d4

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/cargo/ops/fix.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ use tracing::{debug, trace, warn};
5353
use crate::core::compiler::RustcTargetData;
5454
use crate::core::resolver::features::{DiffMap, FeatureOpts, FeatureResolver, FeaturesFor};
5555
use crate::core::resolver::{HasDevUnits, Resolve, ResolveBehavior};
56-
use crate::core::{Edition, MaybePackage, PackageId, Workspace};
56+
use crate::core::PackageIdSpecQuery as _;
57+
use crate::core::{Edition, MaybePackage, Package, PackageId, Workspace};
5758
use crate::ops::resolve::WorkspaceResolve;
5859
use crate::ops::{self, CompileOptions};
5960
use crate::util::diagnostic_server::{Message, RustfixDiagnosticServer};
@@ -96,6 +97,13 @@ pub fn fix(
9697
check_version_control(gctx, opts)?;
9798

9899
if opts.edition {
100+
let specs = opts.compile_opts.spec.to_package_id_specs(&original_ws)?;
101+
let members: Vec<&Package> = original_ws
102+
.members()
103+
.filter(|m| specs.iter().any(|spec| spec.matches(m.package_id())))
104+
.collect();
105+
migrate_manifests(original_ws, &members)?;
106+
99107
check_resolver_change(&original_ws, opts)?;
100108
}
101109
let mut ws = Workspace::new(&root_manifest, gctx)?;
@@ -223,6 +231,27 @@ fn check_version_control(gctx: &GlobalContext, opts: &FixOptions) -> CargoResult
223231
);
224232
}
225233

234+
fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> {
235+
for pkg in pkgs {
236+
let existing_edition = pkg.manifest().edition();
237+
let prepare_for_edition = existing_edition.saturating_next();
238+
if existing_edition == prepare_for_edition
239+
|| (!prepare_for_edition.is_stable() && !ws.gctx().nightly_features_allowed)
240+
{
241+
continue;
242+
}
243+
let file = pkg.manifest_path();
244+
let file = file.strip_prefix(ws.root()).unwrap_or(file);
245+
let file = file.display();
246+
ws.gctx().shell().status(
247+
"Migrating",
248+
format!("{file} from {existing_edition} edition to {prepare_for_edition}"),
249+
)?;
250+
}
251+
252+
Ok(())
253+
}
254+
226255
fn check_resolver_change(ws: &Workspace<'_>, opts: &FixOptions) -> CargoResult<()> {
227256
let root = ws.root_maybe();
228257
match root {

tests/testsuite/fix.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ fn prepare_for_2018() {
172172
.build();
173173

174174
let stderr = "\
175+
[MIGRATING] Cargo.toml from 2015 edition to 2018
175176
[CHECKING] foo v0.0.1 ([..])
176177
[MIGRATING] src/lib.rs from 2015 edition to 2018
177178
[FIXED] src/lib.rs (2 fixes)
@@ -211,6 +212,7 @@ fn local_paths() {
211212
p.cargo("fix --edition --allow-no-vcs")
212213
.with_stderr(
213214
"\
215+
[MIGRATING] Cargo.toml from 2015 edition to 2018
214216
[CHECKING] foo v0.0.1 ([..])
215217
[MIGRATING] src/lib.rs from 2015 edition to 2018
216218
[FIXED] src/lib.rs (1 fix)
@@ -298,6 +300,7 @@ fn specify_rustflags() {
298300
.env("RUSTFLAGS", "-C linker=cc")
299301
.with_stderr(
300302
"\
303+
[MIGRATING] Cargo.toml from 2015 edition to 2018
301304
[CHECKING] foo v0.0.1 ([..])
302305
[MIGRATING] src/lib.rs from 2015 edition to 2018
303306
[FIXED] src/lib.rs (1 fix)
@@ -770,6 +773,7 @@ https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-proje
770773
.masquerade_as_nightly_cargo(&["always_nightly"])
771774
.with_stderr(&format!(
772775
"\
776+
[MIGRATING] Cargo.toml from {latest_stable} edition to {next}
773777
[CHECKING] foo [..]
774778
[MIGRATING] src/lib.rs from {latest_stable} edition to {next}
775779
[FINISHED] [..]
@@ -804,11 +808,11 @@ fn prepare_for_latest_stable() {
804808
p.cargo("fix --edition --allow-no-vcs")
805809
.with_stderr(&format!(
806810
"\
811+
[MIGRATING] Cargo.toml from {previous} edition to {latest_stable}
807812
[CHECKING] foo [..]
808-
[MIGRATING] src/lib.rs from {} edition to {}
813+
[MIGRATING] src/lib.rs from {previous} edition to {latest_stable}
809814
[FINISHED] [..]
810815
",
811-
previous, latest_stable
812816
))
813817
.run();
814818
}
@@ -911,6 +915,7 @@ fn fix_overlapping() {
911915
p.cargo("fix --allow-no-vcs --edition --lib")
912916
.with_stderr(
913917
"\
918+
[MIGRATING] Cargo.toml from 2015 edition to 2018
914919
[CHECKING] foo [..]
915920
[MIGRATING] src/lib.rs from 2015 edition to 2018
916921
[FIXED] src/lib.rs (2 fixes)
@@ -1202,6 +1207,7 @@ fn only_warn_for_relevant_crates() {
12021207
p.cargo("fix --allow-no-vcs --edition")
12031208
.with_stderr(
12041209
"\
1210+
[MIGRATING] Cargo.toml from 2015 edition to 2018
12051211
[LOCKING] 2 packages to latest compatible versions
12061212
[CHECKING] a v0.1.0 ([..])
12071213
[CHECKING] foo v0.1.0 ([..])
@@ -1398,6 +1404,7 @@ fn edition_v2_resolver_report() {
13981404

13991405
p.cargo("fix --edition --allow-no-vcs")
14001406
.with_stderr_unordered("\
1407+
[MIGRATING] Cargo.toml from 2018 edition to 2021
14011408
[UPDATING] [..]
14021409
[LOCKING] 4 packages to latest compatible versions
14031410
[DOWNLOADING] crates ...
@@ -1477,6 +1484,7 @@ fn fix_edition_2021() {
14771484
p.cargo("fix --edition --allow-no-vcs")
14781485
.with_stderr(
14791486
"\
1487+
[MIGRATING] Cargo.toml from 2018 edition to 2021
14801488
[CHECKING] foo v0.1.0 [..]
14811489
[MIGRATING] src/lib.rs from 2018 edition to 2021
14821490
[FIXED] src/lib.rs (1 fix)
@@ -1966,6 +1974,7 @@ edition = "2021"
19661974
.masquerade_as_nightly_cargo(&["edition2024"])
19671975
.with_stderr(
19681976
"\
1977+
[MIGRATING] Cargo.toml from 2021 edition to 2024
19691978
[WARNING] `[project]` is deprecated in favor of `[package]`
19701979
[CHECKING] foo v0.0.0 ([CWD])
19711980
[MIGRATING] src/lib.rs from 2021 edition to 2024
@@ -2018,6 +2027,7 @@ edition = "2021"
20182027
.masquerade_as_nightly_cargo(&["edition2024"])
20192028
.with_stderr(
20202029
"\
2030+
[MIGRATING] Cargo.toml from 2021 edition to 2024
20212031
[WARNING] `[project]` is deprecated in favor of `[package]`
20222032
[CHECKING] foo v0.0.0 ([CWD])
20232033
[MIGRATING] src/lib.rs from 2021 edition to 2024

0 commit comments

Comments
 (0)