From 563b1a27b50ebc7b07f879c356f1655faefaf45e Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 27 Nov 2024 13:10:32 -0600 Subject: [PATCH 1/3] test: Harden against 2027(?) edition --- tests/testsuite/edition.rs | 2 +- tests/testsuite/publish.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testsuite/edition.rs b/tests/testsuite/edition.rs index 7b22c2dbda8..2c6ea53b08b 100644 --- a/tests/testsuite/edition.rs +++ b/tests/testsuite/edition.rs @@ -135,7 +135,7 @@ fn unset_edition_with_unset_rust_version() { p.cargo("check -v") .with_stderr_data(str![[r#" -[WARNING] no edition set: defaulting to the 2015 edition while the latest is 2024 +[WARNING] no edition set: defaulting to the 2015 edition while the latest is [..] [CHECKING] foo v0.1.0 ([ROOT]/foo) [RUNNING] `rustc [..] --edition=2015 [..]` [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index 961af6ad088..51ab6cfdaf7 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -142,6 +142,7 @@ fn duplicate_version() { [package] name = "foo" version = "0.0.1" + edition = "2015" authors = [] license = "MIT" description = "foo" @@ -160,7 +161,6 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for [PACKAGING] foo v0.0.1 ([ROOT]/foo) [PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed) [VERIFYING] foo v0.0.1 ([ROOT]/foo) -[WARNING] no edition set: defaulting to the 2015 edition while the latest is 2024 [COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [UPLOADING] foo v0.0.1 ([ROOT]/foo) From a609028eae9ed11d1aef90a548b8369486891674 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 27 Nov 2024 13:18:34 -0600 Subject: [PATCH 2/3] test(fix): Show cargo-script behavior --- tests/testsuite/fix.rs | 89 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/tests/testsuite/fix.rs b/tests/testsuite/fix.rs index bd47056c0dc..08c9c20e1e0 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -2446,6 +2446,95 @@ edition = "2021" ); } +#[cargo_test] +fn migrate_removes_project_for_script() { + let p = project() + .file( + "foo.rs", + r#" +--- +# Before package +[ package ] # After package header +# After package header line +name = "foo" +edition = "2021" +# After package table + +# Before project +[ project ] # After project header +# After project header line +name = "foo" +edition = "2021" +# After project table +--- + +fn main() { +} +"#, + ) + .build(); + + p.cargo("-Zscript fix --edition --allow-no-vcs --manifest-path foo.rs") + .masquerade_as_nightly_cargo(&["script"]) + .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 + +"#]]) + .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" +name = "foo" + +[profile.release] +strip = true + +[workspace] + +"#]], + ); +} + #[cargo_test] fn migrate_rename_underscore_fields() { let p = project() From ed3947f2eff75dde0983b93862a6d2e0df8f3efc Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 27 Nov 2024 13:24:37 -0600 Subject: [PATCH 3/3] 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() { +} "#]], );