Skip to content

Commit

Permalink
Read user configuration from ~/.config/ruff/ruff.toml on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Apr 23, 2024
1 parent 455d22c commit a663349
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 26 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ crossbeam = { version = "0.8.4" }
dirs = { version = "5.0.0" }
drop_bomb = { version = "0.1.5" }
env_logger = { version = "0.11.0" }
etcetera = { version = "0.8.0" }
fern = { version = "0.6.1" }
filetime = { version = "0.2.23" }
fs-err = { version = "2.11.0" }
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ruff_macros = { path = "../ruff_macros" }
anyhow = { workspace = true }
colored = { workspace = true }
dirs = { workspace = true }
etcetera = { workspace = true }
ignore = { workspace = true }
is-macro = { workspace = true }
itertools = { workspace = true }
Expand Down
45 changes: 25 additions & 20 deletions crates/ruff_workspace/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use etcetera::{AppStrategy, BaseStrategy};
use log::debug;
use pep440_rs::VersionSpecifiers;
use serde::{Deserialize, Serialize};

use ruff_linter::settings::types::PythonVersion;
use ruff_linter::warn_user_once;

use crate::options::Options;

Expand Down Expand Up @@ -99,28 +101,31 @@ pub fn find_settings_toml<P: AsRef<Path>>(path: P) -> Result<Option<PathBuf>> {
/// Find the path to the user-specific `pyproject.toml` or `ruff.toml`, if it
/// exists.
pub fn find_user_settings_toml() -> Option<PathBuf> {
// Search for a user-specific `.ruff.toml`.
let mut path = dirs::config_dir()?;
path.push("ruff");
path.push(".ruff.toml");
if path.is_file() {
return Some(path);
}

// Search for a user-specific `ruff.toml`.
let mut path = dirs::config_dir()?;
path.push("ruff");
path.push("ruff.toml");
if path.is_file() {
return Some(path);
let strategy = etcetera::base_strategy::choose_base_strategy().ok()?;
let config_dir = strategy.config_dir().join("ruff");

// Search for a user-specific `.ruff.toml`, then a `ruff.toml`, then a `pyproject.toml`.
for filename in [".ruff.toml", "ruff.toml", "pyproject.toml"] {
let path = config_dir.join(filename);
if path.is_file() {
return Some(path);
}
}

// Search for a user-specific `pyproject.toml`.
let mut path = dirs::config_dir()?;
path.push("ruff");
path.push("pyproject.toml");
if path.is_file() {
return Some(path);
// On macOS, we used to support reading from `/Users/Alice/Library/Application Support`.
if cfg!(target_os = "macos") {
let deprecated_config_dir = dirs::config_dir()?.join("ruff");

for file in [".ruff.toml", "ruff.toml", "pyproject.toml"] {
let path = deprecated_config_dir.join(file);
if path.is_file() {
warn_user_once!(
"Reading configuration from `~/Library/Application Support` is deprecated. Please move your configuration to `{}/ruff/{file}`.",
config_dir.display(),
);
return Some(path);
}
}
}

None
Expand Down
16 changes: 10 additions & 6 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -606,16 +606,20 @@ Ruff doesn't currently support INI files, like `setup.cfg` or `tox.ini`.

## How can I change Ruff's default configuration?

When no configuration file is found, Ruff will look for a user-specific `pyproject.toml` or
`ruff.toml` file as a last resort. This behavior is similar to Flake8's `~/.config/flake8`.
When no configuration file is found, Ruff will look for a user-specific `ruff.toml` file as a
last resort. This behavior is similar to Flake8's `~/.config/flake8`.

On macOS, Ruff expects that file to be located at `/Users/Alice/Library/Application Support/ruff/ruff.toml`.
On macOS and Linux, Ruff expects that file to be located at `~/.config/ruff/ruff.toml`,
and respects the `XDG_CONFIG_HOME` specification.

On Linux, Ruff expects that file to be located at `/home/alice/.config/ruff/ruff.toml`.
On Windows, Ruff expects that file to be located at `~\AppData\Roaming\ruff\ruff.toml`.

On Windows, Ruff expects that file to be located at `C:\Users\Alice\AppData\Roaming\ruff\ruff.toml`.
!!! note
Prior to `v0.5.0`, Ruff would read user-specific configuration from
`/Users/Alice/Library/Application Support/ruff/ruff.toml` on macOS. While Ruff will still respect
such configuration files, the use of `~/Library/ Application Support` is considered deprecated.

For more, see the [`dirs`](https://docs.rs/dirs/4.0.0/dirs/fn.config_dir.html) crate.
For more, see the [`etcetera`](https://crates.io/crates/etcetera) crate.

## Ruff tried to fix something — but it broke my code. What's going on?

Expand Down

0 comments on commit a663349

Please sign in to comment.