@@ -291,14 +291,8 @@ impl Config {
291291 let mut file = File :: open ( & file_path) ?;
292292 let mut toml = String :: new ( ) ;
293293 file. read_to_string ( & mut toml) ?;
294- Config :: from_toml_for_style_edition (
295- & toml,
296- file_path. parent ( ) . unwrap ( ) ,
297- edition,
298- style_edition,
299- version,
300- )
301- . map_err ( |err| Error :: new ( ErrorKind :: InvalidData , err) )
294+ Config :: from_toml_for_style_edition ( & toml, file_path, edition, style_edition, version)
295+ . map_err ( |err| Error :: new ( ErrorKind :: InvalidData , err) )
302296 }
303297
304298 /// Resolves the config for input in `dir`.
@@ -370,13 +364,13 @@ impl Config {
370364 }
371365
372366 #[ allow( dead_code) ]
373- pub ( super ) fn from_toml ( toml : & str , dir : & Path ) -> Result < Config , String > {
374- Self :: from_toml_for_style_edition ( toml, dir , None , None , None )
367+ pub ( super ) fn from_toml ( toml : & str , file_path : & Path ) -> Result < Config , String > {
368+ Self :: from_toml_for_style_edition ( toml, file_path , None , None , None )
375369 }
376370
377371 pub ( crate ) fn from_toml_for_style_edition (
378372 toml : & str ,
379- dir : & Path ,
373+ file_path : & Path ,
380374 edition : Option < Edition > ,
381375 style_edition : Option < StyleEdition > ,
382376 version : Option < Version > ,
@@ -400,13 +394,19 @@ impl Config {
400394 if !err. is_empty ( ) {
401395 eprint ! ( "{err}" ) ;
402396 }
397+ let dir = file_path. parent ( ) . ok_or_else ( || {
398+ format ! ( "failed to get parent directory for {}" , file_path. display( ) )
399+ } ) ?;
400+
403401 Ok ( parsed_config. to_parsed_config ( style_edition, edition, version, dir) )
404402 }
405403 Err ( e) => {
406- err. push_str ( "Error: Decoding config file failed:\n " ) ;
407- err. push_str ( format ! ( "{e}\n " ) . as_str ( ) ) ;
408- err. push_str ( "Please check your config file." ) ;
409- Err ( err)
404+ let err_msg = format ! (
405+ "The file `{}` failed to parse.\n Error details: {e}" ,
406+ file_path. display( )
407+ ) ;
408+ err. push_str ( & err_msg) ;
409+ Err ( err_msg)
410410 }
411411 }
412412 }
@@ -674,7 +674,7 @@ mod test {
674674
675675 #[ test]
676676 fn test_was_set ( ) {
677- let config = Config :: from_toml ( "hard_tabs = true" , Path :: new ( "" ) ) . unwrap ( ) ;
677+ let config = Config :: from_toml ( "hard_tabs = true" , Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
678678
679679 assert_eq ! ( config. was_set( ) . hard_tabs( ) , true ) ;
680680 assert_eq ! ( config. was_set( ) . verbose( ) , false ) ;
@@ -933,7 +933,8 @@ make_backup = false
933933 #[ nightly_only_test]
934934 #[ test]
935935 fn test_unstable_from_toml ( ) {
936- let config = Config :: from_toml ( "unstable_features = true" , Path :: new ( "" ) ) . unwrap ( ) ;
936+ let config =
937+ Config :: from_toml ( "unstable_features = true" , Path :: new ( "./rustfmt.toml" ) ) . unwrap ( ) ;
937938 assert_eq ! ( config. was_set( ) . unstable_features( ) , true ) ;
938939 assert_eq ! ( config. unstable_features( ) , true ) ;
939940 }
@@ -963,7 +964,7 @@ make_backup = false
963964 unstable_features = true
964965 merge_imports = true
965966 "# ;
966- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
967+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
967968 assert_eq ! ( config. imports_granularity( ) , ImportGranularity :: Crate ) ;
968969 }
969970
@@ -975,7 +976,7 @@ make_backup = false
975976 merge_imports = true
976977 imports_granularity = "Preserve"
977978 "# ;
978- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
979+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
979980 assert_eq ! ( config. imports_granularity( ) , ImportGranularity :: Preserve ) ;
980981 }
981982
@@ -986,7 +987,7 @@ make_backup = false
986987 unstable_features = true
987988 merge_imports = true
988989 "# ;
989- let mut config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
990+ let mut config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
990991 config. override_value ( "imports_granularity" , "Preserve" ) ;
991992 assert_eq ! ( config. imports_granularity( ) , ImportGranularity :: Preserve ) ;
992993 }
@@ -998,7 +999,7 @@ make_backup = false
998999 unstable_features = true
9991000 imports_granularity = "Module"
10001001 "# ;
1001- let mut config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1002+ let mut config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
10021003 config. override_value ( "merge_imports" , "true" ) ;
10031004 // no effect: the new option always takes precedence
10041005 assert_eq ! ( config. imports_granularity( ) , ImportGranularity :: Module ) ;
@@ -1015,7 +1016,7 @@ make_backup = false
10151016 use_small_heuristics = "Default"
10161017 max_width = 200
10171018 "# ;
1018- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1019+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
10191020 assert_eq ! ( config. array_width( ) , 120 ) ;
10201021 assert_eq ! ( config. attr_fn_like_width( ) , 140 ) ;
10211022 assert_eq ! ( config. chain_width( ) , 120 ) ;
@@ -1031,7 +1032,7 @@ make_backup = false
10311032 use_small_heuristics = "Max"
10321033 max_width = 120
10331034 "# ;
1034- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1035+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
10351036 assert_eq ! ( config. array_width( ) , 120 ) ;
10361037 assert_eq ! ( config. attr_fn_like_width( ) , 120 ) ;
10371038 assert_eq ! ( config. chain_width( ) , 120 ) ;
@@ -1047,7 +1048,7 @@ make_backup = false
10471048 use_small_heuristics = "Off"
10481049 max_width = 100
10491050 "# ;
1050- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1051+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
10511052 assert_eq ! ( config. array_width( ) , usize :: max_value( ) ) ;
10521053 assert_eq ! ( config. attr_fn_like_width( ) , usize :: max_value( ) ) ;
10531054 assert_eq ! ( config. chain_width( ) , usize :: max_value( ) ) ;
@@ -1069,7 +1070,7 @@ make_backup = false
10691070 struct_lit_width = 30
10701071 struct_variant_width = 34
10711072 "# ;
1072- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1073+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
10731074 assert_eq ! ( config. array_width( ) , 20 ) ;
10741075 assert_eq ! ( config. attr_fn_like_width( ) , 40 ) ;
10751076 assert_eq ! ( config. chain_width( ) , 20 ) ;
@@ -1091,7 +1092,7 @@ make_backup = false
10911092 struct_lit_width = 30
10921093 struct_variant_width = 34
10931094 "# ;
1094- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1095+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
10951096 assert_eq ! ( config. array_width( ) , 20 ) ;
10961097 assert_eq ! ( config. attr_fn_like_width( ) , 40 ) ;
10971098 assert_eq ! ( config. chain_width( ) , 20 ) ;
@@ -1113,7 +1114,7 @@ make_backup = false
11131114 struct_lit_width = 30
11141115 struct_variant_width = 34
11151116 "# ;
1116- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1117+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
11171118 assert_eq ! ( config. array_width( ) , 20 ) ;
11181119 assert_eq ! ( config. attr_fn_like_width( ) , 40 ) ;
11191120 assert_eq ! ( config. chain_width( ) , 20 ) ;
@@ -1129,7 +1130,7 @@ make_backup = false
11291130 max_width = 90
11301131 fn_call_width = 95
11311132 "# ;
1132- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1133+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
11331134 assert_eq ! ( config. fn_call_width( ) , 90 ) ;
11341135 }
11351136
@@ -1139,7 +1140,7 @@ make_backup = false
11391140 max_width = 80
11401141 attr_fn_like_width = 90
11411142 "# ;
1142- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1143+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
11431144 assert_eq ! ( config. attr_fn_like_width( ) , 80 ) ;
11441145 }
11451146
@@ -1149,7 +1150,7 @@ make_backup = false
11491150 max_width = 78
11501151 struct_lit_width = 90
11511152 "# ;
1152- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1153+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
11531154 assert_eq ! ( config. struct_lit_width( ) , 78 ) ;
11541155 }
11551156
@@ -1159,7 +1160,7 @@ make_backup = false
11591160 max_width = 80
11601161 struct_variant_width = 90
11611162 "# ;
1162- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1163+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
11631164 assert_eq ! ( config. struct_variant_width( ) , 80 ) ;
11641165 }
11651166
@@ -1169,7 +1170,7 @@ make_backup = false
11691170 max_width = 60
11701171 array_width = 80
11711172 "# ;
1172- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1173+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
11731174 assert_eq ! ( config. array_width( ) , 60 ) ;
11741175 }
11751176
@@ -1179,7 +1180,7 @@ make_backup = false
11791180 max_width = 80
11801181 chain_width = 90
11811182 "# ;
1182- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1183+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
11831184 assert_eq ! ( config. chain_width( ) , 80 ) ;
11841185 }
11851186
@@ -1189,7 +1190,7 @@ make_backup = false
11891190 max_width = 70
11901191 single_line_if_else_max_width = 90
11911192 "# ;
1192- let config = Config :: from_toml ( toml, Path :: new ( "" ) ) . unwrap ( ) ;
1193+ let config = Config :: from_toml ( toml, Path :: new ( "./rustfmt.toml " ) ) . unwrap ( ) ;
11931194 assert_eq ! ( config. single_line_if_else_max_width( ) , 70 ) ;
11941195 }
11951196
0 commit comments