Skip to content

Commit d2fbe57

Browse files
committed
Auto merge of #13613 - weihanglo:empty-alias, r=epage
fix(alias): dont panic when resolving an empty alias
2 parents f0ae765 + 46214f3 commit d2fbe57

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

Diff for: src/bin/cargo/main.rs

+7
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ fn aliased_command(gctx: &GlobalContext, command: &str) -> CargoResult<Option<Ve
148148
let result = user_alias.or_else(|| {
149149
builtin_aliases_execs(command).map(|command_str| vec![command_str.1.to_string()])
150150
});
151+
if result
152+
.as_ref()
153+
.map(|alias| alias.is_empty())
154+
.unwrap_or_default()
155+
{
156+
anyhow::bail!("subcommand is required, but `{alias_name}` is empty");
157+
}
151158
Ok(result)
152159
}
153160

Diff for: tests/testsuite/cargo_alias_config.rs

+34
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,37 @@ To pass the arguments to the subcommand, remove `--`
429429
)
430430
.run();
431431
}
432+
433+
#[cargo_test]
434+
fn empty_alias() {
435+
let p = project()
436+
.file("Cargo.toml", &basic_bin_manifest("foo"))
437+
.file("src/main.rs", "fn main() {}")
438+
.file(
439+
".cargo/config.toml",
440+
r#"
441+
[alias]
442+
string = ""
443+
array = []
444+
"#,
445+
)
446+
.build();
447+
448+
p.cargo("string")
449+
.with_status(101)
450+
.with_stderr(
451+
"\
452+
[ERROR] subcommand is required, but `alias.string` is empty
453+
",
454+
)
455+
.run();
456+
457+
p.cargo("array")
458+
.with_status(101)
459+
.with_stderr(
460+
"\
461+
[ERROR] subcommand is required, but `alias.array` is empty
462+
",
463+
)
464+
.run();
465+
}

0 commit comments

Comments
 (0)