Skip to content

Commit

Permalink
fix(fix): Migrate cargo script manifests across editions (#14864)
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
ehuss authored Nov 27, 2024
2 parents 92de1a2 + ed3947f commit 81e0670
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()?;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/edition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 59 additions & 0 deletions tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2446,6 +2446,65 @@ 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)
[CHECKING] foo v0.0.0 ([ROOT]/foo)
[MIGRATING] [ROOT]/home/.cargo/target/[HASH]/foo.rs from 2021 edition to 2024
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
assert_e2e().eq(
p.read_file("foo.rs"),
str![[r#"
---
# Before package
[ package ] # After package header
# After package header line
name = "foo"
edition = "2021"
# After project table
---
fn main() {
}
"#]],
);
}

#[cargo_test]
fn migrate_rename_underscore_fields() {
let p = project()
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ fn duplicate_version() {
[package]
name = "foo"
version = "0.0.1"
edition = "2015"
authors = []
license = "MIT"
description = "foo"
Expand All @@ -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)
Expand Down

0 comments on commit 81e0670

Please sign in to comment.