Skip to content

Commit 81e0670

Browse files
authored
fix(fix): Migrate cargo script manifests across editions (#14864)
### What does this PR try to resolve? 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. ### How should we test and review this PR? ### Additional information While looking for where the tests go, I found a couple places with a hard coded latest-edition in test output and fixed it.
2 parents 92de1a2 + ed3947f commit 81e0670

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
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/edition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ fn unset_edition_with_unset_rust_version() {
135135

136136
p.cargo("check -v")
137137
.with_stderr_data(str![[r#"
138-
[WARNING] no edition set: defaulting to the 2015 edition while the latest is 2024
138+
[WARNING] no edition set: defaulting to the 2015 edition while the latest is [..]
139139
[CHECKING] foo v0.1.0 ([ROOT]/foo)
140140
[RUNNING] `rustc [..] --edition=2015 [..]`
141141
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

tests/testsuite/fix.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,6 +2446,65 @@ edition = "2021"
24462446
);
24472447
}
24482448

2449+
#[cargo_test]
2450+
fn migrate_removes_project_for_script() {
2451+
let p = project()
2452+
.file(
2453+
"foo.rs",
2454+
r#"
2455+
---
2456+
# Before package
2457+
[ package ] # After package header
2458+
# After package header line
2459+
name = "foo"
2460+
edition = "2021"
2461+
# After package table
2462+
2463+
# Before project
2464+
[ project ] # After project header
2465+
# After project header line
2466+
name = "foo"
2467+
edition = "2021"
2468+
# After project table
2469+
---
2470+
2471+
fn main() {
2472+
}
2473+
"#,
2474+
)
2475+
.build();
2476+
2477+
p.cargo("-Zscript fix --edition --allow-no-vcs --manifest-path foo.rs")
2478+
.masquerade_as_nightly_cargo(&["script"])
2479+
.with_stderr_data(str![[r#"
2480+
[MIGRATING] foo.rs from 2021 edition to 2024
2481+
[FIXED] foo.rs (1 fix)
2482+
[CHECKING] foo v0.0.0 ([ROOT]/foo)
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
2485+
2486+
"#]])
2487+
.run();
2488+
assert_e2e().eq(
2489+
p.read_file("foo.rs"),
2490+
str![[r#"
2491+
2492+
---
2493+
# Before package
2494+
[ package ] # After package header
2495+
# After package header line
2496+
name = "foo"
2497+
edition = "2021"
2498+
# After project table
2499+
---
2500+
2501+
fn main() {
2502+
}
2503+
2504+
"#]],
2505+
);
2506+
}
2507+
24492508
#[cargo_test]
24502509
fn migrate_rename_underscore_fields() {
24512510
let p = project()

tests/testsuite/publish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ fn duplicate_version() {
142142
[package]
143143
name = "foo"
144144
version = "0.0.1"
145+
edition = "2015"
145146
authors = []
146147
license = "MIT"
147148
description = "foo"
@@ -160,7 +161,6 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for
160161
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
161162
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
162163
[VERIFYING] foo v0.0.1 ([ROOT]/foo)
163-
[WARNING] no edition set: defaulting to the 2015 edition while the latest is 2024
164164
[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1)
165165
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
166166
[UPLOADING] foo v0.0.1 ([ROOT]/foo)

0 commit comments

Comments
 (0)