|
1 |
| -use crate::command_prelude::*; |
2 |
| - |
3 | 1 | use cargo::ops;
|
4 | 2 |
|
| 3 | +use crate::command_prelude::*; |
| 4 | + |
5 | 5 | pub fn cli() -> Command {
|
6 | 6 | subcommand("check")
|
7 | 7 | // subcommand aliases are handled in aliased_command()
|
@@ -44,16 +44,62 @@ pub fn cli() -> Command {
|
44 | 44 | }
|
45 | 45 |
|
46 | 46 | pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
|
| 47 | + if std::env::var("CARGO_REAL_CHECK").is_err() { |
| 48 | + fixit()?; |
| 49 | + return Ok(()); |
| 50 | + } |
47 | 51 | let ws = args.workspace(gctx)?;
|
48 | 52 | // This is a legacy behavior that causes `cargo check` to pass `--test`.
|
49 |
| - let test = matches!( |
50 |
| - args.get_one::<String>("profile").map(String::as_str), |
51 |
| - Some("test") |
52 |
| - ); |
| 53 | + let test = matches!(args.get_one::<String>("profile").map(String::as_str), Some("test")); |
53 | 54 | let mode = CompileMode::Check { test };
|
54 | 55 | let compile_opts =
|
55 | 56 | args.compile_options(gctx, mode, Some(&ws), ProfileChecking::LegacyTestOnly)?;
|
56 | 57 |
|
57 | 58 | ops::compile(&ws, &compile_opts)?;
|
58 | 59 | Ok(())
|
59 | 60 | }
|
| 61 | + |
| 62 | +fn fixit() -> CliResult { |
| 63 | + use std::path::Path; |
| 64 | + |
| 65 | + use anyhow::Context; |
| 66 | + use cargo_util::{paths, ProcessBuilder}; |
| 67 | + |
| 68 | + eprintln!("Copying to /tmp/fixit"); |
| 69 | + ProcessBuilder::new("cp").args(&["-a", ".", "/tmp/fixit"]).exec()?; |
| 70 | + std::env::set_current_dir("/tmp/fixit").map_err(|e| anyhow::format_err!("cd failed {}", e))?; |
| 71 | + |
| 72 | + let ed_re = regex::Regex::new(r#"(?m)^ *edition *= *['"]([^'"]+)['"]"#).unwrap(); |
| 73 | + let manifest = paths::read(Path::new("Cargo.toml"))?; |
| 74 | + let ed_cap = match ed_re.captures(&manifest) { |
| 75 | + None => { |
| 76 | + eprintln!("no edition found in manifest, probably 2015, skipping"); |
| 77 | + return Ok(()); |
| 78 | + } |
| 79 | + Some(caps) => caps.get(1).unwrap(), |
| 80 | + }; |
| 81 | + if ed_cap.as_str() != "2021" { |
| 82 | + eprintln!("skipping non-2021 edition `{}`", ed_cap.as_str()); |
| 83 | + return Ok(()); |
| 84 | + } |
| 85 | + eprintln!("Running `cargo fix --edition`"); |
| 86 | + // Skip "cargo check" |
| 87 | + let args: Vec<_> = std::env::args().skip(2).collect(); |
| 88 | + ProcessBuilder::new("cargo") |
| 89 | + .args(&["fix", "--edition", "--allow-no-vcs", "--allow-dirty"]) |
| 90 | + .args(&args) |
| 91 | + .exec() |
| 92 | + .with_context(|| "failed to migrate to next edition")?; |
| 93 | + let mut manifest = paths::read(Path::new("Cargo.toml"))?; |
| 94 | + let ed_cap = ed_re.captures(&manifest).unwrap().get(1).unwrap(); |
| 95 | + manifest.replace_range(ed_cap.range(), "2024"); |
| 96 | + paths::write("Cargo.toml", manifest)?; |
| 97 | + eprintln!("Running `cargo check` to verify 2024"); |
| 98 | + ProcessBuilder::new("cargo") |
| 99 | + .args(&["check"]) |
| 100 | + .args(&args) |
| 101 | + .env("CARGO_REAL_CHECK", "1") |
| 102 | + .exec() |
| 103 | + .with_context(|| "failed to check after updating to 2024")?; |
| 104 | + Ok(()) |
| 105 | +} |
0 commit comments