Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get all members as available targets even though default-members was specified. #15199

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 66 additions & 16 deletions src/cargo/ops/cargo_compile/unit_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::util::{closest_msg, CargoResult};

use super::compile_filter::{CompileFilter, FilterRule, LibRule};
use super::packages::build_glob;
use super::Packages;

/// A proposed target.
///
Expand Down Expand Up @@ -247,15 +248,15 @@ impl<'a> UnitGenerator<'a, '_> {
mode: CompileMode,
) -> CargoResult<Vec<Proposal<'a>>> {
let is_glob = is_glob_pattern(target_name);
let proposals = if is_glob {
let pattern = build_glob(target_name)?;
let filter = |t: &Target| is_expected_kind(t) && pattern.matches(t.name());
self.filter_targets(filter, true, mode)
} else {
let filter = |t: &Target| t.name() == target_name && is_expected_kind(t);
self.filter_targets(filter, true, mode)
let pattern = build_glob(target_name)?;
let filter = |t: &Target| {
if is_glob {
is_expected_kind(t) && pattern.matches(t.name())
} else {
is_expected_kind(t) && t.name() == target_name
}
};

let proposals = self.filter_targets(filter, true, mode);
if proposals.is_empty() {
let targets = self
.packages
Expand All @@ -267,35 +268,84 @@ impl<'a> UnitGenerator<'a, '_> {
})
.collect::<Vec<_>>();
let suggestion = closest_msg(target_name, targets.iter(), |t| t.name(), "target");
let targets_elsewhere = self.get_targets_from_other_packages(filter)?;
let need_append_targets_elsewhere = !targets_elsewhere.is_empty();
let append_targets_elsewhere = |msg: &mut String| {
for (package, targets) in targets_elsewhere {
if !targets.is_empty() {
writeln!(msg, "Available {target_desc} in `{package}` package:")?;
for target in targets {
writeln!(msg, " {target}")?;
}
}
}
CargoResult::Ok(())
};

let mut msg = String::new();
if !suggestion.is_empty() {
anyhow::bail!(
"no {} target {} `{}`{}",
write!(
msg,
"no {} target {} `{}` in default-run packages{}{}",
target_desc,
if is_glob { "matches pattern" } else { "named" },
target_name,
suggestion
);
suggestion,
if need_append_targets_elsewhere {
"\n"
} else {
""
},
)?;
append_targets_elsewhere(&mut msg)?;
} else {
let mut msg = String::new();
writeln!(
msg,
"no {} target {} `{}`.",
"no {} target {} `{}` in default-run packages.",
target_desc,
if is_glob { "matches pattern" } else { "named" },
target_name,
)?;
if !targets.is_empty() {

append_targets_elsewhere(&mut msg)?;
if !targets.is_empty() && !need_append_targets_elsewhere {
writeln!(msg, "Available {} targets:", target_desc)?;
for target in targets {
writeln!(msg, " {}", target.name())?;
}
}
anyhow::bail!(msg);
}
anyhow::bail!(msg);
}
Ok(proposals)
}

fn get_targets_from_other_packages(
&self,
filter_fn: impl Fn(&Target) -> bool,
) -> CargoResult<Vec<(&str, Vec<&str>)>> {
let packages = Packages::All(Vec::new()).get_packages(self.ws)?;
let targets = packages
.into_iter()
.filter_map(|pkg| {
let mut targets: Vec<_> = pkg
.manifest()
.targets()
.iter()
.filter_map(|target| filter_fn(target).then(|| target.name()))
.collect();
if targets.is_empty() {
None
} else {
targets.sort();
Some((pkg.name().as_str(), targets))
}
})
.collect();

Ok(targets)
}

/// Returns a list of proposed targets based on command-line target selection flags.
fn list_rule_targets(
&self,
Expand Down
44 changes: 33 additions & 11 deletions src/cargo/util/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,46 @@ use crate::core::compiler::Unit;
use crate::core::manifest::TargetSourcePath;
use crate::core::{Target, Workspace};
use crate::ops::CompileOptions;
use crate::ops::Packages;
use crate::util::CargoResult;
use anyhow::bail;
use cargo_util::paths::normalize_path;
use cargo_util::ProcessBuilder;
use std::collections::BTreeMap;
use std::fmt::Write;
use std::path::PathBuf;

fn get_available_targets<'a>(
filter_fn: fn(&Target) -> bool,
ws: &'a Workspace<'_>,
options: &'a CompileOptions,
) -> CargoResult<Vec<&'a str>> {
let packages = options.spec.get_packages(ws)?;
) -> CargoResult<BTreeMap<String, Vec<String>>> {
let packages = if matches!(options.spec, Packages::Default) {
Packages::All(Vec::new()).get_packages(ws)?
} else {
options.spec.get_packages(ws)?
};

let mut targets: Vec<_> = packages
let targets: BTreeMap<String, Vec<String>> = packages
.into_iter()
.flat_map(|pkg| {
pkg.manifest()
.filter_map(|pkg| {
let mut targets = pkg
.manifest()
.targets()
.iter()
.filter(|target| filter_fn(target))
.map(|t| t.name().to_owned())
.collect::<Vec<String>>();

if targets.is_empty() {
return None;
}
targets.sort();

Some((pkg.name().to_string(), targets))
})
.map(Target::name)
.collect();

targets.sort();

Ok(targets)
}

Expand All @@ -47,9 +60,18 @@ fn print_available_targets(
if targets.is_empty() {
writeln!(output, "No {} available.", plural_name)?;
} else {
writeln!(output, "Available {}:", plural_name)?;
for target in targets {
writeln!(output, " {}", target)?;
for (package, targets) in targets {
if targets.is_empty() {
continue;
}
writeln!(
output,
"Available {} in `{}` package:",
plural_name, package
)?;
for target in targets {
writeln!(output, " {}", target)?;
}
}
}
bail!("{}", output)
Expand Down
12 changes: 6 additions & 6 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ fn cargo_compile_with_filename() {
p.cargo("build --bin bin.rs")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no bin target named `bin.rs`.
[ERROR] no bin target named `bin.rs` in default-run packages.
Available bin targets:
a

Expand All @@ -1348,7 +1348,7 @@ Available bin targets:
p.cargo("build --bin a.rs")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no bin target named `a.rs`
[ERROR] no bin target named `a.rs` in default-run packages

[HELP] a target with a similar name exists: `a`

Expand All @@ -1358,7 +1358,7 @@ Available bin targets:
p.cargo("build --example example.rs")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no example target named `example.rs`.
[ERROR] no example target named `example.rs` in default-run packages.
Available example targets:
a

Expand All @@ -1369,7 +1369,7 @@ Available example targets:
p.cargo("build --example a.rs")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no example target named `a.rs`
[ERROR] no example target named `a.rs` in default-run packages

[HELP] a target with a similar name exists: `a`

Expand Down Expand Up @@ -5906,7 +5906,7 @@ fn target_filters_workspace() {
ws.cargo("build -v --example ex")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no example target named `ex`
[ERROR] no example target named `ex` in default-run packages

[HELP] a target with a similar name exists: `ex1`

Expand All @@ -5916,7 +5916,7 @@ fn target_filters_workspace() {
ws.cargo("build -v --example 'ex??'")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no example target matches pattern `ex??`
[ERROR] no example target matches pattern `ex??` in default-run packages

[HELP] a target with a similar name exists: `ex1`

Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ fn install_relative_path_outside_current_ws() {
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] "--bin" takes one argument.
Available binaries:
Available binaries in `foo` package:
foo


Expand Down
Loading
Loading