Skip to content

Commit

Permalink
only suggest lev_distance < 4
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 committed Jul 16, 2018
1 parent 98e1bdb commit 95b4640
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
9 changes: 4 additions & 5 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,11 @@ fn find_closest(config: &Config, cmd: &str) -> Option<String> {
let cmds = list_commands(config);
// Only consider candidates with a lev_distance of 3 or less so we don't
// suggest out-of-the-blue options.
let mut filtered = cmds.iter()
.map(|&(ref c, _)| (lev_distance(c, cmd), c))
cmds.into_iter()
.map(|(c, _)| (lev_distance(&c, cmd), c))
.filter(|&(d, _)| d < 4)
.collect::<Vec<_>>();
filtered.sort_by(|a, b| a.0.cmp(&b.0));
filtered.get(0).map(|slot| slot.1.clone())
.min_by_key(|a| a.0)
.map(|slot| slot.1)
}

fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> CliResult {
Expand Down
37 changes: 23 additions & 14 deletions src/cargo/core/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ fn activate_deps_loop(
// to amortize the cost of the current time lookup.
ticks += 1;
if let Some(config) = config {
if config.shell().is_err_tty() && !printed && ticks % 1000 == 0
if config.shell().is_err_tty()
&& !printed
&& ticks % 1000 == 0
&& start.elapsed() - deps_time > time_to_print
{
printed = true;
Expand Down Expand Up @@ -858,12 +860,14 @@ fn activation_error(
msg.push_str("\nversions that meet the requirements `");
msg.push_str(&dep.version_req().to_string());
msg.push_str("` are: ");
msg.push_str(&candidates
.iter()
.map(|v| v.summary.version())
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(", "));
msg.push_str(
&candidates
.iter()
.map(|v| v.summary.version())
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(", "),
);

let mut conflicting_activations: Vec<_> = conflicting_activations.iter().collect();
conflicting_activations.sort_unstable();
Expand Down Expand Up @@ -926,7 +930,7 @@ fn activation_error(
// We didn't actually find any candidates, so we need to
// give an error message that nothing was found.
//
// Maybe the user mistyped the ver_req? Like `dep="2"` when `dep=".2"`
// Maybe the user mistyped the ver_req? Like `dep="2"` when `dep="0.2"`
// was meant. So we re-query the registry with `deb="*"` so we can
// list a few versions that were actually found.
let all_req = semver::VersionReq::parse("*").unwrap();
Expand Down Expand Up @@ -981,26 +985,31 @@ fn activation_error(
// was meant. So we try asking the registry for a `fuzzy` search for suggestions.
let mut candidates = Vec::new();
if let Err(e) = registry.query(&new_dep, &mut |s| candidates.push(s.name()), true) {
return e
return e;
};
candidates.sort_unstable();
candidates.dedup();
let mut candidates: Vec<_> = candidates
.iter()
.map(|n| (lev_distance(&*new_dep.name(), &*n), n))
.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.name(),
dep.source_id()
);
if !candidates.is_empty() {
candidates.sort_unstable();
candidates.dedup();
candidates.sort_by_key(|o| lev_distance(&*new_dep.name(), &*o));
let mut names = candidates
.iter()
.take(3)
.map(|c| c.to_string())
.map(|c| c.1.as_str())
.collect::<Vec<_>>();

if candidates.len() > 3 {
names.push("...".into());
names.push("...");
}

msg.push_str("did you mean: ");
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,6 @@ fn cargo_compile_with_dep_name_mismatch() {
execs().with_status(101).with_stderr(&format!(
r#"error: no matching package named `notquitebar` found
location searched: {proj_dir}/bar
did you mean: bar
required by package `foo v0.0.1 ({proj_dir})`
"#,
proj_dir = p.url()
Expand Down

0 comments on commit 95b4640

Please sign in to comment.