首页 > 解决方案 > 如何在 CI 上出现警告时导致编译失败并在 .cargo/config 中设置额外的 rustflags?

问题描述

标签: rustcontinuous-integrationrust-cargo

解决方案


您可以使用built::util::detect_ci()来确定当前构建是否在 CI 下执行。然后你可以使用一个小的构建脚本来设置一个 cfg

货运.toml

[package]
build = true

[build-dependencies]
built = { version = "0.3", default_features = false }

构建.rs

fn main() {
    if let Some(ci) = built::util::detect_ci() {
        // There may be a better way to do this
        println!("cargo:rustc-cfg=DENY_WARNINGS");
        println!("cargo:warning=Denying warnings because we are in CI \"{}\"", ci);
    }
}

请注意,构建脚本的结果是缓存的,所以一旦这个构建脚本运行并且 Cargo 决定我们是否在 CI 下运行,它会一直使用这个结果,直到您修改构建脚本或cargo clean. 这对于本地开发或 CI 来说都不是问题,除非 Scotty 定期向您提供帮助。

main.rslib.rs

#![cfg_attr(DENY_WARNINGS, deny(warnings))]

fn main() {
    // This will be a warning locally but fail to compile e.g. if built on Travis
    Result::<(), ()>::Err(());
}

推荐阅读