@@ -271,8 +271,17 @@ impl Config {
271271 }
272272 }
273273
274- if !paths. is_empty ( ) {
275- return Ok ( paths. into_iter ( ) . filter ( |p| p. is_some ( ) ) . collect ( ) ) ;
274+ // List of closest -> most distant rustfmt config from the current directory.
275+ let config_paths: Option < Vec < _ > > = paths. into_iter ( ) . filter ( |p| p. is_some ( ) ) . collect ( ) ;
276+ let has_paths = config_paths. as_ref ( ) . and_then ( |paths| {
277+ if paths. is_empty ( ) {
278+ None
279+ } else {
280+ Some ( paths. len ( ) )
281+ }
282+ } ) ;
283+ if has_paths. is_some ( ) {
284+ return Ok ( config_paths) ;
276285 }
277286
278287 // If nothing was found, check in the home directory.
@@ -296,6 +305,17 @@ impl Config {
296305 match resolve_project_files ( dir) ? {
297306 None => Ok ( ( Config :: default ( ) , None ) ) ,
298307 Some ( paths) => {
308+ // If this branch is hit, there must be at least one config file available.
309+ let most_local_path = & paths[ 0 ] ;
310+ if let Ok ( partial_config) = PartialConfig :: from_toml_path ( most_local_path) {
311+ let config =
312+ Config :: default ( ) . fill_from_parsed_config ( partial_config, most_local_path) ;
313+ if config. version ( ) == Version :: One {
314+ // In v1, don't merge transient config files. Just use the most local one.
315+ return Ok ( ( config, Some ( vec ! [ most_local_path. clone( ) ] ) ) ) ;
316+ }
317+ }
318+
299319 let mut config = Config :: default ( ) ;
300320 let mut used_paths = Vec :: with_capacity ( paths. len ( ) ) ;
301321 for path in paths. into_iter ( ) . rev ( ) {
@@ -627,33 +647,40 @@ ignore = []
627647
628648 #[ test]
629649 fn test_merged_config ( ) {
630- let _outer_config = make_temp_file (
631- "a/rustfmt.toml" ,
632- r#"
650+ match option_env ! ( "CFG_RELEASE_CHANNEL" ) {
651+ // this test requires nightly
652+ None | Some ( "nightly" ) => {
653+ let _outer_config = make_temp_file (
654+ "a/rustfmt.toml" ,
655+ r#"
633656tab_spaces = 2
634657fn_call_width = 50
635658ignore = ["b/main.rs", "util.rs"]
636659"# ,
637- ) ;
660+ ) ;
638661
639- let inner_config = make_temp_file (
640- "a/b/rustfmt.toml" ,
641- r#"
662+ let inner_config = make_temp_file (
663+ "a/b/rustfmt.toml" ,
664+ r#"
665+ version = "two"
642666tab_spaces = 3
643667ignore = []
644668"# ,
645- ) ;
669+ ) ;
646670
647- let inner_dir = inner_config. path . parent ( ) . unwrap ( ) ;
648- let ( config, paths) = load_config :: < NullOptions > ( Some ( inner_dir) , None ) . unwrap ( ) ;
671+ let inner_dir = inner_config. path . parent ( ) . unwrap ( ) ;
672+ let ( config, paths) = load_config :: < NullOptions > ( Some ( inner_dir) , None ) . unwrap ( ) ;
649673
650- assert_eq ! ( config. tab_spaces( ) , 3 ) ;
651- assert_eq ! ( config. fn_call_width( ) , 50 ) ;
652- assert_eq ! ( config. ignore( ) . to_string( ) , r#"["main.rs"]"# ) ;
674+ assert_eq ! ( config. tab_spaces( ) , 3 ) ;
675+ assert_eq ! ( config. fn_call_width( ) , 50 ) ;
676+ assert_eq ! ( config. ignore( ) . to_string( ) , r#"["main.rs"]"# ) ;
653677
654- let paths = paths. unwrap ( ) ;
655- assert ! ( paths[ 0 ] . ends_with( "a/rustfmt.toml" ) ) ;
656- assert ! ( paths[ 1 ] . ends_with( "a/b/rustfmt.toml" ) ) ;
678+ let paths = paths. unwrap ( ) ;
679+ assert ! ( paths[ 0 ] . ends_with( "a/rustfmt.toml" ) ) ;
680+ assert ! ( paths[ 1 ] . ends_with( "a/b/rustfmt.toml" ) ) ;
681+ }
682+ _ => ( ) ,
683+ } ;
657684 }
658685
659686 mod unstable_features {
0 commit comments