From ed3947f2eff75dde0983b93862a6d2e0df8f3efc Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 27 Nov 2024 13:24:37 -0600 Subject: [PATCH] fix(fix): Migrate cargo script manifests across editions At this point it is unlikely for a script to be written for pre-2024 edition and migrated to 2024+ but this code is independent of Editions so this means we have one less bug and are better prepared for the next edition. When we add `cargo fix` support for regular manifest warnings, we'll need to take into account cargo scripts. This is a part of #12207. --- src/cargo/ops/fix.rs | 8 +++---- tests/testsuite/fix.rs | 52 +++++++++--------------------------------- 2 files changed, 15 insertions(+), 45 deletions(-) diff --git a/src/cargo/ops/fix.rs b/src/cargo/ops/fix.rs index a5b94919240..6e4c718fcd4 100644 --- a/src/cargo/ops/fix.rs +++ b/src/cargo/ops/fix.rs @@ -61,6 +61,7 @@ use crate::ops::resolve::WorkspaceResolve; use crate::ops::{self, CompileOptions}; use crate::util::diagnostic_server::{Message, RustfixDiagnosticServer}; use crate::util::errors::CargoResult; +use crate::util::toml_mut::manifest::LocalManifest; use crate::util::GlobalContext; use crate::util::{existing_vcs_repo, LockServer, LockServerClient}; use crate::{drop_eprint, drop_eprintln}; @@ -272,7 +273,8 @@ fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> { MaybePackage::Virtual(manifest) => manifest.original_toml(), }; if Edition::Edition2024 <= prepare_for_edition { - let mut document = pkg.manifest().document().clone().into_mut(); + let mut manifest_mut = LocalManifest::try_new(pkg.manifest_path())?; + let document = &mut manifest_mut.data; let mut fixes = 0; let root = document.as_table_mut(); @@ -335,9 +337,7 @@ fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> { 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)?; + manifest_mut.write()?; } } } diff --git a/tests/testsuite/fix.rs b/tests/testsuite/fix.rs index 08c9c20e1e0..685f664803e 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -2479,57 +2479,27 @@ fn main() { .with_stderr_data(str![[r#" [MIGRATING] foo.rs from 2021 edition to 2024 [FIXED] foo.rs (1 fix) -[WARNING] `package.edition` is unspecified, defaulting to `2024` [CHECKING] foo v0.0.0 ([ROOT]/foo) -[WARNING] `foo.rs` is already on the latest edition (2024), unable to migrate further - -If you are trying to migrate from the previous edition (2021), the -process requires following these steps: - -1. Start with `edition = "2021"` in `Cargo.toml` -2. Run `cargo fix --edition` -3. Modify `Cargo.toml` to set `edition = "2024"` -4. Run `cargo build` or `cargo test` to verify the fixes worked - -More details may be found at -https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-project-to-a-new-edition.html - -[ERROR] expected item, found `[` - --> foo.rs:1:1 - | -1 | [[bin]] - | ^ expected item - | - = [NOTE] for a full list of items that can appear in modules, see - -[ERROR] could not compile `foo` (bin "foo" test) due to 1 previous error -[WARNING] build failed, waiting for other jobs to finish... -[ERROR] could not compile `foo` (bin "foo") due to 1 previous error +[MIGRATING] [ROOT]/home/.cargo/target/[HASH]/foo.rs from 2021 edition to 2024 +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s "#]]) - .with_status(101) .run(); assert_e2e().eq( p.read_file("foo.rs"), str![[r#" -[[bin]] -name = "foo" -path = "[ROOT]/home/.cargo/target/[HASH]/foo.rs" -[package] -autobenches = false -autobins = false -autoexamples = false -autolib = false -autotests = false -build = false -edition = "2021" +--- +# Before package +[ package ] # After package header +# After package header line name = "foo" +edition = "2021" +# After project table +--- -[profile.release] -strip = true - -[workspace] +fn main() { +} "#]], );