Skip to content

Commit

Permalink
Auto merge of #13613 - weihanglo:empty-alias, r=epage
Browse files Browse the repository at this point in the history
fix(alias): dont panic when resolving an empty alias
  • Loading branch information
bors committed Mar 21, 2024
2 parents f0ae765 + 46214f3 commit d2fbe57
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ fn aliased_command(gctx: &GlobalContext, command: &str) -> CargoResult<Option<Ve
let result = user_alias.or_else(|| {
builtin_aliases_execs(command).map(|command_str| vec![command_str.1.to_string()])
});
if result
.as_ref()
.map(|alias| alias.is_empty())
.unwrap_or_default()
{
anyhow::bail!("subcommand is required, but `{alias_name}` is empty");
}
Ok(result)
}

Expand Down
34 changes: 34 additions & 0 deletions tests/testsuite/cargo_alias_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,37 @@ To pass the arguments to the subcommand, remove `--`
)
.run();
}

#[cargo_test]
fn empty_alias() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/main.rs", "fn main() {}")
.file(
".cargo/config.toml",
r#"
[alias]
string = ""
array = []
"#,
)
.build();

p.cargo("string")
.with_status(101)
.with_stderr(
"\
[ERROR] subcommand is required, but `alias.string` is empty
",
)
.run();

p.cargo("array")
.with_status(101)
.with_stderr(
"\
[ERROR] subcommand is required, but `alias.array` is empty
",
)
.run();
}

0 comments on commit d2fbe57

Please sign in to comment.