Skip to content

Commit 903783a

Browse files
committed
Favor error from alias resolution over trailing args
1 parent 6b6fccc commit 903783a

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

src/bin/cargo/cli.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -231,26 +231,25 @@ fn expand_aliases(
231231
) -> Result<(ArgMatches, GlobalArgs), CliError> {
232232
if let Some((cmd, args)) = args.subcommand() {
233233
let exec = commands::builtin_exec(cmd);
234-
let aliased_cmd = super::aliased_command(config, cmd).or_else(|e| {
235-
// HACK: `cargo version` must always work even with malformed configs.
236-
// Here we treat the error as the aliased command not found,
237-
// so it falls back to call builtin `cargo version`
238-
if cmd == "version" {
239-
Ok(None)
240-
} else {
241-
Err(e)
242-
}
243-
})?;
234+
let aliased_cmd = super::aliased_command(config, cmd);
244235

245236
match (exec, aliased_cmd) {
246-
(Some(_), Some(_)) => {
237+
(_, Err(e)) => {
238+
// HACK: `cargo version` must always work even with malformed configs.
239+
// so when cmd is exactly `version`, it ignores the error
240+
// and falls back to call builtin `cargo version` then.
241+
if cmd != "version" {
242+
return Err(e.into());
243+
}
244+
}
245+
(Some(_), Ok(Some(_))) => {
247246
// User alias conflicts with a built-in subcommand
248247
config.shell().warn(format!(
249248
"user-defined alias `{}` is ignored, because it is shadowed by a built-in command",
250249
cmd,
251250
))?;
252251
}
253-
(Some(_), None) => {
252+
(Some(_), Ok(None)) => {
254253
// Command is built-in and is not conflicting with alias, but contains ignored values.
255254
if let Some(mut values) = args.get_many::<String>("") {
256255
return Err(anyhow::format_err!(
@@ -264,8 +263,8 @@ To pass the arguments to the subcommand, remove `--`",
264263
.into());
265264
}
266265
}
267-
(None, None) => {}
268-
(_, Some(mut alias)) => {
266+
(None, Ok(None)) => {}
267+
(None, Ok(Some(mut alias))) => {
269268
// Check if this alias is shadowing an external subcommand
270269
// (binary of the form `cargo-<subcommand>`)
271270
// Currently this is only a warning, but after a transition period this will become

0 commit comments

Comments
 (0)