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

Code before:
            .join("slides")
            .join("create-slide.list.sh");
        let status = Command::new(path_to_refresh_slides_script)
            .current_dir(Path::new(env!("CARGO_WORKSPACE_DIR")))
            .arg(d)
            .status()
            .expect("Failed to execute create-slide.list.sh");

        if !status.success() {
            let error_message = format!(
                "Command 'cargo xtask web-tests' exited with status code: {}",
                status.code().unwrap()
            );
            return Err(anyhow!(error_message));
        }
    }

    let path_to_tests_dir = Path::new(env!("CARGO_WORKSPACE_DIR")).join("tests");
    let mut command = Command::new("npm");
    command.current_dir(path_to_tests_dir.to_str().unwrap());
    command.arg("test");

    if let Some(d) = dir {
        command.env("TEST_BOOK_DIR", d);
    }
    let status = command.status().expect("Failed to execute npm test");

    if !status.success() {
        let error_message = format!(
            "Command 'cargo xtask web-tests' exited with status code: {}",
            status.code().unwrap()
        );
        return Err(anyhow!(error_message));
    }

    Ok(())
}

fn run_rust_tests() -> Result<()> {
    println!("Running rust tests...");

    let path_to_workspace_root = Path::new(env!("CARGO_WORKSPACE_DIR"));

    let status = Command::new("mdbook")
        .current_dir(path_to_workspace_root.to_str().unwrap())
        .arg("test")
        .status()
        .expect("Failed to execute mdbook test");

    if !status.success() {
        let error_message = format!(

Review comment (suggestion):
this is taking the directory provided by the user in direct way and stored in TEST_BOOK_DIR.
This is no issue with absolute paths, as an absolute path always resolves correctly, but if a relative path e.g. "book/html" is being used this causes issues as this is relative to the folder of the working directory (here the CARGO_WORKSPACE_DIR/tests, leading to a path like ..../tests/book/html. The user would need to specify ../book/html to correctly specify this directory. But this conflicts with the above usage (line 130) where the path is relative to the CARGO_WORKSPACE_DIR directly.

We can make our life much more simple by providing a canonical representation (absolute path) to **both** the above script and the npm command. Path (PathBuf) supports a `canonicalize()` function that provides the canonic absolute representation of the path. Please use that for the above script and this call. This should resolve your issue

```
let absolute_dir = dir.map(|d| d.canonicalize()).transpose()?;
```

Code after:
        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)
            .current_dir(Path::new(env!("CARGO_WORKSPACE_DIR")))
            .arg(d)
            .status()
            .expect("Failed to execute create-slide.list.sh");

        if !status.success() {
            let error_message = format!(
                "Command 'cargo xtask web-tests' exited with status code: {}",
                status.code().unwrap()
            );
            return Err(anyhow!(error_message));
        }
    }

    let path_to_tests_dir = Path::new(env!("CARGO_WORKSPACE_DIR")).join("tests");
    let mut command = Command::new("npm");
    command.current_dir(path_to_tests_dir.to_str().unwrap());
    command.arg("test");

    if let Some(d) = absolute_dir {
        command.env("TEST_BOOK_DIR", d);
    }
    let status = command.status().expect("Failed to execute npm test");

    if !status.success() {
        let error_message = format!(
            "Command 'cargo xtask web-tests' exited with status code: {}",
            status.code().unwrap()
        );
        return Err(anyhow!(error_message));
    }

    Ok(())
}

fn run_rust_tests() -> Result<()> {
    println!("Running rust tests...");

    let path_to_workspace_root = Path::new(env!("CARGO_WORKSPACE_DIR"));

    let status = Command::new("mdbook")
        .current_dir(path_to_workspace_root.to_str().unwrap())
        .arg("test")
        .status()
        .expect("Failed to execute mdbook test");