Skip to content

Commit 4ee5a91

Browse files
committed
fixup! Merge configs from parent directories
1 parent 9c006c9 commit 4ee5a91

File tree

2 files changed

+53
-24
lines changed

2 files changed

+53
-24
lines changed

ci/integration.sh

+8-6
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,27 @@ function show_head {
8585
echo "Head commit of ${INTEGRATION}: $head"
8686
}
8787

88+
tempdir=$(mktemp -d)
89+
8890
case ${INTEGRATION} in
8991
cargo)
90-
git clone --depth=1 https://github.com/rust-lang/${INTEGRATION}.git
91-
cd ${INTEGRATION}
92+
git clone --depth=1 https://github.com/rust-lang/${INTEGRATION}.git ${tempdir}
93+
cd ${tempdir}
9294
show_head
9395
export CFG_DISABLE_CROSS_TESTS=1
9496
check_fmt_with_all_tests
9597
cd -
9698
;;
9799
crater)
98-
git clone --depth=1 https://github.com/rust-lang-nursery/${INTEGRATION}.git
99-
cd ${INTEGRATION}
100+
git clone --depth=1 https://github.com/rust-lang-nursery/${INTEGRATION}.git ${tempdir}
101+
cd ${tempdir}
100102
show_head
101103
check_fmt_with_lib_tests
102104
cd -
103105
;;
104106
*)
105-
git clone --depth=1 https://github.com/rust-lang-nursery/${INTEGRATION}.git
106-
cd ${INTEGRATION}
107+
git clone --depth=1 https://github.com/rust-lang-nursery/${INTEGRATION}.git ${tempdir}
108+
cd ${tempdir}
107109
show_head
108110
check_fmt_with_all_tests
109111
cd -

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

+45-18
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)