@@ -271,8 +271,17 @@ impl Config {
271
271
}
272
272
}
273
273
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) ;
276
285
}
277
286
278
287
// If nothing was found, check in the home directory.
@@ -296,6 +305,17 @@ impl Config {
296
305
match resolve_project_files ( dir) ? {
297
306
None => Ok ( ( Config :: default ( ) , None ) ) ,
298
307
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
+
299
319
let mut config = Config :: default ( ) ;
300
320
let mut used_paths = Vec :: with_capacity ( paths. len ( ) ) ;
301
321
for path in paths. into_iter ( ) . rev ( ) {
@@ -627,33 +647,40 @@ ignore = []
627
647
628
648
#[ test]
629
649
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#"
633
656
tab_spaces = 2
634
657
fn_call_width = 50
635
658
ignore = ["b/main.rs", "util.rs"]
636
659
"# ,
637
- ) ;
660
+ ) ;
638
661
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"
642
666
tab_spaces = 3
643
667
ignore = []
644
668
"# ,
645
- ) ;
669
+ ) ;
646
670
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 ( ) ;
649
673
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"]"# ) ;
653
677
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
+ } ;
657
684
}
658
685
659
686
mod unstable_features {
0 commit comments