Skip to content

Commit

Permalink
fix(alias): Aliases without subcommands should not panic
Browse files Browse the repository at this point in the history
  • Loading branch information
heisen-li committed Apr 30, 2024
1 parent b9d913e commit 45851ef
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/bin/cargo/cli.rs
Expand Up @@ -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 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());
if already_expanded.contains(&new_cmd.to_string()) {
// Crash if the aliases are corecursive / unresolvable
Expand Down
24 changes: 24 additions & 0 deletions tests/testsuite/cargo_alias_config.rs
Expand Up @@ -463,3 +463,27 @@ fn empty_alias() {
)
.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();
}

0 comments on commit 45851ef

Please sign in to comment.