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
19 changes: 16 additions & 3 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![warn(clippy::needless_borrow)]
#![warn(clippy::redundant_clone)]

use std::collections::BTreeSet;
use std::collections::{BTreeMap, BTreeSet};
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -113,6 +113,14 @@ fn list_commands(config: &Config) -> BTreeSet<CommandInfo> {
commands
}

/// List all runnable aliases
fn list_aliases(config: &Config) -> Vec<String> {
match config.get::<BTreeMap<String, String>>("alias") {
Ok(aliases) => aliases.keys().map(|a| a.to_string()).collect(),
Err(_) => Vec::new(),
}
}

fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> CliResult {
let command_exe = format!("cargo-{}{}", cmd, env::consts::EXE_SUFFIX);
let path = search_directories(config)
Expand All @@ -122,8 +130,13 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> Cli
let command = match path {
Some(command) => command,
None => {
let cmds = list_commands(config);
let did_you_mean = closest_msg(cmd, cmds.iter(), |c| c.name());
let commands: Vec<String> = list_commands(config)
.iter()
.map(|c| c.name().to_string())
.collect();
let aliases = list_aliases(config);
let suggestions = commands.iter().chain(aliases.iter());
let did_you_mean = closest_msg(cmd, suggestions, |c| c);
let err = failure::format_err!("no such subcommand: `{}`{}", cmd, did_you_mean);
return Err(CliError::new(err, 101));
}
Expand Down
43 changes: 43 additions & 0 deletions tests/testsuite/cargo_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,49 @@ error: no such subcommand: `biuld`
.run();
}

#[cargo_test]
fn find_closest_alias() {
let root = paths::root();
let my_home = root.join("my_home");
fs::create_dir(&my_home).unwrap();
File::create(&my_home.join("config"))
.unwrap()
.write_all(
br#"
[alias]
myalias = "build"
"#,
)
.unwrap();

cargo_process("myalais")
.env("CARGO_HOME", &my_home)
.with_status(101)
.with_stderr_contains(
"\
error: no such subcommand: `myalais`

<tab>Did you mean `myalias`?
",
)
.run();

// But, if no alias is defined, it must not suggest one!
cargo_process("myalais")
.with_status(101)
.with_stderr_contains(
"\
error: no such subcommand: `myalais`
",
)
.with_stderr_does_not_contain(
"\
<tab>Did you mean `myalias`?
",
)
.run();
}

// If a subcommand is more than an edit distance of 3 away, we don't make a suggestion.
#[cargo_test]
fn find_closest_dont_correct_nonsense() {
Expand Down