Skip to content

Commit ee13fd6

Browse files
committed
feat(workspace): Get all members even though default-members was specified
1 parent 6a478ef commit ee13fd6

File tree

3 files changed

+79
-40
lines changed

3 files changed

+79
-40
lines changed

src/cargo/ops/cargo_compile/unit_generator.rs

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::util::{closest_msg, CargoResult};
1717

1818
use super::compile_filter::{CompileFilter, FilterRule, LibRule};
1919
use super::packages::build_glob;
20+
use super::Packages;
2021

2122
/// A proposed target.
2223
///
@@ -267,35 +268,61 @@ impl<'a> UnitGenerator<'a, '_> {
267268
})
268269
.collect::<Vec<_>>();
269270
let suggestion = closest_msg(target_name, targets.iter(), |t| t.name(), "target");
270-
if !suggestion.is_empty() {
271-
anyhow::bail!(
272-
"no {} target {} `{}`{}",
273-
target_desc,
274-
if is_glob { "matches pattern" } else { "named" },
275-
target_name,
276-
suggestion
277-
);
271+
let target_elsewhere = if is_glob {
272+
let pattern = build_glob(target_name)?;
273+
let filter = |t: &Target| is_expected_kind(t) && pattern.matches(t.name());
274+
self.get_targets_from_other_packages(filter)?
278275
} else {
279-
let mut msg = String::new();
280-
writeln!(
281-
msg,
282-
"no {} target {} `{}`.",
283-
target_desc,
284-
if is_glob { "matches pattern" } else { "named" },
285-
target_name,
286-
)?;
287-
if !targets.is_empty() {
288-
writeln!(msg, "Available {} targets:", target_desc)?;
289-
for target in targets {
290-
writeln!(msg, " {}", target.name())?;
291-
}
276+
let filter = |t: &Target| t.name() == target_name && is_expected_kind(t);
277+
self.get_targets_from_other_packages(filter)?
278+
};
279+
280+
let mut msg = String::new();
281+
writeln!(
282+
msg,
283+
"no {} target {} `{}`",
284+
target_desc,
285+
if is_glob { "matches pattern" } else { "named" },
286+
target_name
287+
)?;
288+
if let Some((package, target)) = target_elsewhere {
289+
writeln!(msg, "Available {target_desc} targets in {package}:")?;
290+
writeln!(msg, " {target}")?;
291+
} else if !suggestion.is_empty() {
292+
writeln!(msg, "{suggestion}")?;
293+
} else if !targets.is_empty() {
294+
writeln!(msg, "Available {} targets:", target_desc)?;
295+
for target in targets {
296+
writeln!(msg, " {}", target.name())?;
292297
}
293-
anyhow::bail!(msg);
294298
}
299+
300+
anyhow::bail!(msg);
295301
}
296302
Ok(proposals)
297303
}
298304

305+
fn get_targets_from_other_packages(
306+
&self,
307+
filter_fn: impl Fn(&Target) -> bool,
308+
) -> CargoResult<Option<(String, String)>> {
309+
let packages = Packages::All(Vec::new()).get_packages(self.ws)?;
310+
let targets = packages.into_iter().find_map(|pkg| {
311+
if let Some(target) = pkg
312+
.manifest()
313+
.targets()
314+
.iter()
315+
.find(|target| filter_fn(target))
316+
{
317+
Some((pkg.name().to_string(), target.name().to_string()))
318+
} else {
319+
None
320+
}
321+
});
322+
323+
Ok(targets)
324+
}
325+
299326
/// Returns a list of proposed targets based on command-line target selection flags.
300327
fn list_rule_targets(
301328
&self,

src/cargo/util/workspace.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,40 @@ use crate::core::compiler::Unit;
22
use crate::core::manifest::TargetSourcePath;
33
use crate::core::{Target, Workspace};
44
use crate::ops::CompileOptions;
5+
use crate::ops::Packages;
56
use crate::util::CargoResult;
67
use anyhow::bail;
78
use cargo_util::paths::normalize_path;
89
use cargo_util::ProcessBuilder;
10+
use std::collections::BTreeMap;
911
use std::fmt::Write;
1012
use std::path::PathBuf;
1113

1214
fn get_available_targets<'a>(
1315
filter_fn: fn(&Target) -> bool,
1416
ws: &'a Workspace<'_>,
1517
options: &'a CompileOptions,
16-
) -> CargoResult<Vec<&'a str>> {
17-
let packages = options.spec.get_packages(ws)?;
18-
19-
let mut targets: Vec<_> = packages
18+
) -> CargoResult<BTreeMap<String, Vec<String>>> {
19+
let packages = if matches!(options.spec, Packages::Default) {
20+
Packages::All(Vec::new()).get_packages(ws)?
21+
} else {
22+
options.spec.get_packages(ws)?
23+
};
24+
let targets: BTreeMap<String, Vec<String>> = packages
2025
.into_iter()
21-
.flat_map(|pkg| {
22-
pkg.manifest()
26+
.map(|pkg| {
27+
let mut targets = pkg
28+
.manifest()
2329
.targets()
2430
.iter()
2531
.filter(|target| filter_fn(target))
32+
.map(|t| t.name().to_owned())
33+
.collect::<Vec<String>>();
34+
targets.sort();
35+
(pkg.name().to_string(), targets)
2636
})
27-
.map(Target::name)
2837
.collect();
2938

30-
targets.sort();
31-
3239
Ok(targets)
3340
}
3441

@@ -47,9 +54,11 @@ fn print_available_targets(
4754
if targets.is_empty() {
4855
writeln!(output, "No {} available.", plural_name)?;
4956
} else {
50-
writeln!(output, "Available {}:", plural_name)?;
51-
for target in targets {
52-
writeln!(output, " {}", target)?;
57+
for (package, targets) in targets {
58+
writeln!(output, "Available {} in {}:", plural_name, package)?;
59+
for target in targets {
60+
writeln!(output, " {}", target)?;
61+
}
5362
}
5463
}
5564
bail!("{}", output)

tests/testsuite/workspaces.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2767,8 +2767,10 @@ fn print_available_targets_within_virtual_workspace() {
27672767
.with_status(101)
27682768
.with_stderr_data(str![[r#"
27692769
[ERROR] "--bin" takes one argument.
2770-
Available binaries:
2770+
Available binaries in crate1:
27712771
crate1
2772+
Available binaries in crate2:
2773+
crate2
27722774
27732775
27742776
"#]])
@@ -2778,13 +2780,14 @@ Available binaries:
27782780
.with_status(101)
27792781
.with_stderr_data(str![[r#"
27802782
[ERROR] no bin target named `crate2`
2783+
Available bin targets in crate2:
2784+
crate2
27812785
2782-
[HELP] a target with a similar name exists: `crate1`
27832786
27842787
"#]])
27852788
.run();
27862789

2787-
// This another branch that none of similar name exists, and print available targets in the
2790+
// This another branch that none of similar name exists, and print available targets in the
27882791
// default-members.
27892792
p.change_file(
27902793
"Cargo.toml",
@@ -2800,9 +2803,9 @@ Available binaries:
28002803
p.cargo("run --bin crate2")
28012804
.with_status(101)
28022805
.with_stderr_data(str![[r#"
2803-
[ERROR] no bin target named `crate2`.
2804-
Available bin targets:
2805-
another
2806+
[ERROR] no bin target named `crate2`
2807+
Available bin targets in crate2:
2808+
crate2
28062809
28072810
28082811
"#]])

0 commit comments

Comments
 (0)