Skip to content

Commit 127d2b4

Browse files
committed
Auto merge of #7486 - zachlute:suggest-alias2, r=alexcrichton
Added aliases to subcommand typo suggestions. Fixes #7278. Also adds tests for alias suggestions.
2 parents 2130016 + 0f157f5 commit 127d2b4

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/bin/cargo/main.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![warn(clippy::needless_borrow)]
55
#![warn(clippy::redundant_clone)]
66

7-
use std::collections::BTreeSet;
7+
use std::collections::{BTreeMap, BTreeSet};
88
use std::env;
99
use std::fs;
1010
use std::path::{Path, PathBuf};
@@ -111,6 +111,14 @@ fn list_commands(config: &Config) -> BTreeSet<CommandInfo> {
111111
commands
112112
}
113113

114+
/// List all runnable aliases
115+
fn list_aliases(config: &Config) -> Vec<String> {
116+
match config.get::<BTreeMap<String, String>>("alias") {
117+
Ok(aliases) => aliases.keys().map(|a| a.to_string()).collect(),
118+
Err(_) => Vec::new(),
119+
}
120+
}
121+
114122
fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> CliResult {
115123
let command_exe = format!("cargo-{}{}", cmd, env::consts::EXE_SUFFIX);
116124
let path = search_directories(config)
@@ -120,8 +128,13 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> Cli
120128
let command = match path {
121129
Some(command) => command,
122130
None => {
123-
let cmds = list_commands(config);
124-
let did_you_mean = closest_msg(cmd, cmds.iter(), |c| c.name());
131+
let commands: Vec<String> = list_commands(config)
132+
.iter()
133+
.map(|c| c.name().to_string())
134+
.collect();
135+
let aliases = list_aliases(config);
136+
let suggestions = commands.iter().chain(aliases.iter());
137+
let did_you_mean = closest_msg(cmd, suggestions, |c| c);
125138
let err = failure::format_err!("no such subcommand: `{}`{}", cmd, did_you_mean);
126139
return Err(CliError::new(err, 101));
127140
}

tests/testsuite/cargo_command.rs

+43
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,49 @@ error: no such subcommand: `biuld`
168168
.run();
169169
}
170170

171+
#[cargo_test]
172+
fn find_closest_alias() {
173+
let root = paths::root();
174+
let my_home = root.join("my_home");
175+
fs::create_dir(&my_home).unwrap();
176+
File::create(&my_home.join("config"))
177+
.unwrap()
178+
.write_all(
179+
br#"
180+
[alias]
181+
myalias = "build"
182+
"#,
183+
)
184+
.unwrap();
185+
186+
cargo_process("myalais")
187+
.env("CARGO_HOME", &my_home)
188+
.with_status(101)
189+
.with_stderr_contains(
190+
"\
191+
error: no such subcommand: `myalais`
192+
193+
<tab>Did you mean `myalias`?
194+
",
195+
)
196+
.run();
197+
198+
// But, if no alias is defined, it must not suggest one!
199+
cargo_process("myalais")
200+
.with_status(101)
201+
.with_stderr_contains(
202+
"\
203+
error: no such subcommand: `myalais`
204+
",
205+
)
206+
.with_stderr_does_not_contain(
207+
"\
208+
<tab>Did you mean `myalias`?
209+
",
210+
)
211+
.run();
212+
}
213+
171214
// If a subcommand is more than an edit distance of 3 away, we don't make a suggestion.
172215
#[cargo_test]
173216
fn find_closest_dont_correct_nonsense() {

0 commit comments

Comments
 (0)