我的 build.rs 脚本在 CI 管道(Azure Pipelines)中失败,但在本地运行它工作正常。 我特别知道我无法生成 cbindgen 标头,但错误消息只是列出它解析的文件,并没有真正说明失败的原因。
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 = []
我在generate_with_config().unwrap()上失败了,错误消息是我试图编译的板条箱列表,然后是一个典型的unwrap()堆栈,开头为:
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
对每个依赖项都像这样继续,没有错误消息
它可能无法100%解决您的问题,但它会帮助您获得更好的错误消息,即将
.unwrap
替换为.expect
例如,您的
build.rs
可以重写为:
// ..
fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
let build_ffi = env::var("CARGO_GENERATE_FFI").expect("CARGO_GENERATE_FFI not set");
if build_ffi == "True" {
let output_file = target_d
// ..
这样您将收到编译器发出的类似于以下内容的错误消息
Caused by:
process didn't exit successfully: `/Users/picard/stackoverflow/questions/61548743/is-there-a-good-way-to-debug-cbindgen-usage-in-my-build-rs/target/debug/build/xyz-0df08043e3b5e608/build-script-build` (exit status: 101)
--- stderr
thread 'main' panicked at build.rs:9:52:
CARGO_GENERATE_FFI not set: NotPresent
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
这个错误
CARGO_GENERATE_FFI not set: NotPresent
很明显。
此外,您还应该验证
assert!
中使用 assert_eq!
或 build.rs
所做的所有假设,并提供有意义的消息。