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

Spot the crate typo easily #9665

Merged
merged 3 commits into from
Jul 14, 2021
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
23 changes: 13 additions & 10 deletions src/cargo/core/resolver/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,15 @@ pub(super) fn activation_error(
.filter(|&(d, _)| d < 4)
.collect();
candidates.sort_by_key(|o| o.0);
let mut msg = format!(
"no matching package named `{}` found\n\
location searched: {}\n",
dep.package_name(),
dep.source_id()
);
if !candidates.is_empty() {
let mut msg: String;
if candidates.is_empty() {
msg = format!("no matching package named `{}` found\n", dep.package_name());
} else {
msg = format!(
"no matching package found\nsearched package name: `{}`\n",
dep.package_name()
);

// If dependency package name is equal to the name of the candidate here
// it may be a prerelease package which hasn't been specified correctly
if dep.package_name() == candidates[0].1.name()
Expand All @@ -312,8 +314,9 @@ pub(super) fn activation_error(
if candidates.len() > 3 {
names.push("...");
}

msg.push_str("perhaps you meant: ");
// Vertically align first suggestion with missing crate name
// so a typo jumps out at you.
msg.push_str("perhaps you meant: ");
msg.push_str(&names.iter().enumerate().fold(
String::default(),
|acc, (i, el)| match i {
Expand All @@ -323,9 +326,9 @@ pub(super) fn activation_error(
},
));
}

msg.push('\n');
}
msg.push_str(&format!("location searched: {}\n", dep.source_id()));
msg.push_str("required by ");
msg.push_str(&describe_path(
&cx.parents.path_to_bottom(&parent.package_id()),
Expand Down
5 changes: 3 additions & 2 deletions tests/testsuite/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,10 @@ fn simple_install_fail() {
error: failed to compile `bar v0.1.0`, intermediate artifacts can be found at `[..]`
Caused by:
no matching package named `baz` found
no matching package found
searched package name: `baz`
perhaps you meant: bar or foo
location searched: registry `https://github.com/rust-lang/crates.io-index`
perhaps you meant: bar or foo
required by package `bar v0.1.0`
",
)
Expand Down
5 changes: 3 additions & 2 deletions tests/testsuite/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,9 +964,10 @@ fn invalid_path_dep_in_workspace_with_lockfile() {
.with_status(101)
.with_stderr(
"\
error: no matching package named `bar` found
error: no matching package found
searched package name: `bar`
perhaps you meant: foo
location searched: [..]
perhaps you meant: foo
required by package `foo v0.5.0 ([..])`
",
)
Expand Down
15 changes: 9 additions & 6 deletions tests/testsuite/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ fn wrong_case() {
.with_stderr(
"\
[UPDATING] [..] index
error: no matching package named `Init` found
error: no matching package found
searched package name: `Init`
perhaps you meant: init
location searched: registry [..]
perhaps you meant: init
required by package `foo v0.0.1 ([..])`
",
)
Expand Down Expand Up @@ -190,9 +191,10 @@ fn mis_hyphenated() {
.with_stderr(
"\
[UPDATING] [..] index
error: no matching package named `mis_hyphenated` found
error: no matching package found
searched package name: `mis_hyphenated`
perhaps you meant: mis-hyphenated
location searched: registry [..]
perhaps you meant: mis-hyphenated
required by package `foo v0.0.1 ([..])`
",
)
Expand Down Expand Up @@ -1438,10 +1440,11 @@ fn use_semver_package_incorrectly() {
.with_status(101)
.with_stderr(
"\
error: no matching package named `a` found
location searched: [..]
error: no matching package found
searched package name: `a`
prerelease package needs to be specified explicitly
a = { version = \"0.1.1-alpha.0\" }
location searched: [..]
required by package `b v0.1.0 ([..])`
",
)
Expand Down