bazelbuild / bazelbuild/rules_rust

`cargo_build_script` unable to build crate dependency (works fine with `cargo`)

Open
#2,432 0 comments 0 reactions 0 assignees View on GitHub
needs-triage
Dominant language
Starlark
Stars
843
Forks
651
Avg merge
2d 18h
Merged PRs (30d)
15

Description

I'm currently working on a project that depends on [headless-chrome](https://github.com/rust-headless-chrome/rust-headless-chrome) which also has [auto_generate_cdp](https://github.com/mdrokz/auto_generate_cdp) as a build dependency i.e. `my_project -> headless_chrome -> auto_generate_cdp`.

I ran this with `cargo` and all worked fine but failed with Bazel. During my investigation, the generated code by the `auto_generate_cdp` seems to be missing or the crate couldn't figure it out and it panicked!

This is my first experience using Bazel with Rust, so it's possible I don't understand something going wrong somewhere.

This is the failing snippet in the crate:

```rust
// src/compile.rs

if cfg!(feature = "offline") {
let path = Path::new(MANIFEST_DIR).join("json").join(file_name);

let json = std::fs::read_to_string(path).unwrap();

let protocol: Protocol = serde_json::from_str(&json).unwrap();

return protocol;
}

```

It has a build.rs

```rust
// build.rs

fn main() {
if cfg!(feature = "offline") {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();

#[cfg(target_os = "windows")]
let manifest_dir = manifest_dir.replace(r#"\"#,r#"\\"#);

let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("path.rs");

let mut f = File::create(&dest_path).unwrap();
write!(
f,
"pub const MANIFEST_DIR: &str = \"{}\";",
manifest_dir
).unwrap();
}
}
```

This is the generated `cargo_build_script` for `headless_chrome` which my project depends on:

```
cargo_build_script(
name = "headless_chrome_build_script",
crate_features = [
"auto_generate_cdp/offline",
"default",
"directories",
"fetch",
"offline",
"ureq",
"walkdir",
"zip",
],
crate_name = "build_script_build",
crate_root = "build.rs",
data = glob(
include = ["**"],
exclude = [
"**/* *",
".tmp_git_root/**/*",
"BUILD",
"BUILD.bazel",
"WORKSPACE",
"WORKSPACE.bazel",
],
),
deps = [
"@crate_index__auto_generate_cdp-0.4.4//:auto_generate_cdp",
],
edition = "2021",
rustc_flags = [
"--cap-lints=allow",
],
srcs = glob(["**/*.rs"]),
tags = [
"cargo-bazel",
"crate-name=headless_chrome",
"manual",
"noclippy",
"norustfmt",
],
version = "1.0.8",
visibility = ["//visibility:private"],
)
```

And I have this in my WORKSPACE:

```
crates_repository(
name = "crate_index",
annotations = {
"headless_chrome": [crate.annotation(
crate_features = [
"fetch",
"auto_generate_cdp/offline",
],
)],
},
cargo_lockfile = "//:Cargo.lock",
lockfile = "//:cargo.bazel.lock",
manifests = [
"//:Cargo.toml",
"//:scraper/Cargo.toml",
],
)
```

## The Error

```
--stderr:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }', external/crate_index__auto_generate_cdp-0.4.4/src/compile.rs:1267:50
```

I would greatly appreciate any pointers.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.