You need to enable JavaScript to run this app. Rust中如何针对特定模块关闭cargo doc test并排除生成的.rs文件
优惠活动
大模型
产品
解决方案
定价
更多

Rust中如何针对特定模块关闭cargo doc test并排除生成的.rs文件

Excluding Generated gRPC .rs Files From Doc Tests Without Modifying Generated Code

Primary Working Solution

Generated gRPC output files are pulled into your crate via hand-written mod declarations or include! macros that you control. You can disable doc tests for the entire generated code tree without editing any generated files by using the #[doc(test(ignore))] attribute on the root of your generated code module.

  • For generated code organized under a dedicated submodule (e.g. the src/google tree from your use case):
    Modify only the mod declaration in your hand-written crate root (like src/lib.rs):

    #[doc(test(ignore))]
    pub mod google;
    

    This attribute applies to all nested modules under google, including every generated .rs file in its subdirectories. All doc tests (and inline comment code blocks from generated code) will be skipped automatically when running cargo test.

  • For generated files pulled in via include! macros:
    Wrap the include! call in a hand-written module with the same attribute:

    #[doc(test(ignore))]
    mod generated_bigtable_v2 {
        include!("./google/google.bigtable.v2.rs");
    }
    

Optional Secondary Approach

If your crate consists only of generated code (no hand-written business logic), you can disable all doc tests for the entire crate by adding this line to your crate root (src/lib.rs or src/main.rs):

#![doc(test(ignore))]

This is only suitable for dedicated generated code crates, as it will skip all doc tests for your hand-written code too if added to a mixed crate.

Practices to Avoid

  • Manually editing generated .rs files: Changes will be overwritten when you regenerate code from proto files.
  • Relying on manual cargo test flags (like --skip): These add friction for CI/CD and other developers running the test suite.

内容的提问来源于stack exchange,提问作者Fuyang Liu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.05 22:15:05