Skip to content

Commit 90a660d

Browse files
committed
fix: Improve the quality of the panic message
1 parent 656f70f commit 90a660d

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

src/cargo.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,33 @@ fn cargo_bin_str(name: &str) -> path::PathBuf {
233233
env::var_os(env_var)
234234
.map(|p| p.into())
235235
.or_else(|| legacy_cargo_bin(name))
236-
.unwrap_or_else(|| missing_cargo_bin())
236+
.unwrap_or_else(|| missing_cargo_bin(name))
237237
}
238238

239239
const CARGO_BIN_EXE_: &str = "CARGO_BIN_EXE_";
240240

241-
fn missing_cargo_bin() -> ! {
242-
panic!("`CARGO_BIN_EXE_*` is not set")
241+
fn missing_cargo_bin(name: &str) -> ! {
242+
let possible_names: Vec<_> = env::vars_os()
243+
.filter_map(|(k, _)| k.into_string().ok())
244+
.filter_map(|k| k.strip_prefix(CARGO_BIN_EXE_).map(|s| s.to_owned()))
245+
.collect();
246+
if possible_names.is_empty() {
247+
panic!("`CARGO_BIN_EXE_{name}` is unset
248+
help: if this is running within a unit test, move it to an integration test to gain access to `CARGO_BIN_EXE_{name}`")
249+
} else {
250+
let mut names = String::new();
251+
for (i, name) in possible_names.iter().enumerate() {
252+
use std::fmt::Write as _;
253+
if i != 0 {
254+
let _ = write!(&mut names, ", ");
255+
}
256+
let _ = write!(&mut names, "\"{name}\"");
257+
}
258+
panic!(
259+
"`CARGO_BIN_EXE_{name}` is unset
260+
help: available binary names are {names}"
261+
)
262+
}
243263
}
244264

245265
fn legacy_cargo_bin(name: &str) -> Option<path::PathBuf> {

0 commit comments

Comments
 (0)