Skip to content

Commit

Permalink
force edition 2024 lint into action on crater
Browse files Browse the repository at this point in the history
  • Loading branch information
dingxiangfei2009 committed Sep 17, 2024
1 parent f416e1b commit 8c63870
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 153 deletions.
58 changes: 52 additions & 6 deletions src/bin/cargo/commands/check.rs
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -44,16 +44,62 @@ 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::<String>("profile").map(String::as_str),
Some("test")
);
let test = matches!(args.get_one::<String>("profile").map(String::as_str), Some("test"));
let mode = CompileMode::Check { test };
let compile_opts =
args.compile_options(gctx, mode, Some(&ws), ProfileChecking::LegacyTestOnly)?;

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(())
}
Loading

0 comments on commit 8c63870

Please sign in to comment.