endorlabs / MIRAI

MIT License
41 stars 7 forks source link

Fix error when using rustversion with MIRAI #3

Open YichiZhang0613 opened 1 week ago

YichiZhang0613 commented 1 week ago

Issue

When using rustversion with MIRAI an error will occur

Steps to Reproduce

As mentioned in the documentation

Expected Behavior

No error.

Actual Results

Error: unexpected output from rustc --version': "mirai 1.1.9\n"

Environment

rustc 1.77.0-nightly (2023-12-29)

Solution

Replace following code in MIRAI/checker/src/cargo-mirai.rs

if std::env::args().any(|a| a == "--version" || a == "-V") {
        let version_info = rustc_tools_util::get_version_info!();
        println!("{version_info}");
        return;
 }

with

if std::env::args().any(|a| a == "--version" || a == "-V") {
       let output = std::process::Command::new("rustc")
            .arg("--version")
            .output()
            .expect("failed to execute rustc");

        let version_info = String::from_utf8_lossy(&output.stdout);
        // your personal logic to retrieve version information that matches the required format.
        // Get YYYY-MM-DD date pattern
        let date_pattern = Regex::new(r"\d{4}-\d{2}-\d{2}").unwrap();

        if let Some(date_match) = date_pattern.find(&version_info) {
            let mut prefix = "";
            if let Some(left_parenthesis_index) = version_info.find("("){
                prefix = &version_info[..left_parenthesis_index].trim_end();
            }
            let date = &version_info[date_match.start()..date_match.end()];
            let parsed_version = format!("{} ({})", prefix, date);
            println!("{}", parsed_version);
        } else {
            println!("{}", version_info.trim());
        }
        return;
 }

This is an example of a version that meets the format requirements: rustc 1.77.0-nightly (2023-12-29) You can add the required logic according to the version_info obtained to make it meet the format requirements.

hermanventer commented 16 hours ago

I am not up to speed on why cargo-mirai needs to implement "--version" this way. I'd like to understand it fully before incorporating the suggested change. Can you tell me a bit more about just how I would replicate "When using rustversion with MIRAI"? Also, can you tell me where I can find the code that produces the 'Error: unexpected output from rustc --version' message?

Thanks