rust-lang/cargo · error · anyhow::Error

package ` ` cannot be tested because it requires…

Error message

package `{}` cannot be tested because it requires dev-dependencies and is not a member of the workspace

What it means

Thrown while building test/bench units when a package is NOT a workspace member but declares non-transitive (i.e. dev-) dependencies. cargo cannot provide dev-deps for a non-member path dependency, so testing it directly is refused. The check is `intent.is_any_test() && !ws.is_member(pkg) && any dep is not transitive`.

Solutions

  1. Add the package to the workspace members list in the root Cargo.toml so its dev-deps resolve.
  2. Run tests from within that package's own checkout where it is the workspace root.
  3. If you only need its library, test a non-test intent (e.g. `cargo build -p <pkg>`).
  4. Remove the dev-dependencies from that package if testing it from the downstream is required and they are unused.

Example fix

# before (root Cargo.toml)
[workspace]
members = ["app"]
# after
[workspace]
members = ["app", "../path-dep"]
Defensive patterns

Strategy: validation

Validate before calling

// Confirm the package is a workspace member before targeting it for tests.
fn is_member(ws_root: &Path, pkg: &str) -> bool {
    // parse root Cargo.toml [workspace].members and glob-match
    true
}

Prevention

When it happens

Trigger: Running `cargo test -p <path-dep>` where <path-dep> is a path dependency pulled into the resolve graph but not listed in the workspace members; testing a vendored crate that has its own [dev-dependencies].

Common situations: Trying to test an upstream path dependency from within a downstream project; monorepo where a crate is referenced by path but not added to the workspace; after splitting a crate out without adding it as a member.

Related errors


AI-assisted analysis of rust-lang/cargo@495c385d08 (2026-08-11). Data as JSON: /api/errors/4b622d7284ce129b. Report an issue: GitHub.

Appendix: source

Thrown at src/ops/cargo_compile/mod.rs:385

    let to_build_ids = resolve.specs_to_ids(&specs)?;
    // Now get the `Package` for each `PackageId`. This may trigger a download
    // if the user specified `-p` for a dependency that is not downloaded.
    // Dependencies will be downloaded during build_unit_dependencies.
    let mut to_builds = pkg_set.get_many(to_build_ids)?;

    // The ordering here affects some error messages coming out of cargo, so
    // let's be test and CLI friendly by always printing in the same order if
    // there's an error.
    to_builds.sort_by_key(|p| p.package_id());

    for pkg in to_builds.iter() {
        pkg.manifest().print_teapot(gctx);

        if build_config.intent.is_any_test()
            && !ws.is_member(pkg)
            && pkg.dependencies().iter().any(|dep| !dep.is_transitive())
        {
            anyhow::bail!(
                "package `{}` cannot be tested because it requires dev-dependencies \
                 and is not a member of the workspace",
                pkg.name()
            );
        }
    }

    let (extra_args, extra_args_name) = match (target_rustc_args, target_rustdoc_args) {
        (Some(args), _) => (Some(args.clone()), "rustc"),
        (_, Some(args)) => (Some(args.clone()), "rustdoc"),
        _ => (None, ""),
    };

    if extra_args.is_some() && to_builds.len() != 1 {
        panic!(
            "`{}` should not accept multiple `-p` flags",
            extra_args_name
        );

View on GitHub (pinned to 495c385d08)