Skip to content

Commit ed3947f

Browse files
committed
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.
1 parent a609028 commit ed3947f

File tree

2 files changed

+15
-45
lines changed

2 files changed

+15
-45
lines changed

src/cargo/ops/fix.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ use crate::ops::resolve::WorkspaceResolve;
6161
use crate::ops::{self, CompileOptions};
6262
use crate::util::diagnostic_server::{Message, RustfixDiagnosticServer};
6363
use crate::util::errors::CargoResult;
64+
use crate::util::toml_mut::manifest::LocalManifest;
6465
use crate::util::GlobalContext;
6566
use crate::util::{existing_vcs_repo, LockServer, LockServerClient};
6667
use crate::{drop_eprint, drop_eprintln};
@@ -272,7 +273,8 @@ fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> {
272273
MaybePackage::Virtual(manifest) => manifest.original_toml(),
273274
};
274275
if Edition::Edition2024 <= prepare_for_edition {
275-
let mut document = pkg.manifest().document().clone().into_mut();
276+
let mut manifest_mut = LocalManifest::try_new(pkg.manifest_path())?;
277+
let document = &mut manifest_mut.data;
276278
let mut fixes = 0;
277279

278280
let root = document.as_table_mut();
@@ -335,9 +337,7 @@ fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> {
335337
let msg = format!("{file} ({fixes} {verb})");
336338
ws.gctx().shell().status("Fixed", msg)?;
337339

338-
let s = document.to_string();
339-
let new_contents_bytes = s.as_bytes();
340-
cargo_util::paths::write_atomic(pkg.manifest_path(), new_contents_bytes)?;
340+
manifest_mut.write()?;
341341
}
342342
}
343343
}

tests/testsuite/fix.rs

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,57 +2479,27 @@ fn main() {
24792479
.with_stderr_data(str![[r#"
24802480
[MIGRATING] foo.rs from 2021 edition to 2024
24812481
[FIXED] foo.rs (1 fix)
2482-
[WARNING] `package.edition` is unspecified, defaulting to `2024`
24832482
[CHECKING] foo v0.0.0 ([ROOT]/foo)
2484-
[WARNING] `foo.rs` is already on the latest edition (2024), unable to migrate further
2485-
2486-
If you are trying to migrate from the previous edition (2021), the
2487-
process requires following these steps:
2488-
2489-
1. Start with `edition = "2021"` in `Cargo.toml`
2490-
2. Run `cargo fix --edition`
2491-
3. Modify `Cargo.toml` to set `edition = "2024"`
2492-
4. Run `cargo build` or `cargo test` to verify the fixes worked
2493-
2494-
More details may be found at
2495-
https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-project-to-a-new-edition.html
2496-
2497-
[ERROR] expected item, found `[`
2498-
--> foo.rs:1:1
2499-
|
2500-
1 | [[bin]]
2501-
| ^ expected item
2502-
|
2503-
= [NOTE] for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
2504-
2505-
[ERROR] could not compile `foo` (bin "foo" test) due to 1 previous error
2506-
[WARNING] build failed, waiting for other jobs to finish...
2507-
[ERROR] could not compile `foo` (bin "foo") due to 1 previous error
2483+
[MIGRATING] [ROOT]/home/.cargo/target/[HASH]/foo.rs from 2021 edition to 2024
2484+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
25082485
25092486
"#]])
2510-
.with_status(101)
25112487
.run();
25122488
assert_e2e().eq(
25132489
p.read_file("foo.rs"),
25142490
str![[r#"
2515-
[[bin]]
2516-
name = "foo"
2517-
path = "[ROOT]/home/.cargo/target/[HASH]/foo.rs"
25182491
2519-
[package]
2520-
autobenches = false
2521-
autobins = false
2522-
autoexamples = false
2523-
autolib = false
2524-
autotests = false
2525-
build = false
2526-
edition = "2021"
2492+
---
2493+
# Before package
2494+
[ package ] # After package header
2495+
# After package header line
25272496
name = "foo"
2497+
edition = "2021"
2498+
# After project table
2499+
---
25282500
2529-
[profile.release]
2530-
strip = true
2531-
2532-
[workspace]
2501+
fn main() {
2502+
}
25332503
25342504
"#]],
25352505
);

0 commit comments

Comments
 (0)