Skip to content

Commit

Permalink
Favor error from alias resolution over trailing args
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Sep 21, 2022
1 parent 6b6fccc commit 903783a
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,26 +231,25 @@ fn expand_aliases(
) -> Result<(ArgMatches, GlobalArgs), CliError> {
if let Some((cmd, args)) = args.subcommand() {
let exec = commands::builtin_exec(cmd);
let aliased_cmd = super::aliased_command(config, cmd).or_else(|e| {
// HACK: `cargo version` must always work even with malformed configs.
// Here we treat the error as the aliased command not found,
// so it falls back to call builtin `cargo version`
if cmd == "version" {
Ok(None)
} else {
Err(e)
}
})?;
let aliased_cmd = super::aliased_command(config, cmd);

match (exec, aliased_cmd) {
(Some(_), Some(_)) => {
(_, Err(e)) => {
// HACK: `cargo version` must always work even with malformed configs.
// so when cmd is exactly `version`, it ignores the error
// and falls back to call builtin `cargo version` then.
if cmd != "version" {
return Err(e.into());
}
}
(Some(_), Ok(Some(_))) => {
// User alias conflicts with a built-in subcommand
config.shell().warn(format!(
"user-defined alias `{}` is ignored, because it is shadowed by a built-in command",
cmd,
))?;
}
(Some(_), None) => {
(Some(_), Ok(None)) => {
// Command is built-in and is not conflicting with alias, but contains ignored values.
if let Some(mut values) = args.get_many::<String>("") {
return Err(anyhow::format_err!(
Expand All @@ -264,8 +263,8 @@ To pass the arguments to the subcommand, remove `--`",
.into());
}
}
(None, None) => {}
(_, Some(mut alias)) => {
(None, Ok(None)) => {}
(None, Ok(Some(mut alias))) => {
// Check if this alias is shadowing an external subcommand
// (binary of the form `cargo-<subcommand>`)
// Currently this is only a warning, but after a transition period this will become
Expand Down

0 comments on commit 903783a

Please sign in to comment.