@@ -107,6 +107,10 @@ where
107107 /// - `>=1.91,<=1.93`: works with default `build-dir`
108108 /// - `<=1.92`: works
109109 ///
110+ /// # Panic
111+ ///
112+ /// Panicks if no binary is found
113+ ///
110114 /// # Examples
111115 ///
112116 /// ```rust,no_run
@@ -220,6 +224,10 @@ impl fmt::Display for NotFoundError {
220224/// - `>1.94`: works
221225/// - `>=1.91,<=1.93`: works with default `build-dir`
222226/// - `<=1.92`: works
227+ ///
228+ /// # Panic
229+ ///
230+ /// Panicks if no binary is found
223231pub fn cargo_bin < S : AsRef < str > > ( name : S ) -> path:: PathBuf {
224232 cargo_bin_str ( name. as_ref ( ) )
225233}
@@ -261,6 +269,9 @@ help: available binary names are {names}"
261269fn legacy_cargo_bin ( name : & str ) -> Option < path:: PathBuf > {
262270 let target_dir = target_dir ( ) ?;
263271 let bin_path = target_dir. join ( format ! ( "{}{}" , name, env:: consts:: EXE_SUFFIX ) ) ;
272+ if !bin_path. exists ( ) {
273+ return None ;
274+ }
264275 Some ( bin_path)
265276}
266277
@@ -277,3 +288,10 @@ fn target_dir() -> Option<path::PathBuf> {
277288
278289/// The current process' target triplet.
279290const CURRENT_TARGET : & str = include_str ! ( concat!( env!( "OUT_DIR" ) , "/current_target.txt" ) ) ;
291+
292+ #[ test]
293+ #[ should_panic = "`CARGO_BIN_EXE_non-existent` is unset
294+ help: if this is running within a unit test, move it to an integration test to gain access to `CARGO_BIN_EXE_non-existent`" ]
295+ fn cargo_bin_in_unit_test ( ) {
296+ cargo_bin ( "non-existent" ) ;
297+ }
0 commit comments