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
48 changes: 35 additions & 13 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
//! repeats until the queue is empty.

use std::collections::{BTreeSet, HashMap, HashSet};
use std::fmt::Write;
use std::hash::{Hash, Hasher};
use std::sync::Arc;

Expand Down Expand Up @@ -1526,19 +1527,40 @@ fn find_named_targets<'a>(
};

if proposals.is_empty() {
let targets = packages.iter().flat_map(|pkg| {
pkg.targets()
.iter()
.filter(|target| is_expected_kind(target))
});
let suggestion = closest_msg(target_name, targets, |t| t.name());
anyhow::bail!(
"no {} target {} `{}`{}",
target_desc,
if is_glob { "matches pattern" } else { "named" },
target_name,
suggestion
);
let targets = packages
.iter()
.flat_map(|pkg| {
pkg.targets()
.iter()
.filter(|target| is_expected_kind(target))
})
.collect::<Vec<_>>();
let suggestion = closest_msg(target_name, targets.iter(), |t| t.name());
if !suggestion.is_empty() {
anyhow::bail!(
"no {} target {} `{}`{}",
target_desc,
if is_glob { "matches pattern" } else { "named" },
target_name,
suggestion
);
} else {
let mut msg = String::new();
writeln!(
msg,
"no {} target {} `{}`.",
target_desc,
if is_glob { "matches pattern" } else { "named" },
target_name,
)?;
if !targets.is_empty() {
writeln!(msg, "Available {} targets:", target_desc)?;
for target in targets {
writeln!(msg, " {}", target.name())?;
}
}
anyhow::bail!(msg);
}
}
Ok(proposals)
}
Expand Down
18 changes: 16 additions & 2 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,14 @@ fn cargo_compile_with_filename() {

p.cargo("build --bin bin.rs")
.with_status(101)
.with_stderr("[ERROR] no bin target named `bin.rs`")
.with_stderr(
"\
[ERROR] no bin target named `bin.rs`.
Available bin targets:
a

",
)
.run();

p.cargo("build --bin a.rs")
Expand All @@ -1056,7 +1063,14 @@ fn cargo_compile_with_filename() {

p.cargo("build --example example.rs")
.with_status(101)
.with_stderr("[ERROR] no example target named `example.rs`")
.with_stderr(
"\
[ERROR] no example target named `example.rs`.
Available example targets:
a

",
)
.run();

p.cargo("build --example a.rs")
Expand Down
32 changes: 28 additions & 4 deletions tests/testsuite/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,10 @@ 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`.
Available example targets:
do_magic

",
)
.run();
Expand All @@ -528,7 +531,14 @@ fn run_example_autodiscover_2015_with_autoexamples_disabled() {
let p = autodiscover_examples_project("2015", Some(false));
p.cargo("run --example a")
.with_status(101)
.with_stderr("error: no example target named `a`\n")
.with_stderr(
"\
error: no example target named `a`.
Available example targets:
do_magic

",
)
.run();
}

Expand Down Expand Up @@ -600,7 +610,14 @@ fn run_with_filename() {

p.cargo("run --bin bin.rs")
.with_status(101)
.with_stderr("[ERROR] no bin target named `bin.rs`")
.with_stderr(
"\
[ERROR] no bin target named `bin.rs`.
Available bin targets:
a

",
)
.run();

p.cargo("run --bin a.rs")
Expand All @@ -615,7 +632,14 @@ fn run_with_filename() {

p.cargo("run --example example.rs")
.with_status(101)
.with_stderr("[ERROR] no example target named `example.rs`")
.with_stderr(
"\
[ERROR] no example target named `example.rs`.
Available example targets:
a

",
)
.run();

p.cargo("run --example a.rs")
Expand Down
14 changes: 12 additions & 2 deletions tests/testsuite/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2122,11 +2122,21 @@ fn bad_example() {

p.cargo("run --example foo")
.with_status(101)
.with_stderr("[ERROR] no example target named `foo`")
.with_stderr(
"\
[ERROR] no example target named `foo`.

",
)
.run();
p.cargo("run --bin foo")
.with_status(101)
.with_stderr("[ERROR] no bin target named `foo`")
.with_stderr(
"\
[ERROR] no bin target named `foo`.

",
)
.run();
}

Expand Down