Skip to content

Commit

Permalink
fixup! Merge configs from parent directories
Browse files Browse the repository at this point in the history
  • Loading branch information
ayazhafiz committed May 21, 2020
1 parent 9c006c9 commit 4ee5a91
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 24 deletions.
14 changes: 8 additions & 6 deletions ci/integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,27 @@ function show_head {
echo "Head commit of ${INTEGRATION}: $head"
}

tempdir=$(mktemp -d)

case ${INTEGRATION} in
cargo)
git clone --depth=1 https://github.com/rust-lang/${INTEGRATION}.git
cd ${INTEGRATION}
git clone --depth=1 https://github.com/rust-lang/${INTEGRATION}.git ${tempdir}
cd ${tempdir}
show_head
export CFG_DISABLE_CROSS_TESTS=1
check_fmt_with_all_tests
cd -
;;
crater)
git clone --depth=1 https://github.com/rust-lang-nursery/${INTEGRATION}.git
cd ${INTEGRATION}
git clone --depth=1 https://github.com/rust-lang-nursery/${INTEGRATION}.git ${tempdir}
cd ${tempdir}
show_head
check_fmt_with_lib_tests
cd -
;;
*)
git clone --depth=1 https://github.com/rust-lang-nursery/${INTEGRATION}.git
cd ${INTEGRATION}
git clone --depth=1 https://github.com/rust-lang-nursery/${INTEGRATION}.git ${tempdir}
cd ${tempdir}
show_head
check_fmt_with_all_tests
cd -
Expand Down
63 changes: 45 additions & 18 deletions rustfmt-core/rustfmt-lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,17 @@ impl Config {
}
}

if !paths.is_empty() {
return Ok(paths.into_iter().filter(|p| p.is_some()).collect());
// List of closest -> most distant rustfmt config from the current directory.
let config_paths: Option<Vec<_>> = paths.into_iter().filter(|p| p.is_some()).collect();
let has_paths = config_paths.as_ref().and_then(|paths| {
if paths.is_empty() {
None
} else {
Some(paths.len())
}
});
if has_paths.is_some() {
return Ok(config_paths);
}

// If nothing was found, check in the home directory.
Expand All @@ -296,6 +305,17 @@ impl Config {
match resolve_project_files(dir)? {
None => Ok((Config::default(), None)),
Some(paths) => {
// If this branch is hit, there must be at least one config file available.
let most_local_path = &paths[0];
if let Ok(partial_config) = PartialConfig::from_toml_path(most_local_path) {
let config =
Config::default().fill_from_parsed_config(partial_config, most_local_path);
if config.version() == Version::One {
// In v1, don't merge transient config files. Just use the most local one.
return Ok((config, Some(vec![most_local_path.clone()])));
}
}

let mut config = Config::default();
let mut used_paths = Vec::with_capacity(paths.len());
for path in paths.into_iter().rev() {
Expand Down Expand Up @@ -627,33 +647,40 @@ ignore = []

#[test]
fn test_merged_config() {
let _outer_config = make_temp_file(
"a/rustfmt.toml",
r#"
match option_env!("CFG_RELEASE_CHANNEL") {
// this test requires nightly
None | Some("nightly") => {
let _outer_config = make_temp_file(
"a/rustfmt.toml",
r#"
tab_spaces = 2
fn_call_width = 50
ignore = ["b/main.rs", "util.rs"]
"#,
);
);

let inner_config = make_temp_file(
"a/b/rustfmt.toml",
r#"
let inner_config = make_temp_file(
"a/b/rustfmt.toml",
r#"
version = "two"
tab_spaces = 3
ignore = []
"#,
);
);

let inner_dir = inner_config.path.parent().unwrap();
let (config, paths) = load_config::<NullOptions>(Some(inner_dir), None).unwrap();
let inner_dir = inner_config.path.parent().unwrap();
let (config, paths) = load_config::<NullOptions>(Some(inner_dir), None).unwrap();

assert_eq!(config.tab_spaces(), 3);
assert_eq!(config.fn_call_width(), 50);
assert_eq!(config.ignore().to_string(), r#"["main.rs"]"#);
assert_eq!(config.tab_spaces(), 3);
assert_eq!(config.fn_call_width(), 50);
assert_eq!(config.ignore().to_string(), r#"["main.rs"]"#);

let paths = paths.unwrap();
assert!(paths[0].ends_with("a/rustfmt.toml"));
assert!(paths[1].ends_with("a/b/rustfmt.toml"));
let paths = paths.unwrap();
assert!(paths[0].ends_with("a/rustfmt.toml"));
assert!(paths[1].ends_with("a/b/rustfmt.toml"));
}
_ => (),
};
}

mod unstable_features {
Expand Down

0 comments on commit 4ee5a91

Please sign in to comment.