2

My build.rs script is failing in my CI pipeline (Azure Pipelines), but running it locally is working fine. I specifically know I'm failing to generate my cbindgen header, but the error message is just listing files it parsed and not really saying why it failed.

build.rs:


extern crate cbindgen;

use cbindgen::Config;
use std::env;
use std::path::PathBuf;

fn main() {
    let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
    let build_ffi = env::var("CARGO_GENERATE_FFI").unwrap();

    if build_ffi == "True" {
        let output_file = target_dir().join("smartscreen.h").display().to_string();

        let config = Config::from_file("cbindgen.toml").unwrap();

        cbindgen::generate_with_config(&crate_dir, config)
            .unwrap()
            .write_to_file(&output_file);
    }
}

/// Find the location of the `target/` directory. Note that this may be
/// overridden by `cmake`, so we also need to check the `CARGO_TARGET_DIR`
/// variable.
fn target_dir() -> PathBuf {
    if let Ok(target) = env::var("CARGO_TARGET_DIR") {
        PathBuf::from(target)
    } else {
        PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("target")
    }
}

cbindgen.toml:

language = "C"

[parse]
parse_deps = false
include = ["src/lib.rs"]
exclude = ["src/urlrep.rs", "src/core.rs", "src/service.rs", "src/smartscreen.rs"]
clean = true
extra_bindings = []

[parse.expand]
crates = ["rustscreen"]
all_features = false
default_features = true
features = []

I'm failing at the generate_with_config().unwrap(), and the error message is a list of crates I'm trying to compile and then a typical unwrap() stack starting with:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: 
CargoExpand("rustscreen", Compile("   Compiling libc v0.2.67
Compiling proc-macro2 v1.0.8Compiling unicode-xid v0.2.0Compiling syn v1.0.14
  Running `rustc --crate-name build_script_build /Users/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/libc-0.2.67/build.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C debuginfo=2 --cfg \'feature=\"default\"\' --cfg \'feature=\"std\"\' -C metadata=7d7a4e8d940c7a40 -C extra-filename=-7d7a4e8d940c7a40 --out-dir /var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/cbindgen-expandleR23k/debug/build/libc-7d7a4e8d940c7a40 -L dependency=/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/cbindgen-expandleR23k/debug/deps --cap-lints allow`

... SNIP

Continues like this for each dependency, with no error messages

ZachChilders
  • 415
  • 3
  • 15
  • 1
    IIRC you can just `println!` stuff in `build.rs` and it will show up in the log. – PiRocks May 01 '20 at 21:37
  • I had tried putting a `println!` in the err arm of a match (instead of the offending unwrap), but it didn't change the output, unfortunately. – ZachChilders May 01 '20 at 22:24
  • 1
    Standard output might not show up in the task's log. Instead of using `println!`, you can check out [this document](https://doc.rust-lang.org/book/ch12-06-writing-to-stderr-instead-of-stdout.html) and try writing to Standard error. – Levi Lu-MSFT May 04 '20 at 09:51
  • Your question solved my separate issue of needing to know the target directory! Thank you!!! – Sam Johnson May 30 '21 at 22:20

0 Answers0