fix(alias): Aliases without subcommands should not panic

This commit is contained in:
heisen-li 2024-04-28 17:03:01 +08:00
parent b9d913e532
commit 45851ef31e
2 changed files with 31 additions and 1 deletions

View File

@ -356,7 +356,13 @@ For more information, see issue #12207 <https://github.com/rust-lang/cargo/issue
let global_args = GlobalArgs::new(sub_args); let global_args = GlobalArgs::new(sub_args);
let new_args = cli(gctx).no_binary_name(true).try_get_matches_from(alias)?; let new_args = cli(gctx).no_binary_name(true).try_get_matches_from(alias)?;
let new_cmd = new_args.subcommand_name().expect("subcommand is required"); let Some(new_cmd) = new_args.subcommand_name() else {
return Err(anyhow!(
"subcommand is required, add a subcommand to the command alias `alias.{cmd}`"
)
.into());
};
already_expanded.push(cmd.to_string()); already_expanded.push(cmd.to_string());
if already_expanded.contains(&new_cmd.to_string()) { if already_expanded.contains(&new_cmd.to_string()) {
// Crash if the aliases are corecursive / unresolvable // Crash if the aliases are corecursive / unresolvable

View File

@ -463,3 +463,27 @@ fn empty_alias() {
) )
.run(); .run();
} }
#[cargo_test]
fn alias_no_subcommand() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/main.rs", "fn main() {}")
.file(
".cargo/config.toml",
r#"
[alias]
a = "--locked"
"#,
)
.build();
p.cargo("a")
.with_status(101)
.with_stderr(
"\
[ERROR] subcommand is required, add a subcommand to the command alias `alias.a`
",
)
.run();
}