Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fix): Migrate cargo script manifests across editions #14864

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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