Skip to content

cargo doc doesn't pass --cfg doc to rustc #8601

Closed
@jmillikin

Description

@jmillikin

Problem
PR rust-lang/rust#53076 added support for #[cfg(doc)], which conditionally enables code when being built for documentation. This cfg is set by rustdoc when parsing a module, but is not set by Cargo when running rustc prior to running rustdoc.

This causes confusing failures to document code when the current platform can't build it, but should be able to document it.

Steps
Given this library:

pub struct T {}

impl T {
    pub const SOME_CONST: u32 = match () {
        #[cfg(target_os = "linux")] _ => 1,
        #[cfg(target_os = "freebsd")] _ => 2,
        #[cfg(doc)] _ => 3,
    };
}

A macOS machine should be able to run cargo doc to build docs, even though the library doesn't support that platform. Currently this fails

To confirm that this is caused by a bad rustc argv, invoke Cargo with RUSTFLAGS="--cfg doc" cargo doc and observe that it builds the docs successfully.

Possible Solution(s)
The best solution would be to pass --cfg doc to rustc when building docs.

Notes

Output of cargo version:

$ cargo --version
cargo 1.47.0-nightly (1653f3546 2020-08-04)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions