@@ -21,12 +21,11 @@ use cargo_util_schemas::manifest::{
21
21
TomlManifest , TomlPackageBuild , TomlTarget , TomlTestTarget ,
22
22
} ;
23
23
24
- use crate :: core:: compiler:: rustdoc:: RustdocScrapeExamples ;
25
- use crate :: core:: compiler:: CrateType ;
24
+ use crate :: core:: compiler:: { rustdoc:: RustdocScrapeExamples , CrateType } ;
26
25
use 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
+ } ;
30
29
31
30
const DEFAULT_TEST_DIR_NAME : & ' static str = "tests" ;
32
31
const DEFAULT_BENCH_DIR_NAME : & ' static str = "benches" ;
@@ -952,73 +951,59 @@ fn target_path_not_found_error_message(
952
951
package_root : & Path ,
953
952
target : & TomlTarget ,
954
953
target_kind : & str ,
954
+ inferred : & [ ( String , PathBuf ) ] ,
955
955
) -> String {
956
956
fn possible_target_paths ( name : & str , kind : & str , commonly_wrong : bool ) -> [ PathBuf ; 2 ] {
957
957
let mut target_path = PathBuf :: new ( ) ;
958
958
match ( kind, commonly_wrong) {
959
959
// commonly wrong paths
960
960
( "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" ] ) ,
965
962
// default inferred paths
966
963
( "test" , false ) => target_path. push ( DEFAULT_TEST_DIR_NAME ) ,
967
964
( "bench" , false ) => target_path. push ( DEFAULT_BENCH_DIR_NAME ) ,
968
965
( "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" ] ) ,
973
967
_ => unreachable ! ( "invalid target kind: {}" , kind) ,
974
968
}
975
- target_path. push ( name) ;
976
969
977
970
let target_path_file = {
978
971
let mut path = target_path. clone ( ) ;
979
- path. set_extension ( " rs") ;
972
+ path. push ( format ! ( "{name}. rs") ) ;
980
973
path
981
974
} ;
982
975
let target_path_subdir = {
983
- target_path. push ( "main.rs" ) ;
976
+ target_path. extend ( [ name , "main.rs" ] ) ;
984
977
target_path
985
978
} ;
986
979
return [ target_path_file, target_path_subdir] ;
987
980
}
988
981
989
982
let target_name = name_or_panic ( target) ;
983
+
990
984
let commonly_wrong_paths = possible_target_paths ( & target_name, target_kind, true ) ;
991
985
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
- } ;
1000
986
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
+ )
1012
1006
}
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
- )
1022
1007
}
1023
1008
1024
1009
fn target_path (
@@ -1054,6 +1039,7 @@ fn target_path(
1054
1039
package_root,
1055
1040
target,
1056
1041
target_kind,
1042
+ inferred,
1057
1043
) )
1058
1044
}
1059
1045
( Some ( p0) , Some ( p1) ) => {
0 commit comments