From ca1af41b1222dd7c9ef4db1d6ec6a7a339debc35 Mon Sep 17 00:00:00 2001 From: Ding Xiang Fei Date: Fri, 23 Aug 2024 15:51:24 +0800 Subject: [PATCH] force edition 2024 lint into action on crater --- src/bin/cargo/commands/check.rs | 58 +++++++++++++++++++++++++++++---- src/cargo/util/toml/mod.rs | 18 +++++----- 2 files changed, 61 insertions(+), 15 deletions(-) diff --git a/src/bin/cargo/commands/check.rs b/src/bin/cargo/commands/check.rs index 66f378c3e90..698f9961a13 100644 --- a/src/bin/cargo/commands/check.rs +++ b/src/bin/cargo/commands/check.rs @@ -1,7 +1,7 @@ -use crate::command_prelude::*; - use cargo::ops; +use crate::command_prelude::*; + pub fn cli() -> Command { subcommand("check") // subcommand aliases are handled in aliased_command() @@ -44,12 +44,13 @@ pub fn cli() -> Command { } pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult { + if std::env::var("CARGO_REAL_CHECK").is_err() { + fixit()?; + return Ok(()); + } let ws = args.workspace(gctx)?; // This is a legacy behavior that causes `cargo check` to pass `--test`. - let test = matches!( - args.get_one::("profile").map(String::as_str), - Some("test") - ); + let test = matches!(args.get_one::("profile").map(String::as_str), Some("test")); let mode = CompileMode::Check { test }; let compile_opts = args.compile_options(gctx, mode, Some(&ws), ProfileChecking::LegacyTestOnly)?; @@ -57,3 +58,48 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult { ops::compile(&ws, &compile_opts)?; Ok(()) } + +fn fixit() -> CliResult { + use std::path::Path; + + use anyhow::Context; + use cargo_util::{paths, ProcessBuilder}; + + eprintln!("Copying to /tmp/fixit"); + ProcessBuilder::new("cp").args(&["-a", ".", "/tmp/fixit"]).exec()?; + std::env::set_current_dir("/tmp/fixit").map_err(|e| anyhow::format_err!("cd failed {}", e))?; + + let ed_re = regex::Regex::new(r#"(?m)^ *edition *= *['"]([^'"]+)['"]"#).unwrap(); + let manifest = paths::read(Path::new("Cargo.toml"))?; + let ed_cap = match ed_re.captures(&manifest) { + None => { + eprintln!("no edition found in manifest, probably 2015, skipping"); + return Ok(()); + } + Some(caps) => caps.get(1).unwrap(), + }; + if ed_cap.as_str() != "2021" { + eprintln!("skipping non-2021 edition `{}`", ed_cap.as_str()); + return Ok(()); + } + eprintln!("Running `cargo fix --edition`"); + // Skip "cargo check" + let args: Vec<_> = std::env::args().skip(2).collect(); + ProcessBuilder::new("cargo") + .args(&["fix", "--edition", "--allow-no-vcs", "--allow-dirty"]) + .args(&args) + .exec() + .with_context(|| "failed to migrate to next edition")?; + let mut manifest = paths::read(Path::new("Cargo.toml"))?; + let ed_cap = ed_re.captures(&manifest).unwrap().get(1).unwrap(); + manifest.replace_range(ed_cap.range(), "2024"); + paths::write("Cargo.toml", manifest)?; + eprintln!("Running `cargo check` to verify 2024"); + ProcessBuilder::new("cargo") + .args(&["check"]) + .args(&args) + .env("CARGO_REAL_CHECK", "1") + .exec() + .with_context(|| "failed to check after updating to 2024")?; + Ok(()) +} diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 6f74ff6e8b2..117666bfe6c 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1257,15 +1257,15 @@ pub fn to_real_manifest( // features.require(Feature::edition20xx())?; // } // ``` - if edition == Edition::Edition2024 { - features.require(Feature::edition2024())?; - } else if !edition.is_stable() { - // Guard in case someone forgets to add .require() - return Err(util::errors::internal(format!( - "edition {} should be gated", - edition - ))); - } + // if edition == Edition::Edition2024 { + // features.require(Feature::edition2024())?; + // } else if !edition.is_stable() { + // // Guard in case someone forgets to add .require() + // return Err(util::errors::internal(format!( + // "edition {} should be gated", + // edition + // ))); + // } if original_toml.project.is_some() { if Edition::Edition2024 <= edition {