mirror of
https://github.com/rust-lang/cargo.git
synced 2025-09-25 11:14:46 +00:00
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.
This commit is contained in:
commit
81e0670ddd
@ -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()?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user