【问题标题】:How can I cause compilation to fail on warnings on CI and have extra rustflags set in .cargo/config?如何导致编译因 CI 上的警告而失败并在 .cargo/config 中设置额外的 rustflags?
【发布时间】:2019-11-14 03:52:40
【问题描述】:

我试图让 Cargo 在 CI 上出现警告时失败,但在本地开发时却没有。

我有一个可行的解决方案,即在 CI 作业中设置 RUSTFLAGS=“-D warnings”。这很有效,因为它确实会导致本地 crates 的构建失败,但依赖 crates 不会失败(由于 —cap-lints)。

最近,必须在(签入).cargo/config 文件中设置一些 rustflag:

[target.’cfg(target_os = “linux”)’]
rustflags = [“some”, “options”]

这不起作用,因为RUSTFLAGS 优先,.cargo/config 标志将被忽略。我不想将-D warnings 添加到config,因为这在开发过程中会很痛苦。

当前的解决方法是在 CI 作业之前调整 config 的脚本:

sed -i "s:\(rustflags = .*\)]:\1, \"-D\", \"warnings\"]:g" .cargo/config
echo '[build]' >> .cargo/config
echo 'rustflags = [ "-D", "warnings"]' >> .cargo/config

这会将-D warnings 附加到配置中的现有rustflags 创建一个额外的包罗万象的config 条目,以确保-D warnings 在非Linux CI 版本上也启用.

这太骇人听闻了;有没有更好的解决方案?

也许这应该是对 Cargo 存储库的功能请求,但我不知道理想的解决方案是什么样的。

【问题讨论】:

    标签: rust continuous-integration rust-cargo


    【解决方案1】:

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

    Cargo.toml

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

    build.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 下运行,它会一直使用这个结果,直到你修改构建脚本或 @987654325 @。这对于本地开发或 CI 来说都不是问题,除非 Scotty 定期向您提供帮助。

    ma​​in.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(());
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 2019-05-28
      • 2020-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多