Library doc doesn't build

Hi, I published a library for windows but the doc wasn't compiled because of errors such as these

[INFO] [stderr] error[E0433]: failed to resolve: could not find `um` in `winapi`
[INFO] [stderr]  --> src/lib.rs:9:13
[INFO] [stderr]   |
[INFO] [stderr] 9 | use winapi::um::processthreadsapi::GetCurrentProcess;
[INFO] [stderr]   |             ^^ could not find `um` in `winapi`

I had this type of errors when compiling the code, which was solved by adding the corresponding features in Cargo.toml, but that doesn't work for the doc. cargo doc works but cargo rustdoc doesn't and gives the same errors. What should I do?

cargo rustdoc doesn’t integrate with more complex features of cargo like crate features (or dependencies if I’m reading this right). It’s analogous to cargo rustc, which is an advanced command. The solution is to use cargo doc. This SO answer gives some more context and information. Do you have a specific reason you need to use cargo rustdoc?

No, I just tested with cargo rustdoc because that's what crates.io uses to build the doc. Is there a way to make crates.io build the doc with cargo doc instead?

If the cause of the failure is that docs.rs should enable all features for your crate when building the docs but isn't (It's hard to tell from your descriptions so far), you can tell docs.rs to do so by adding the following to your Cargo.toml:

[package.metadata.docs.rs]
all-features = true
2 Likes

Is it supposed to make cargo rustdoc work?

It ought to make the docs on crates.io build.

It went like this:

Crates.io built your crate's docs using no features

Cargo rustdoc complained due to missing features

You ran cargo rustdoc and got the same error.

You ran cargo doc and didn't get the error, because it built with the features

Crates.io will do the same thing (build with features) if explicitly told to do so by Cargo.toml

It doesn't "fix cargo rustdoc" - it will continue to do what it's told to do and build with/without features. Without features you'll see your error.

I'm not sure what exactly happens while crates.io is deciding which features to use while building your docs.
Nor do I know why it uses different features than cargo doc by default. I think those are interesting questions though!

It doesn't work.

The solution is to add this to Cargo.toml

[package.metadata.docs.rs]
default-target = "x86_64-pc-windows-gnu"
targets = ["x86_64-pc-windows-gnu", "x86_64-pc-windows-msvc"]
1 Like