Skip to content

Commit ff3e880

Browse files
committed
Added aliases to subcommand typo suggestions.
Fixes #7278. Also adds tests for alias suggestions.
1 parent a429e8c commit ff3e880

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/bin/cargo/main.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ fn list_commands(config: &Config) -> BTreeSet<CommandInfo> {
113113
commands
114114
}
115115

116+
/// List all runnable aliases
117+
fn list_aliases(config: &Config) -> Vec<String> {
118+
match config.get_table("alias") {
119+
Ok(table) => match table {
120+
Some(aliases) => aliases.val.keys().map(|a| a.to_string()).collect(),
121+
None => Vec::new(),
122+
},
123+
Err(_) => Vec::new(),
124+
}
125+
}
126+
116127
fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> CliResult {
117128
let command_exe = format!("cargo-{}{}", cmd, env::consts::EXE_SUFFIX);
118129
let path = search_directories(config)
@@ -122,8 +133,13 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> Cli
122133
let command = match path {
123134
Some(command) => command,
124135
None => {
125-
let cmds = list_commands(config);
126-
let did_you_mean = closest_msg(cmd, cmds.iter(), |c| c.name());
136+
let commands: Vec<String> = list_commands(config)
137+
.iter()
138+
.map(|c| c.name().to_string())
139+
.collect();
140+
let aliases = list_aliases(config);
141+
let suggestions = commands.iter().chain(aliases.iter());
142+
let did_you_mean = closest_msg(cmd, suggestions, |c| c);
127143
let err = failure::format_err!("no such subcommand: `{}`{}", cmd, did_you_mean);
128144
return Err(CliError::new(err, 101));
129145
}

tests/testsuite/cargo_command.rs

Lines changed: 43 additions & 0 deletions
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)