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

Code before:
        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")
        {
            println!("mdbook-linkcheck not installed. Continuing...");
        } else {
            eprintln!(
                "An error occurred during uninstallation of mdbook-linkcheck:\n{:#?}",
                String::from_utf8_lossy(&output.stderr)
            );
        }
    }
}

fn run_web_tests(dir: Option<PathBuf>) -> Result<()> {
    println!("Running web tests...");

    let absolute_dir = dir.map(|d| d.canonicalize()).transpose()?;

    if let Some(d) = &absolute_dir {
        println!("Refreshing slide lists...");
        let path_to_refresh_slides_script = Path::new("tests")
            .join("src")
            .join("slides")
            .join("create-slide.list.sh");
        let status = Command::new(path_to_refresh_slides_script)

Review comment (suggestion):
I think you could use [`bail!`](https://docs.rs/anyhow/latest/anyhow/macro.bail.html) here:

```suggestion
            bail!(
```

Code after:
    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}"
            ));
        }
        println!("mdbook-linkcheck not installed. Continuing...");
    }
    Ok(())
}

fn run_web_tests(dir: Option<PathBuf>) -> Result<()> {
    println!("Running web tests...");
    let workspace_dir = Path::new(env!("CARGO_WORKSPACE_DIR"));

    let absolute_dir = dir.map(|d| d.canonicalize()).transpose()?;

    if let Some(d) = &absolute_dir {
        println!("Refreshing slide lists...");
        let refresh_slides_script = Path::new("tests")
            .join("src")
            .join("slides")
            .join("create-slide.list.sh");
        let mut cmd = Command::new(&refresh_slides_script);
        cmd.current_dir(workspace_dir).arg(d);
        run_command(&mut cmd)?;
    }