Skip to content

fix(alias): Aliases without subcommands should not panic #13819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}