Skip to content

Commit

Permalink
feat(fix): Report manifest migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Apr 12, 2024
1 parent 81072ab commit 058c2e0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
31 changes: 30 additions & 1 deletion src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ use tracing::{debug, trace, warn};
use crate::core::compiler::RustcTargetData;
use crate::core::resolver::features::{DiffMap, FeatureOpts, FeatureResolver, FeaturesFor};
use crate::core::resolver::{HasDevUnits, Resolve, ResolveBehavior};
use crate::core::{Edition, MaybePackage, PackageId, Workspace};
use crate::core::PackageIdSpecQuery as _;
use crate::core::{Edition, MaybePackage, Package, PackageId, Workspace};
use crate::ops::resolve::WorkspaceResolve;
use crate::ops::{self, CompileOptions};
use crate::util::diagnostic_server::{Message, RustfixDiagnosticServer};
Expand Down Expand Up @@ -96,6 +97,13 @@ pub fn fix(
check_version_control(gctx, opts)?;

if opts.edition {
let specs = opts.compile_opts.spec.to_package_id_specs(&original_ws)?;
let members: Vec<&Package> = original_ws
.members()
.filter(|m| specs.iter().any(|spec| spec.matches(m.package_id())))
.collect();
migrate_manifests(original_ws, &members)?;

check_resolver_change(&original_ws, opts)?;
}
let ws = Workspace::new(&root_manifest, gctx)?;
Expand Down Expand Up @@ -222,6 +230,27 @@ fn check_version_control(gctx: &GlobalContext, opts: &FixOptions) -> CargoResult
);
}

fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> {
for pkg in pkgs {
let existing_edition = pkg.manifest().edition();
let prepare_for_edition = existing_edition.saturating_next();
if existing_edition == prepare_for_edition
|| (!prepare_for_edition.is_stable() && !ws.gctx().nightly_features_allowed)
{
continue;
}
let file = pkg.manifest_path();
let file = file.strip_prefix(ws.root()).unwrap_or(file);
let file = file.display();
ws.gctx().shell().status(
"Migrating",
format!("{file} from {existing_edition} edition to {prepare_for_edition}"),
)?;
}

Ok(())
}

fn check_resolver_change(ws: &Workspace<'_>, opts: &FixOptions) -> CargoResult<()> {
let root = ws.root_maybe();
match root {
Expand Down
14 changes: 12 additions & 2 deletions tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ fn prepare_for_2018() {
.build();

let stderr = "\
[MIGRATING] Cargo.toml from 2015 edition to 2018
[CHECKING] foo v0.0.1 ([..])
[MIGRATING] src/lib.rs from 2015 edition to 2018
[FIXED] src/lib.rs (2 fixes)
Expand Down Expand Up @@ -211,6 +212,7 @@ fn local_paths() {
p.cargo("fix --edition --allow-no-vcs")
.with_stderr(
"\
[MIGRATING] Cargo.toml from 2015 edition to 2018
[CHECKING] foo v0.0.1 ([..])
[MIGRATING] src/lib.rs from 2015 edition to 2018
[FIXED] src/lib.rs (1 fix)
Expand Down Expand Up @@ -298,6 +300,7 @@ fn specify_rustflags() {
.env("RUSTFLAGS", "-C linker=cc")
.with_stderr(
"\
[MIGRATING] Cargo.toml from 2015 edition to 2018
[CHECKING] foo v0.0.1 ([..])
[MIGRATING] src/lib.rs from 2015 edition to 2018
[FIXED] src/lib.rs (1 fix)
Expand Down Expand Up @@ -770,6 +773,7 @@ https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-proje
.masquerade_as_nightly_cargo(&["always_nightly"])
.with_stderr(&format!(
"\
[MIGRATING] Cargo.toml from {latest_stable} edition to {next}
[CHECKING] foo [..]
[MIGRATING] src/lib.rs from {latest_stable} edition to {next}
[FINISHED] [..]
Expand Down Expand Up @@ -804,11 +808,11 @@ fn prepare_for_latest_stable() {
p.cargo("fix --edition --allow-no-vcs")
.with_stderr(&format!(
"\
[MIGRATING] Cargo.toml from {previous} edition to {latest_stable}
[CHECKING] foo [..]
[MIGRATING] src/lib.rs from {} edition to {}
[MIGRATING] src/lib.rs from {previous} edition to {latest_stable}
[FINISHED] [..]
",
previous, latest_stable
))
.run();
}
Expand Down Expand Up @@ -911,6 +915,7 @@ fn fix_overlapping() {
p.cargo("fix --allow-no-vcs --edition --lib")
.with_stderr(
"\
[MIGRATING] Cargo.toml from 2015 edition to 2018
[CHECKING] foo [..]
[MIGRATING] src/lib.rs from 2015 edition to 2018
[FIXED] src/lib.rs (2 fixes)
Expand Down Expand Up @@ -1202,6 +1207,7 @@ fn only_warn_for_relevant_crates() {
p.cargo("fix --allow-no-vcs --edition")
.with_stderr(
"\
[MIGRATING] Cargo.toml from 2015 edition to 2018
[LOCKING] 2 packages
[CHECKING] a v0.1.0 ([..])
[CHECKING] foo v0.1.0 ([..])
Expand Down Expand Up @@ -1398,6 +1404,7 @@ fn edition_v2_resolver_report() {

p.cargo("fix --edition --allow-no-vcs")
.with_stderr_unordered("\
[MIGRATING] Cargo.toml from 2018 edition to 2021
[UPDATING] [..]
[LOCKING] 4 packages
[DOWNLOADING] crates ...
Expand Down Expand Up @@ -1477,6 +1484,7 @@ fn fix_edition_2021() {
p.cargo("fix --edition --allow-no-vcs")
.with_stderr(
"\
[MIGRATING] Cargo.toml from 2018 edition to 2021
[CHECKING] foo v0.1.0 [..]
[MIGRATING] src/lib.rs from 2018 edition to 2021
[FIXED] src/lib.rs (1 fix)
Expand Down Expand Up @@ -1966,6 +1974,7 @@ edition = "2021"
.masquerade_as_nightly_cargo(&["edition2024"])
.with_stderr(
"\
[MIGRATING] Cargo.toml from 2021 edition to 2024
[WARNING] `[project]` is deprecated in favor of `[package]`
[CHECKING] foo v0.0.0 ([CWD])
[MIGRATING] src/lib.rs from 2021 edition to 2024
Expand Down Expand Up @@ -2018,6 +2027,7 @@ edition = "2021"
.masquerade_as_nightly_cargo(&["edition2024"])
.with_stderr(
"\
[MIGRATING] Cargo.toml from 2021 edition to 2024
[WARNING] `[project]` is deprecated in favor of `[package]`
[CHECKING] foo v0.0.0 ([CWD])
[MIGRATING] src/lib.rs from 2021 edition to 2024
Expand Down

0 comments on commit 058c2e0

Please sign in to comment.