Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/cargo/ops/cargo_compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ pub fn create_bcx<'a, 'gctx>(
let generator = UnitGenerator {
ws,
packages: &to_builds,
spec,
target_data: &target_data,
filter,
requested_kinds: &build_config.requested_kinds,
Expand Down
102 changes: 86 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 @@ -47,6 +48,7 @@ struct Proposal<'a> {
pub(super) struct UnitGenerator<'a, 'gctx> {
pub ws: &'a Workspace<'gctx>,
pub packages: &'a [&'a Package],
pub spec: &'a Packages,
pub target_data: &'a RustcTargetData<'gctx>,
pub filter: &'a CompileFilter,
pub requested_kinds: &'a [CompileKind],
Expand Down Expand Up @@ -247,15 +249,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 +269,103 @@ 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, prefix: &str| {
let mut available_msg = Vec::new();
for (package, targets) in targets_elsewhere {
if !targets.is_empty() {
available_msg.push(format!(
"help: Available {target_desc} in `{package}` package:"
));
for target in targets {
available_msg.push(format!(" {target}"));
}
}
}
if !available_msg.is_empty() {
write!(msg, "{prefix}{}", available_msg.join("\n"))?;
}
CargoResult::Ok(())
};

let unmatched_packages = || match self.spec {
Packages::Default | Packages::OptOut(_) | Packages::All(_) => {
"default-run packages".to_owned()
}
Packages::Packages(packages) => {
let first = packages
.first()
.expect("The number of packages must be at least 1");
if packages.len() == 1 {
format!("`{}` package", first)
} else {
format!("`{}`, ... packages", first)
}
}
};

let mut msg = String::new();
if !suggestion.is_empty() {
anyhow::bail!(
"no {} target {} `{}`{}",
write!(
msg,
"no {} target {} `{}` in {}{}",
target_desc,
if is_glob { "matches pattern" } else { "named" },
target_name,
suggestion
);
unmatched_packages(),
suggestion,
)?;
append_targets_elsewhere(&mut msg, "\n")?;
} else {
let mut msg = String::new();
writeln!(
msg,
"no {} target {} `{}`.",
"no {} target {} `{}` in {}.",
target_desc,
if is_glob { "matches pattern" } else { "named" },
target_name,
unmatched_packages()
)?;
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
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
12 changes: 6 additions & 6 deletions tests/testsuite/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ automatically infer them to be a target, such as in subfolders.

For more information on this warning you can consult
https://github.com/rust-lang/cargo/issues/5330
[ERROR] no example target named `a`.
[ERROR] no example target named `a` in default-run packages.
Available example targets:
do_magic

Expand Down Expand Up @@ -655,7 +655,7 @@ fn run_example_autodiscover_2015_with_autoexamples_disabled() {
p.cargo("run --example a")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no example target named `a`.
[ERROR] no example target named `a` in default-run packages.
Available example targets:
do_magic

Expand Down Expand Up @@ -743,7 +743,7 @@ fn run_with_filename() {
p.cargo("run --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 @@ -754,7 +754,7 @@ Available bin targets:
p.cargo("run --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 @@ -764,7 +764,7 @@ Available bin targets:
p.cargo("run --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 @@ -775,7 +775,7 @@ Available example targets:
p.cargo("run --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
4 changes: 2 additions & 2 deletions tests/testsuite/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2447,15 +2447,15 @@ fn bad_example() {
p.cargo("run --example foo")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no example target named `foo`.
[ERROR] no example target named `foo` in default-run packages.


"#]])
.run();
p.cargo("run --bin foo")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no bin target named `foo`.
[ERROR] no bin target named `foo` in default-run packages.


"#]])
Expand Down
Loading