Skip to content

#[cfg(doc)] does not work in Cargo.toml #11570

@arctic-hen7

Description

@arctic-hen7

Hey! I'm working on a library that uses two custom RUSTFLAGS to control what it provides for different environments, and those two flags are mutually exclusive. However, I wanted to make sure items from both were available in the documentation produced by cargo doc, so I followed these instructions and used #[cfg(doc)] for this. That works excellently, except the dependencies of the crate are also controlled by these custom flags (e.g. [target.'cfg(engine)'.dependencies]), so I expected to be able to write something like this:

[target.'cfg(any(client, doc))'.dependencies]

However, this doesn't seem to work at all, because I still get all the usual unresolved import errors. Changing this to use another custom flag clientdoc, rather than the internal doc, and then running

RUSTFLAGS="--cfg=engine --cfg=clientdoc" cargo doc

works perfectly. (Although one of these custom flags is involved with importing traditionally Wasm-specific crates, I am almost certain that this is not the problem.)

I expected that #[cfg(doc)] would, like any custom RUSTFLAGS I provide, be applied to Cargo.toml as well as the actual code.

Instead, this does not seem to occur, and another custom flag has to be used, which isn't ideal.

Meta

rustc --version --verbose:

rustc 1.68.0-nightly (92c1937a9 2022-12-27)
binary: rustc
commit-hash: 92c1937a90e5b6f20fa6e87016d6869da363972e
commit-date: 2022-12-27
host: x86_64-unknown-linux-gnu
release: 1.68.0-nightly
LLVM version: 15.0.6
Backtrace

<many more errors of the same kind>
error[E0432]: unresolved import `web_sys`
  --> packages/perseus/src/reactor/initial_load.rs:19:5
   |
19 | use web_sys::Element;
   |     ^^^^^^^ use of undeclared crate or module `web_sys`

error[E0432]: unresolved import `wasm_bindgen`
   --> packages/perseus/src/reactor/initial_load.rs:256:13
    |
256 |         use wasm_bindgen::JsCast;
    |             ^^^^^^^^^^^^ use of undeclared crate or module `wasm_bindgen`

error[E0433]: failed to resolve: use of undeclared crate or module `web_sys`
  --> packages/perseus/src/utils/render.rs:25:13
   |
25 |     parent: web_sys::Element,
   |             ^^^^^^^ use of undeclared crate or module `web_sys`

error: Compilation failed, aborting rustdoc

Some errors have detailed explanations: E0432, E0433.
For more information about an error, try `rustc --explain E0432`.
error: could not document `perseus`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-cfg-exprArea: Platform cfg expressionsA-target-dependenciesArea: [target.'cfg(foo)'.dependencies]C-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions