@@ -35,12 +35,18 @@ fn format_cargo_dependency(dep: &str) -> String {
3535async fn find_package_dir (
3636 repo : & Path ,
3737 binary_name : & str ,
38+ cargo : Option < & Path > ,
3839) -> anyhow:: Result < ( PathBuf , String , bool ) > {
3940 let repo = repo. to_path_buf ( ) ;
4041 let binary_name = binary_name. to_string ( ) ;
42+ let cargo = cargo. map ( Path :: to_path_buf) ;
4143
4244 tokio:: task:: spawn_blocking ( move || {
43- let metadata = MetadataCommand :: new ( )
45+ let mut cmd = MetadataCommand :: new ( ) ;
46+ if let Some ( cargo) = cargo {
47+ cmd. cargo_path ( cargo) ;
48+ }
49+ let metadata = cmd
4450 . manifest_path ( repo. join ( "Cargo.toml" ) )
4551 . no_deps ( )
4652 . exec ( )
@@ -207,7 +213,7 @@ impl LanguageImpl for Rust {
207213
208214 // Find the specific package directory for this hook's binary
209215 let ( package_dir, package_name, is_workspace) =
210- find_package_dir ( repo, binary_name) . await ?;
216+ find_package_dir ( repo, binary_name, Some ( & cargo ) ) . await ?;
211217
212218 if lib_deps. is_empty ( ) && !is_workspace {
213219 // For single packages without lib deps, use cargo install directly
@@ -430,8 +436,9 @@ edition = "2021"
430436 write_file ( & temp. path ( ) . join ( "Cargo.toml" ) , cargo_toml) . await ;
431437 write_file ( & temp. path ( ) . join ( "src/main.rs" ) , "fn main() {}" ) . await ;
432438
433- let ( path, pkg_name, is_workspace) =
434- find_package_dir ( temp. path ( ) , "my-tool" ) . await . unwrap ( ) ;
439+ let ( path, pkg_name, is_workspace) = find_package_dir ( temp. path ( ) , "my-tool" , None )
440+ . await
441+ . unwrap ( ) ;
435442 assert_eq ! ( path, temp. path( ) ) ;
436443 assert_eq ! ( pkg_name, "my-tool" ) ;
437444 assert ! ( !is_workspace) ;
@@ -450,7 +457,9 @@ edition = "2021"
450457 write_file ( & temp. path ( ) . join ( "src/main.rs" ) , "fn main() {}" ) . await ;
451458
452459 // Should match with underscores instead of hyphens
453- let ( path, _pkg, is_workspace) = find_package_dir ( temp. path ( ) , "my_tool" ) . await . unwrap ( ) ;
460+ let ( path, _pkg, is_workspace) = find_package_dir ( temp. path ( ) , "my_tool" , None )
461+ . await
462+ . unwrap ( ) ;
454463 assert_eq ! ( path, temp. path( ) ) ;
455464 assert ! ( !is_workspace) ;
456465 }
@@ -480,8 +489,9 @@ edition = "2021"
480489 write_file ( & temp. path ( ) . join ( "subcrate/Cargo.toml" ) , subcrate_toml) . await ;
481490 write_file ( & temp. path ( ) . join ( "subcrate/src/lib.rs" ) , "" ) . await ;
482491
483- let ( path, pkg_name, is_workspace) =
484- find_package_dir ( temp. path ( ) , "cargo-deny" ) . await . unwrap ( ) ;
492+ let ( path, pkg_name, is_workspace) = find_package_dir ( temp. path ( ) , "cargo-deny" , None )
493+ . await
494+ . unwrap ( ) ;
485495 assert_eq ! ( path, temp. path( ) ) ;
486496 assert_eq ! ( pkg_name, "cargo-deny" ) ;
487497 assert ! ( is_workspace) ;
@@ -514,7 +524,8 @@ edition = "2021"
514524 write_file ( & temp. path ( ) . join ( "lib/Cargo.toml" ) , lib_toml) . await ;
515525 write_file ( & temp. path ( ) . join ( "lib/src/lib.rs" ) , "" ) . await ;
516526
517- let ( path, pkg_name, is_workspace) = find_package_dir ( temp. path ( ) , "my-cli" ) . await . unwrap ( ) ;
527+ let ( path, pkg_name, is_workspace) =
528+ find_package_dir ( temp. path ( ) , "my-cli" , None ) . await . unwrap ( ) ;
518529 assert_eq ! ( path, temp. path( ) . join( "cli" ) ) ;
519530 assert_eq ! ( pkg_name, "my-cli" ) ;
520531 assert ! ( is_workspace) ;
@@ -549,7 +560,8 @@ path = "src/main.rs"
549560 . await ;
550561
551562 // Should find by binary name, return package name
552- let ( path, pkg_name, is_workspace) = find_package_dir ( temp. path ( ) , "typos" ) . await . unwrap ( ) ;
563+ let ( path, pkg_name, is_workspace) =
564+ find_package_dir ( temp. path ( ) , "typos" , None ) . await . unwrap ( ) ;
553565 assert_eq ! ( path, temp. path( ) . join( "crates/typos-cli" ) ) ;
554566 assert_eq ! ( pkg_name, "typos-cli" ) ;
555567 assert ! ( is_workspace) ;
@@ -570,7 +582,9 @@ edition = "2021"
570582 // Need a lib.rs or main.rs for the package itself
571583 write_file ( & temp. path ( ) . join ( "src/lib.rs" ) , "" ) . await ;
572584
573- let ( path, _pkg, is_workspace) = find_package_dir ( temp. path ( ) , "my-tool" ) . await . unwrap ( ) ;
585+ let ( path, _pkg, is_workspace) = find_package_dir ( temp. path ( ) , "my-tool" , None )
586+ . await
587+ . unwrap ( ) ;
574588 assert_eq ! ( path, temp. path( ) ) ;
575589 assert ! ( !is_workspace) ;
576590 }
@@ -594,8 +608,9 @@ edition = "2021"
594608 write_file ( & temp. path ( ) . join ( "crates/cli/Cargo.toml" ) , cli_toml) . await ;
595609 write_file ( & temp. path ( ) . join ( "crates/cli/src/main.rs" ) , "fn main() {}" ) . await ;
596610
597- let ( path, pkg_name, is_workspace) =
598- find_package_dir ( temp. path ( ) , "virtual-cli" ) . await . unwrap ( ) ;
611+ let ( path, pkg_name, is_workspace) = find_package_dir ( temp. path ( ) , "virtual-cli" , None )
612+ . await
613+ . unwrap ( ) ;
599614 assert_eq ! ( path, temp. path( ) . join( "crates/cli" ) ) ;
600615 assert_eq ! ( pkg_name, "virtual-cli" ) ;
601616 assert ! ( is_workspace) ;
@@ -629,21 +644,22 @@ edition = "2021"
629644 write_file ( & temp. path ( ) . join ( "crates/lib/Cargo.toml" ) , lib_toml) . await ;
630645 write_file ( & temp. path ( ) . join ( "crates/lib/src/lib.rs" ) , "" ) . await ;
631646
632- let ( path, pkg_name, is_workspace) = find_package_dir ( temp. path ( ) , "my-cli" ) . await . unwrap ( ) ;
647+ let ( path, pkg_name, is_workspace) =
648+ find_package_dir ( temp. path ( ) , "my-cli" , None ) . await . unwrap ( ) ;
633649 assert_eq ! ( path, temp. path( ) . join( "crates/cli" ) ) ;
634650 assert_eq ! ( pkg_name, "my-cli" ) ;
635651 assert ! ( is_workspace) ;
636652
637653 // my-lib is a library (no binary), so searching for it should fail
638- let result = find_package_dir ( temp. path ( ) , "my-lib" ) . await ;
654+ let result = find_package_dir ( temp. path ( ) , "my-lib" , None ) . await ;
639655 assert ! ( result. is_err( ) ) ;
640656 }
641657
642658 #[ tokio:: test]
643659 async fn test_find_package_dir_no_cargo_toml ( ) {
644660 let temp = TempDir :: new ( ) . unwrap ( ) ;
645661
646- let result = find_package_dir ( temp. path ( ) , "anything" ) . await ;
662+ let result = find_package_dir ( temp. path ( ) , "anything" , None ) . await ;
647663 assert ! ( result. is_err( ) ) ;
648664 // cargo metadata gives a different error message
649665 assert ! ( result. unwrap_err( ) . to_string( ) . contains( "cargo metadata" ) ) ;
@@ -667,7 +683,7 @@ edition = "2021"
667683 write_file ( & temp. path ( ) . join ( "cli/Cargo.toml" ) , cli_toml) . await ;
668684 write_file ( & temp. path ( ) . join ( "cli/src/main.rs" ) , "fn main() {}" ) . await ;
669685
670- let result = find_package_dir ( temp. path ( ) , "nonexistent-binary" ) . await ;
686+ let result = find_package_dir ( temp. path ( ) , "nonexistent-binary" , None ) . await ;
671687 assert ! ( result. is_err( ) ) ;
672688 assert ! ( result. unwrap_err( ) . to_string( ) . contains( "No package found" ) ) ;
673689 }
0 commit comments