Skip to content

Commit

Permalink
fix(fix): Migrate cargo script manifests across editions
Browse files Browse the repository at this point in the history
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 rust-lang#12207.
  • Loading branch information
epage committed Nov 27, 2024
1 parent a609028 commit ed3947f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 45 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
52 changes: 11 additions & 41 deletions tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2479,57 +2479,27 @@ fn main() {
.with_stderr_data(str![[r#"
[MIGRATING] foo.rs from 2021 edition to 2024
[FIXED] foo.rs (1 fix)
[WARNING] `package.edition` is unspecified, defaulting to `2024`
[CHECKING] foo v0.0.0 ([ROOT]/foo)
[WARNING] `foo.rs` is already on the latest edition (2024), unable to migrate further
If you are trying to migrate from the previous edition (2021), the
process requires following these steps:
1. Start with `edition = "2021"` in `Cargo.toml`
2. Run `cargo fix --edition`
3. Modify `Cargo.toml` to set `edition = "2024"`
4. Run `cargo build` or `cargo test` to verify the fixes worked
More details may be found at
https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-project-to-a-new-edition.html
[ERROR] expected item, found `[`
--> foo.rs:1:1
|
1 | [[bin]]
| ^ expected item
|
= [NOTE] for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
[ERROR] could not compile `foo` (bin "foo" test) due to 1 previous error
[WARNING] build failed, waiting for other jobs to finish...
[ERROR] could not compile `foo` (bin "foo") due to 1 previous error
[MIGRATING] [ROOT]/home/.cargo/target/[HASH]/foo.rs from 2021 edition to 2024
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.with_status(101)
.run();
assert_e2e().eq(
p.read_file("foo.rs"),
str![[r#"
[[bin]]
name = "foo"
path = "[ROOT]/home/.cargo/target/[HASH]/foo.rs"
[package]
autobenches = false
autobins = false
autoexamples = false
autolib = false
autotests = false
build = false
edition = "2021"
---
# Before package
[ package ] # After package header
# After package header line
name = "foo"
edition = "2021"
# After project table
---
[profile.release]
strip = true
[workspace]
fn main() {
}
"#]],
);
Expand Down

0 comments on commit ed3947f

Please sign in to comment.