cargo: panic when attempting to `cargo doc -p` a dev-dependency

Problem

Panic occurs when attempting to build the docs of a crate added as a dev-dependency.

Steps

> cargo new foo && cd foo && cargo add --dev serde && RUST_BACKTRACE=1 cargo doc -p serde
     Created binary (application) `foo` package
    Updating crates.io index
      Adding serde v1.0.144 to dev-dependencies.
             Features:
             + std
             - alloc
             - derive
             - rc
             - serde_derive
             - unstable
thread 'main' panicked at 'activated_features for invalid package: features did not find PackageId { name: "serde", version: "1.0.144", source: "registry `crates-io`" } NormalOrDevOrArtifactTarget(None)', src/tools/cargo/src/cargo/core/resolver/features.rs:318:14
stack backtrace:
   0: rust_begin_unwind
             at /rustc/c07a8b4e09f356c7468b69c50cac7fc5b5000b8a/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/c07a8b4e09f356c7468b69c50cac7fc5b5000b8a/library/core/src/panicking.rs:142:14
   2: core::result::unwrap_failed
             at /rustc/c07a8b4e09f356c7468b69c50cac7fc5b5000b8a/library/core/src/result.rs:1785:5
   3: cargo::ops::cargo_compile::generate_targets
   4: cargo::ops::cargo_compile::create_bcx
   5: cargo::ops::cargo_compile::compile_ws
   6: cargo::ops::cargo_compile::compile
   7: cargo::ops::cargo_doc::doc
   8: cargo::commands::doc::exec
   9: cargo::cli::main
  10: cargo::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Possible Solution(s)

No response

Notes

No response

Version

cargo 1.65.0-nightly (6da726708 2022-08-23)
release: 1.65.0-nightly
commit-hash: 6da726708a4406f31f996d813790818dce837161
commit-date: 2022-08-23
host: x86_64-unknown-linux-gnu
libgit2: 1.4.2 (sys:0.14.2 vendored)
libcurl: 7.83.1-DEV (sys:0.4.55+curl-7.83.1 vendored ssl:OpenSSL/1.1.1q)
os: Arch Linux [64-bit]

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 5
  • Comments: 15 (14 by maintainers)

Most upvoted comments

IMO it’s an important usecase, when you are developing offline and need to check the API documentation of your dev-dependencies. There are workarounds like temporarily adding them to your dependencies, or adding an example and using cargo doc --examples (though this builds docs for all dev-dependencies, not just whichever one you are interested in), but it seems like it should just work.

I also hit this problem in a slightly different context. Turns out, it affects any cargo command when using -p on a dev dependency (cargo build, cargo clippy, cargo doc, …)

Minimum Cargo.toml to reproduce:

[package]
name = "cargo-panic"
version = "0.1.0"
edition = "2021"

[dev-dependencies]
anyhow = "1"

Then try e.g. cargo build -p anyhow

No matter if you decide to support this or not, cargo shouldn’t panic here. I can also confirm that edition = "2018" works fine.

I’ll bring this to the next triage meeting if there is any available slot.

@Nemo157, @hi-rustin or anyone, if you’ve got more thoughts on this, please let us know. Thank you!

I debugged it. I found the cause of the panic is we do not contain the package information in https://github.com/rust-lang/cargo/blob/13ae438cf079da58272edc71f4d4968043dbd27b/src/cargo/core/resolver/features.rs#L55