From 02ec8d48e53fef1c14ff271203ba5c661b6b2a01 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 6 May 2025 11:05:10 -0500 Subject: [PATCH 1/3] test(rustc): Show the bug --- tests/testsuite/rustc.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/testsuite/rustc.rs b/tests/testsuite/rustc.rs index 6e9b5b228..8c17fd2bc 100644 --- a/tests/testsuite/rustc.rs +++ b/tests/testsuite/rustc.rs @@ -533,6 +533,29 @@ fn fail_with_multiple_packages() { .run(); } +#[cargo_test] +fn fail_with_bad_bin_no_package() { + let p = project() + .file( + "src/main.rs", + r#" + fn main() { println!("hello a.rs"); } + "#, + ) + .build(); + + p.cargo("rustc --bin main") + .with_status(101) + .with_stderr_data(str![[r#" + +thread 'main' panicked [..]: +The number of packages must be at least 1 +... + +"#]]) + .run(); +} + #[cargo_test] fn fail_with_glob() { let p = project() From 981ea84bdc9fb095e818b65c70bca27d7504c82b Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 6 May 2025 11:10:12 -0500 Subject: [PATCH 2/3] refactor(compile): Make unmatched_packages optional --- src/cargo/ops/cargo_compile/unit_generator.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cargo/ops/cargo_compile/unit_generator.rs b/src/cargo/ops/cargo_compile/unit_generator.rs index 588581457..c11406bc4 100644 --- a/src/cargo/ops/cargo_compile/unit_generator.rs +++ b/src/cargo/ops/cargo_compile/unit_generator.rs @@ -294,16 +294,16 @@ impl<'a> UnitGenerator<'a, '_> { let unmatched_packages = match self.spec { Packages::Default | Packages::OptOut(_) | Packages::All(_) => { - "default-run packages".to_owned() + " in default-run packages".to_owned() } Packages::Packages(packages) => { let first = packages .first() .expect("The number of packages must be at least 1"); if packages.len() == 1 { - format!("`{}` package", first) + format!(" in `{}` package", first) } else { - format!("`{}`, ... packages", first) + format!(" in `{}`, ... packages", first) } } }; @@ -313,7 +313,7 @@ impl<'a> UnitGenerator<'a, '_> { let mut msg = String::new(); write!( msg, - "no {target_desc} target {named} `{target_name}` in {unmatched_packages}{suggestion}", + "no {target_desc} target {named} `{target_name}`{unmatched_packages}{suggestion}", )?; if !targets_elsewhere.is_empty() { append_targets_elsewhere(&mut msg)?; From f83e11acfed3f71e3c9657e3dd3f2f8196e173b2 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 6 May 2025 11:13:30 -0500 Subject: [PATCH 3/3] fix(rustc): Don't panic on unknown bins This takes the most surgical, direct route to addressing the problem. Alternatively, we could look into why `cargo rustc` and `cargo check` are different. Fixes #15493 --- src/cargo/ops/cargo_compile/unit_generator.rs | 15 +++++---------- tests/testsuite/rustc.rs | 6 +++--- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/cargo/ops/cargo_compile/unit_generator.rs b/src/cargo/ops/cargo_compile/unit_generator.rs index c11406bc4..3c984f476 100644 --- a/src/cargo/ops/cargo_compile/unit_generator.rs +++ b/src/cargo/ops/cargo_compile/unit_generator.rs @@ -296,16 +296,11 @@ impl<'a> UnitGenerator<'a, '_> { Packages::Default | Packages::OptOut(_) | Packages::All(_) => { " in default-run packages".to_owned() } - Packages::Packages(packages) => { - let first = packages - .first() - .expect("The number of packages must be at least 1"); - if packages.len() == 1 { - format!(" in `{}` package", first) - } else { - format!(" in `{}`, ... packages", first) - } - } + Packages::Packages(packages) => match packages.len() { + 0 => String::new(), + 1 => format!(" in `{}` package", packages[0]), + _ => format!(" in `{}`, ... packages", packages[0]), + }, }; let named = if is_glob { "matches pattern" } else { "named" }; diff --git a/tests/testsuite/rustc.rs b/tests/testsuite/rustc.rs index 8c17fd2bc..da32f20ac 100644 --- a/tests/testsuite/rustc.rs +++ b/tests/testsuite/rustc.rs @@ -547,9 +547,9 @@ fn fail_with_bad_bin_no_package() { p.cargo("rustc --bin main") .with_status(101) .with_stderr_data(str![[r#" - -thread 'main' panicked [..]: -The number of packages must be at least 1 +[ERROR] no bin target named `main` +[HELP] available bin targets: + foo ... "#]])