Skip to content

Commit

Permalink
[Fix rubocop#12699] Support searching for .rubocop.yml in complianc…
Browse files Browse the repository at this point in the history
…e with dot-config

Resolves rubocop#12699

This PR supports searching for `.rubocop.yml` in compliance with dot-config.

[dot-config](https://dot-config.github.io/) prioritizes project-specific configurations over
user-specific configurations, with a search order that first looks for the `.rubocop.yml`
within the project's directories, in accordance with dot-config standards,
before considering user-specific configurations.
  • Loading branch information
koic committed Feb 16, 2024
1 parent 948a6ed commit 30e87b8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/new_support_dot_config.md
@@ -0,0 +1 @@
* [#12699](https://github.com/rubocop/rubocop/issues/12699): Support searching for `.rubocop.yml` in compliance with dot-config. ([@koic][])
4 changes: 3 additions & 1 deletion docs/modules/ROOT/pages/configuration.adoc
Expand Up @@ -29,10 +29,12 @@ RuboCop will start looking for the configuration file in the directory
where the inspected file is and continue its way up to the root directory.

If it cannot be found until reaching the project's root directory, then it will
be searched for in the user's global config locations, which consists of a
be searched for in the https://dot-config.github.io[.config directory of the project root]
and the user's global config locations. The user's global config locations consist of a
dotfile or a config file inside the https://specifications.freedesktop.org/basedir-spec/latest/index.html[XDG Base Directory
specification].

* `.config/.rubocop.yml` of the project root
* `~/.rubocop.yml`
* `$XDG_CONFIG_HOME/rubocop/config.yml` (expands to `~/.config/rubocop/config.yml`
if `$XDG_CONFIG_HOME` is not set)
Expand Down
12 changes: 10 additions & 2 deletions lib/rubocop/config_finder.rb
Expand Up @@ -17,8 +17,8 @@ class << self
attr_writer :project_root

def find_config_path(target_dir)
find_project_dotfile(target_dir) || find_user_dotfile || find_user_xdg_config ||
DEFAULT_FILE
find_project_dotfile(target_dir) || find_project_root_dot_config ||
find_user_dotfile || find_user_xdg_config || DEFAULT_FILE
end

# Returns the path RuboCop inferred as the root of the project. No file
Expand All @@ -41,6 +41,14 @@ def find_project_dotfile(target_dir)
find_file_upwards(DOTFILE, target_dir, project_root)
end

def find_project_root_dot_config
return unless project_root

file = File.join(project_root, '.config', DOTFILE)

file if File.exist?(file)
end

def find_user_dotfile
return unless ENV.key?('HOME')

Expand Down
23 changes: 23 additions & 0 deletions spec/rubocop/config_loader_spec.rb
Expand Up @@ -29,6 +29,29 @@

before { create_empty_file('dir/example.rb') }

context 'but a config file exists in .config directory of the project root' do
before do
create_empty_file('Gemfile')
create_empty_file('.config/.rubocop.yml')
end

it 'returns the path to the file in home directory' do
expect(configuration_file_for).to end_with('.config/.rubocop.yml')
end
end

context 'but a config file exists in both .config of the project root and home directories' do
before do
create_empty_file('Gemfile')
create_empty_file('.config/.rubocop.yml')
create_empty_file('~/.rubocop.yml')
end

it 'returns the path to the file in home directory' do
expect(configuration_file_for).to end_with('.config/.rubocop.yml')
end
end

context 'but a config file exists in home directory' do
before { create_empty_file('~/.rubocop.yml') }

Expand Down

0 comments on commit 30e87b8

Please sign in to comment.