diff --git a/src/cargo/ops/tree/mod.rs b/src/cargo/ops/tree/mod.rs index 4344daa49e7..e92570eace4 100644 --- a/src/cargo/ops/tree/mod.rs +++ b/src/cargo/ops/tree/mod.rs @@ -182,7 +182,7 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<() .collect(); for SpecsAndResolvedFeatures { - specs, + specs: entry_specs, resolved_features, } in ws_resolve.specs_and_features { @@ -190,7 +190,7 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<() ws, &ws_resolve.targeted_resolve, &resolved_features, - &specs, + &entry_specs, &opts.cli_features, &target_data, &requested_kinds, @@ -198,15 +198,30 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<() opts, )?; - let root_specs = if opts.invert.is_empty() { - specs + let root_ids = if opts.invert.is_empty() { + let entry_ids = ws_resolve.targeted_resolve.specs_to_ids(&entry_specs)?; + let requested_ids = ws_resolve + .targeted_resolve + .specs_to_ids(&specs)? + .into_iter() + .collect::>(); + + // `entry_specs` can be broader than the CLI request (for example, + // all workspace members when `feature-unification=workspace`). + // Keep only the packages explicitly requested by `-p`. + entry_ids + .into_iter() + .filter(|id| requested_ids.contains(id)) + .collect() } else { - opts.invert + let invert_specs = opts + .invert .iter() .map(|p| PackageIdSpec::parse(p)) - .collect::, _>>()? + .collect::, _>>()?; + + ws_resolve.targeted_resolve.specs_to_ids(&invert_specs)? }; - let root_ids = ws_resolve.targeted_resolve.specs_to_ids(&root_specs)?; let root_indexes = graph.indexes_from_ids(&root_ids); let root_indexes = if opts.duplicates { diff --git a/tests/testsuite/feature_unification.rs b/tests/testsuite/feature_unification.rs index ea34fed42e2..5025c8d4891 100644 --- a/tests/testsuite/feature_unification.rs +++ b/tests/testsuite/feature_unification.rs @@ -835,6 +835,18 @@ b v0.1.0 ([ROOT]/foo/b) └── outside feature "default" └── outside v0.1.0 +"#]]) + .run(); + + p.cargo("tree -p a") + .arg("-Zfeature-unification") + .masquerade_as_nightly_cargo(&["feature-unification"]) + .env("CARGO_RESOLVER_FEATURE_UNIFICATION", "workspace") + .with_stdout_data(str![[r#" +a v0.1.0 ([ROOT]/foo/a) +├── common v0.1.0 ([ROOT]/foo/common) +└── outside v0.1.0 + "#]]) .run(); }