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

support XDG_CONFIG_HOME for configuration file on macOS #6181

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
142 changes: 104 additions & 38 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ cargo_metadata = "0.18"
clap = { version = "4.4.2", features = ["derive"] }
clap-cargo = "0.12.0"
diff = "0.1"
dirs = "5.0"
etcetera = "0.8.0"
getopts = "0.2"
ignore = "0.4"
itertools = "0.12"
Expand Down
2 changes: 1 addition & 1 deletion Configurations.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Configuring Rustfmt

Rustfmt is designed to be very configurable. You can create a TOML file called `rustfmt.toml` or `.rustfmt.toml`, place it in the project or any other parent directory and it will apply the options in that file. If none of these directories contain such a file, both your home directory and a directory called `rustfmt` in your [global config directory](https://docs.rs/dirs/5.0.1/dirs/fn.config_dir.html) (e.g. `.config/rustfmt/`) are checked as well.
Rustfmt is designed to be very configurable. You can create a TOML file called `rustfmt.toml` or `.rustfmt.toml`, place it in the project or any other parent directory and it will apply the options in that file. If none of these directories contain such a file, both your home directory and a directory called `rustfmt` in your global config directory (e.g. `.config/rustfmt/`) are checked as well.

A possible content of `rustfmt.toml` or `.rustfmt.toml` might look like this:

Expand Down
17 changes: 13 additions & 4 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::io::{Error, ErrorKind, Read};
use std::path::{Path, PathBuf};
use std::{env, fs};

use etcetera::BaseStrategy;
use thiserror::Error;

use crate::config::config_type::ConfigType;
Expand Down Expand Up @@ -281,20 +282,28 @@ impl Config {
}

// If nothing was found, check in the home directory.
if let Some(home_dir) = dirs::home_dir() {
if let Ok(home_dir) = etcetera::home_dir() {
if let Some(path) = get_toml_path(&home_dir)? {
return Ok(Some(path));
}
}

// If none was found there either, check in the user's configuration directory.
if let Some(mut config_dir) = dirs::config_dir() {
config_dir.push("rustfmt");
// If nothing was found there, check in the user's configuration directory.
if let Ok(strategy) = etcetera::base_strategy::choose_base_strategy() {
let config_dir = strategy.config_dir().join("rustfmt");
if let Some(path) = get_toml_path(&config_dir)? {
return Ok(Some(path));
}
}

// If nothing was found there either, check the legacy configuration directory.
if let Ok(strategy) = etcetera::base_strategy::choose_native_strategy() {
let data_dir = strategy.data_dir().join("rustfmt");
if let Some(path) = get_toml_path(&data_dir)? {
return Ok(Some(path));
}
}

Ok(None)
}

Expand Down
Loading