Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Return an error when multiple config files are found #6139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,24 @@ pub fn load_config<O: CliOptions>(
// Return the path if a config file exists, empty if no file exists, and Error for IO errors
fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> {
const CONFIG_FILE_NAMES: [&str; 2] = [".rustfmt.toml", "rustfmt.toml"];
let mut found_file_name: Option<PathBuf> = None;
for config_file_name in &CONFIG_FILE_NAMES {
let config_file = dir.join(config_file_name);
match fs::metadata(&config_file) {
// Only return if it's a file to handle the unlikely situation of a directory named
// `rustfmt.toml`.
Ok(ref md) if md.is_file() => return Ok(Some(config_file)),
Ok(ref md) if md.is_file() => {
if found_file_name.is_some() {
// If both rustfmt.toml and .rustfmt.toml exists, return an Error
let msg = format!(
"Multiple config files found {:?} and {:?}",
&found_file_name, &config_file
);
return Err(Error::new(ErrorKind::Other, msg));
} else {
found_file_name = Some(config_file);
}
}
// Return the error if it's something other than `NotFound`; otherwise we didn't
// find the project file yet, and continue searching.
Err(e) => {
Expand All @@ -385,7 +397,7 @@ fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> {
_ => {}
}
}
Ok(None)
Ok(found_file_name)
}

fn config_path(options: &dyn CliOptions) -> Result<Option<PathBuf>, Error> {
Expand Down
Loading