Skip to content

Commit

Permalink
Auto merge of #13819 - heisen-li:alias, r=weihanglo
Browse files Browse the repository at this point in the history
fix(alias): Aliases without subcommands should not panic

### What does this PR try to resolve?

Fixes #13814
  • Loading branch information
bors committed Apr 30, 2024
2 parents e0b9fae + 45851ef commit c10b6d3
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 c10b6d3

Please sign in to comment.