Skip to content

Commit 74c051f

Browse files
authored
fix(rustc): Don't panic on unknown bins (#15497)
### What does this PR try to resolve? Fixes #15493 ### How should we test and review this PR? This takes the most surgical, direct route to addressing the problem. Alternatively, we could look into why `cargo rustc` and `cargo check` are different. ### Additional information
2 parents 33d7b0a + f83e11a commit 74c051f

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/cargo/ops/cargo_compile/unit_generator.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -294,26 +294,21 @@ impl<'a> UnitGenerator<'a, '_> {
294294

295295
let unmatched_packages = match self.spec {
296296
Packages::Default | Packages::OptOut(_) | Packages::All(_) => {
297-
"default-run packages".to_owned()
298-
}
299-
Packages::Packages(packages) => {
300-
let first = packages
301-
.first()
302-
.expect("The number of packages must be at least 1");
303-
if packages.len() == 1 {
304-
format!("`{}` package", first)
305-
} else {
306-
format!("`{}`, ... packages", first)
307-
}
297+
" in default-run packages".to_owned()
308298
}
299+
Packages::Packages(packages) => match packages.len() {
300+
0 => String::new(),
301+
1 => format!(" in `{}` package", packages[0]),
302+
_ => format!(" in `{}`, ... packages", packages[0]),
303+
},
309304
};
310305

311306
let named = if is_glob { "matches pattern" } else { "named" };
312307

313308
let mut msg = String::new();
314309
write!(
315310
msg,
316-
"no {target_desc} target {named} `{target_name}` in {unmatched_packages}{suggestion}",
311+
"no {target_desc} target {named} `{target_name}`{unmatched_packages}{suggestion}",
317312
)?;
318313
if !targets_elsewhere.is_empty() {
319314
append_targets_elsewhere(&mut msg)?;

tests/testsuite/rustc.rs

+23
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,29 @@ fn fail_with_multiple_packages() {
533533
.run();
534534
}
535535

536+
#[cargo_test]
537+
fn fail_with_bad_bin_no_package() {
538+
let p = project()
539+
.file(
540+
"src/main.rs",
541+
r#"
542+
fn main() { println!("hello a.rs"); }
543+
"#,
544+
)
545+
.build();
546+
547+
p.cargo("rustc --bin main")
548+
.with_status(101)
549+
.with_stderr_data(str![[r#"
550+
[ERROR] no bin target named `main`
551+
[HELP] available bin targets:
552+
foo
553+
...
554+
555+
"#]])
556+
.run();
557+
}
558+
536559
#[cargo_test]
537560
fn fail_with_glob() {
538561
let p = project()

0 commit comments

Comments
 (0)