Repository: google/comprehensive-rust
File: xtask/src/main.rs (Rust)

Code before:
    let install_args = vec![
        // The --locked flag is important for reproducible builds. It also
        // avoids breakage due to skews between mdbook and mdbook-svgbob.
        vec!["mdbook", "--locked", "--version", "0.4.52"],
        vec!["mdbook-svgbob", "--locked", "--version", "0.2.2"],
        vec!["mdbook-pandoc", "--locked", "--version", "0.10.4"],
        vec!["mdbook-i18n-helpers", "--locked", "--version", "0.3.6"],
        vec!["i18n-report", "--locked", "--version", "0.2.0"],
        vec!["mdbook-linkcheck2", "--locked", "--version", "0.9.1"],
        // Mdbook-exerciser and mdbook-course are located in this repository.
        // To make it possible to install them from any directory we need to
        // specify their path from the workspace root.
        vec!["--path", path_to_mdbook_exerciser.to_str().unwrap(), "--locked"],
        vec!["--path", path_to_mdbook_course.to_str().unwrap(), "--locked"],
    ];

    for args in &install_args {
        let status = Command::new(env!("CARGO"))
            .arg("install")
            .args(args)
            .status()
            .expect("Failed to execute cargo install");

        if !status.success() {
            let error_message = format!(
                "Command 'cargo install {}' exited with status code: {}",
                args.join(" "),
                status.code().unwrap()
            );
            return Err(anyhow!(error_message));
        }
    }

    // Uninstall original linkcheck if currently installed (see issue no 2773)
    uninstall_mdbook_linkcheck();

    Ok(())
}

fn uninstall_mdbook_linkcheck() {
    println!("Uninstalling old mdbook-linkcheck if installed...");
    let output = Command::new(env!("CARGO"))
        .arg("uninstall")
        .arg("mdbook-linkcheck")
        .output()
        .expect("Failed to execute cargo uninstall mdbook-linkcheck");

    if !output.status.success() {
        if String::from_utf8_lossy(&output.stderr)
            .into_owned()
            .contains("did not match any packages")

Review comment (suggestion):
I think the `mut cmd` is unnecessary here:

```suggestion
        let cmd = Command::new(cargo);
            .args(["install", "--path"])
```

Code after:
        run_command(&mut cmd)?;
    }

    let cargo = env!("CARGO");
    // The --locked flag is important for reproducible builds.
    let tools = [
        ("mdbook", "0.4.52"),
        ("mdbook-svgbob", "0.2.2"),
        ("mdbook-pandoc", "0.10.4"),
        ("mdbook-i18n-helpers", "0.3.6"),
        ("i18n-report", "0.2.0"),
        ("mdbook-linkcheck2", "0.9.1"),
    ];

    for (tool, version) in tools {
        let mut cmd = Command::new(cargo);
        cmd.args(["install", tool, "--version", version, "--locked"]);
        run_command(&mut cmd)?;
    }

    // Install local tools from the workspace.
    let workspace_dir = Path::new(env!("CARGO_WORKSPACE_DIR"));
    let local_tools = ["mdbook-exerciser", "mdbook-course"];
    for tool in local_tools {
        let mut cmd = Command::new(cargo);
        cmd.args(["install", "--path"])
            .arg(workspace_dir.join(tool))
            .arg("--locked");
        run_command(&mut cmd)?;
    }

    // Uninstall original linkcheck if currently installed (see issue no 2773)
    uninstall_mdbook_linkcheck()?;

    Ok(())
}

fn uninstall_mdbook_linkcheck() -> Result<()> {
    println!("Uninstalling old mdbook-linkcheck if installed...");
    let output = Command::new(env!("CARGO"))
        .args(["uninstall", "mdbook-linkcheck"])
        .output()
        .context("Failed to execute `cargo uninstall mdbook-linkcheck`")?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        // This specific error is OK, it just means the package wasn't installed.
        if !stderr.contains("did not match any packages") {
            return Err(anyhow!(
                "Failed to uninstall `mdbook-linkcheck`.\n--- stderr:\n{stderr}"
            ));