Auto merge of #13613 - weihanglo:empty-alias, r=epage

fix(alias): dont panic when resolving an empty alias
This commit is contained in:
bors 2024-03-21 12:56:31 +00:00
commit d2fbe57b81
2 changed files with 41 additions and 0 deletions

View File

@ -148,6 +148,13 @@ fn aliased_command(gctx: &GlobalContext, command: &str) -> CargoResult<Option<Ve
let result = user_alias.or_else(|| {
builtin_aliases_execs(command).map(|command_str| vec![command_str.1.to_string()])
});
if result
.as_ref()
.map(|alias| alias.is_empty())
.unwrap_or_default()
{
anyhow::bail!("subcommand is required, but `{alias_name}` is empty");
}
Ok(result)
}

View File

@ -429,3 +429,37 @@ To pass the arguments to the subcommand, remove `--`
)
.run();
}
#[cargo_test]
fn empty_alias() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/main.rs", "fn main() {}")
.file(
".cargo/config.toml",
r#"
[alias]
string = ""
array = []
"#,
)
.build();
p.cargo("string")
.with_status(101)
.with_stderr(
"\
[ERROR] subcommand is required, but `alias.string` is empty
",
)
.run();
p.cargo("array")
.with_status(101)
.with_stderr(
"\
[ERROR] subcommand is required, but `alias.array` is empty
",
)
.run();
}