Skip to content

Commit fc9e0bc

Browse files
committed
fixup! Merge configs from parent directories
1 parent 2a9648c commit fc9e0bc

File tree

2 files changed

+53
-24
lines changed

2 files changed

+53
-24
lines changed

ci/integration.sh

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,27 @@ function show_head {
7171
echo "Head commit of ${INTEGRATION}: $head"
7272
}
7373

74+
tempdir=$(mktemp -d)
75+
7476
case ${INTEGRATION} in
7577
cargo)
76-
git clone --depth=1 https://github.com/rust-lang/${INTEGRATION}.git
77-
cd ${INTEGRATION}
78+
git clone --depth=1 https://github.com/rust-lang/${INTEGRATION}.git ${tempdir}
79+
cd ${tempdir}
7880
show_head
7981
export CFG_DISABLE_CROSS_TESTS=1
8082
check_fmt_with_all_tests
8183
cd -
8284
;;
8385
crater)
84-
git clone --depth=1 https://github.com/rust-lang-nursery/${INTEGRATION}.git
85-
cd ${INTEGRATION}
86+
git clone --depth=1 https://github.com/rust-lang-nursery/${INTEGRATION}.git ${tempdir}
87+
cd ${tempdir}
8688
show_head
8789
check_fmt_with_lib_tests
8890
cd -
8991
;;
9092
*)
91-
git clone --depth=1 https://github.com/rust-lang-nursery/${INTEGRATION}.git
92-
cd ${INTEGRATION}
93+
git clone --depth=1 https://github.com/rust-lang-nursery/${INTEGRATION}.git ${tempdir}
94+
cd ${tempdir}
9395
show_head
9496
check_fmt_with_all_tests
9597
cd -

rustfmt-core/rustfmt-lib/src/config.rs

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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#"
633656
tab_spaces = 2
634657
fn_call_width = 50
635658
ignore = ["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"
642666
tab_spaces = 3
643667
ignore = []
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

Comments
 (0)