Skip to content

Commit 45851ef

Browse files
committed
fix(alias): Aliases without subcommands should not panic
1 parent b9d913e commit 45851ef

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/bin/cargo/cli.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,13 @@ For more information, see issue #12207 <https://github.com/rust-lang/cargo/issue
356356
let global_args = GlobalArgs::new(sub_args);
357357
let new_args = cli(gctx).no_binary_name(true).try_get_matches_from(alias)?;
358358

359-
let new_cmd = new_args.subcommand_name().expect("subcommand is required");
359+
let Some(new_cmd) = new_args.subcommand_name() else {
360+
return Err(anyhow!(
361+
"subcommand is required, add a subcommand to the command alias `alias.{cmd}`"
362+
)
363+
.into());
364+
};
365+
360366
already_expanded.push(cmd.to_string());
361367
if already_expanded.contains(&new_cmd.to_string()) {
362368
// Crash if the aliases are corecursive / unresolvable

tests/testsuite/cargo_alias_config.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,3 +463,27 @@ fn empty_alias() {
463463
)
464464
.run();
465465
}
466+
467+
#[cargo_test]
468+
fn alias_no_subcommand() {
469+
let p = project()
470+
.file("Cargo.toml", &basic_bin_manifest("foo"))
471+
.file("src/main.rs", "fn main() {}")
472+
.file(
473+
".cargo/config.toml",
474+
r#"
475+
[alias]
476+
a = "--locked"
477+
"#,
478+
)
479+
.build();
480+
481+
p.cargo("a")
482+
.with_status(101)
483+
.with_stderr(
484+
"\
485+
[ERROR] subcommand is required, add a subcommand to the command alias `alias.a`
486+
",
487+
)
488+
.run();
489+
}

0 commit comments

Comments
 (0)