@@ -21,12 +21,11 @@ use cargo_util_schemas::manifest::{
2121 TomlManifest , TomlPackageBuild , TomlTarget , TomlTestTarget ,
2222} ;
2323
24- use crate :: core:: compiler:: rustdoc:: RustdocScrapeExamples ;
25- use crate :: core:: compiler:: CrateType ;
24+ use crate :: core:: compiler:: { rustdoc:: RustdocScrapeExamples , CrateType } ;
2625use crate :: core:: { Edition , Feature , Features , Target } ;
27- use crate :: util:: errors :: CargoResult ;
28- use crate :: util :: restricted_names ;
29- use crate :: util :: toml :: deprecated_underscore ;
26+ use crate :: util:: {
27+ closest_msg , errors :: CargoResult , restricted_names , toml :: deprecated_underscore ,
28+ } ;
3029
3130const DEFAULT_TEST_DIR_NAME : & ' static str = "tests" ;
3231const DEFAULT_BENCH_DIR_NAME : & ' static str = "benches" ;
@@ -952,73 +951,59 @@ fn target_path_not_found_error_message(
952951 package_root : & Path ,
953952 target : & TomlTarget ,
954953 target_kind : & str ,
954+ inferred : & [ ( String , PathBuf ) ] ,
955955) -> String {
956956 fn possible_target_paths ( name : & str , kind : & str , commonly_wrong : bool ) -> [ PathBuf ; 2 ] {
957957 let mut target_path = PathBuf :: new ( ) ;
958958 match ( kind, commonly_wrong) {
959959 // commonly wrong paths
960960 ( "test" | "bench" | "example" , true ) => target_path. push ( kind) ,
961- ( "bin" , true ) => {
962- target_path. push ( "src" ) ;
963- target_path. push ( "bins" ) ;
964- }
961+ ( "bin" , true ) => target_path. extend ( [ "src" , "bins" ] ) ,
965962 // default inferred paths
966963 ( "test" , false ) => target_path. push ( DEFAULT_TEST_DIR_NAME ) ,
967964 ( "bench" , false ) => target_path. push ( DEFAULT_BENCH_DIR_NAME ) ,
968965 ( "example" , false ) => target_path. push ( DEFAULT_EXAMPLE_DIR_NAME ) ,
969- ( "bin" , false ) => {
970- target_path. push ( "src" ) ;
971- target_path. push ( "bin" ) ;
972- }
966+ ( "bin" , false ) => target_path. extend ( [ "src" , "bin" ] ) ,
973967 _ => unreachable ! ( "invalid target kind: {}" , kind) ,
974968 }
975- target_path. push ( name) ;
976969
977970 let target_path_file = {
978971 let mut path = target_path. clone ( ) ;
979- path. set_extension ( " rs") ;
972+ path. push ( format ! ( "{name}. rs") ) ;
980973 path
981974 } ;
982975 let target_path_subdir = {
983- target_path. push ( "main.rs" ) ;
976+ target_path. extend ( [ name , "main.rs" ] ) ;
984977 target_path
985978 } ;
986979 return [ target_path_file, target_path_subdir] ;
987980 }
988981
989982 let target_name = name_or_panic ( target) ;
983+
990984 let commonly_wrong_paths = possible_target_paths ( & target_name, target_kind, true ) ;
991985 let possible_paths = possible_target_paths ( & target_name, target_kind, false ) ;
992- let existing_wrong_path_index = match (
993- package_root. join ( & commonly_wrong_paths[ 0 ] ) . exists ( ) ,
994- package_root. join ( & commonly_wrong_paths[ 1 ] ) . exists ( ) ,
995- ) {
996- ( true , _) => Some ( 0 ) ,
997- ( _, true ) => Some ( 1 ) ,
998- _ => None ,
999- } ;
1000986
1001- if let Some ( i) = existing_wrong_path_index {
1002- return format ! (
1003- "\
1004- can't find `{name}` {kind} at default paths, but found a file at `{wrong_path}`.
1005- Perhaps rename the file to `{possible_path}` for target auto-discovery, \
1006- or specify {kind}.path if you want to use a non-default path.",
1007- name = target_name,
1008- kind = target_kind,
1009- wrong_path = commonly_wrong_paths[ i] . display( ) ,
1010- possible_path = possible_paths[ i] . display( ) ,
1011- ) ;
987+ let msg = closest_msg ( target_name, inferred. iter ( ) , |( n, _p) | n, target_kind) ;
988+ if let Some ( ( wrong_path, possible_path) ) = commonly_wrong_paths
989+ . iter ( )
990+ . zip ( possible_paths. iter ( ) )
991+ . filter ( |( wp, _) | package_root. join ( wp) . exists ( ) )
992+ . next ( )
993+ {
994+ let [ wrong_path, possible_path] = [ wrong_path, possible_path] . map ( |p| p. display ( ) ) ;
995+ format ! (
996+ "can't find `{target_name}` {target_kind} at default paths, but found a file at `{wrong_path}`.\n \
997+ Perhaps rename the file to `{possible_path}` for target auto-discovery, \
998+ or specify {target_kind}.path if you want to use a non-default path.{msg}",
999+ )
1000+ } else {
1001+ let [ path_file, path_dir] = possible_paths. each_ref ( ) . map ( |p| p. display ( ) ) ;
1002+ format ! (
1003+ "can't find `{target_name}` {target_kind} at `{path_file}` or `{path_dir}`. \
1004+ Please specify {target_kind}.path if you want to use a non-default path.{msg}"
1005+ )
10121006 }
1013-
1014- format ! (
1015- "can't find `{name}` {kind} at `{path_file}` or `{path_dir}`. \
1016- Please specify {kind}.path if you want to use a non-default path.",
1017- name = target_name,
1018- kind = target_kind,
1019- path_file = possible_paths[ 0 ] . display( ) ,
1020- path_dir = possible_paths[ 1 ] . display( ) ,
1021- )
10221007}
10231008
10241009fn target_path (
@@ -1054,6 +1039,7 @@ fn target_path(
10541039 package_root,
10551040 target,
10561041 target_kind,
1042+ inferred,
10571043 ) )
10581044 }
10591045 ( Some ( p0) , Some ( p1) ) => {
0 commit comments