{"record":{"id":"4b622d7284ce129b","repo":"rust-lang/cargo","slug":"package-cannot-be-tested-because-it-requires","errorCode":null,"errorMessage":"package `{}` cannot be tested because it requires dev-dependencies and is not a member of the workspace","messagePattern":"package `(.+?)` cannot be tested because it requires dev-dependencies and is not a member of the workspace","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_compile/mod.rs","lineNumber":385,"sourceCode":"    let to_build_ids = resolve.specs_to_ids(&specs)?;\n    // Now get the `Package` for each `PackageId`. This may trigger a download\n    // if the user specified `-p` for a dependency that is not downloaded.\n    // Dependencies will be downloaded during build_unit_dependencies.\n    let mut to_builds = pkg_set.get_many(to_build_ids)?;\n\n    // The ordering here affects some error messages coming out of cargo, so\n    // let's be test and CLI friendly by always printing in the same order if\n    // there's an error.\n    to_builds.sort_by_key(|p| p.package_id());\n\n    for pkg in to_builds.iter() {\n        pkg.manifest().print_teapot(gctx);\n\n        if build_config.intent.is_any_test()\n            && !ws.is_member(pkg)\n            && pkg.dependencies().iter().any(|dep| !dep.is_transitive())\n        {\n            anyhow::bail!(\n                \"package `{}` cannot be tested because it requires dev-dependencies \\\n                 and is not a member of the workspace\",\n                pkg.name()\n            );\n        }\n    }\n\n    let (extra_args, extra_args_name) = match (target_rustc_args, target_rustdoc_args) {\n        (Some(args), _) => (Some(args.clone()), \"rustc\"),\n        (_, Some(args)) => (Some(args.clone()), \"rustdoc\"),\n        _ => (None, \"\"),\n    };\n\n    if extra_args.is_some() && to_builds.len() != 1 {\n        panic!(\n            \"`{}` should not accept multiple `-p` flags\",\n            extra_args_name\n        );","sourceCodeStart":367,"sourceCodeEnd":403,"githubUrl":"https://github.com/rust-lang/cargo/blob/495c385d0875c4ba51eb72ea0448a2d4c018b8d4/src/ops/cargo_compile/mod.rs#L367-L403","documentation":"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`.","triggerScenarios":"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].","commonSituations":"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.","solutions":["Add the package to the workspace members list in the root Cargo.toml so its dev-deps resolve.","Run tests from within that package's own checkout where it is the workspace root.","If you only need its library, test a non-test intent (e.g. `cargo build -p <pkg>`).","Remove the dev-dependencies from that package if testing it from the downstream is required and they are unused."],"exampleFix":"# before (root Cargo.toml)\n[workspace]\nmembers = [\"app\"]\n# after\n[workspace]\nmembers = [\"app\", \"../path-dep\"]","handlingStrategy":"validation","validationCode":"// Confirm the package is a workspace member before targeting it for tests.\nfn is_member(ws_root: &Path, pkg: &str) -> bool {\n    // parse root Cargo.toml [workspace].members and glob-match\n    true\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Add path dependencies you need to test to the workspace members list.","Test upstream crates from their own checkout, not the downstream project.","Avoid dev-dependencies in path-dep crates used outside a workspace."],"tags":["cargo-compile","dev-dependencies","workspace","testing"],"backgroundTag":null,"analyzedSha":"495c385d0875c4ba51eb72ea0448a2d4c018b8d4","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}