From 30e87b892fc1504573244055b3f88d1682e75043 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 17 Feb 2024 03:25:43 +0900 Subject: [PATCH] [Fix #12699] Support searching for `.rubocop.yml` in compliance with dot-config Resolves #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. --- changelog/new_support_dot_config.md | 1 + docs/modules/ROOT/pages/configuration.adoc | 4 +++- lib/rubocop/config_finder.rb | 12 +++++++++-- spec/rubocop/config_loader_spec.rb | 23 ++++++++++++++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 changelog/new_support_dot_config.md diff --git a/changelog/new_support_dot_config.md b/changelog/new_support_dot_config.md new file mode 100644 index 000000000000..b44ad49cbb7f --- /dev/null +++ b/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][]) diff --git a/docs/modules/ROOT/pages/configuration.adoc b/docs/modules/ROOT/pages/configuration.adoc index 3d8505b62917..c4d7d4075dfb 100644 --- a/docs/modules/ROOT/pages/configuration.adoc +++ b/docs/modules/ROOT/pages/configuration.adoc @@ -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) diff --git a/lib/rubocop/config_finder.rb b/lib/rubocop/config_finder.rb index 648034501ef8..667c71d05139 100644 --- a/lib/rubocop/config_finder.rb +++ b/lib/rubocop/config_finder.rb @@ -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 @@ -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') diff --git a/spec/rubocop/config_loader_spec.rb b/spec/rubocop/config_loader_spec.rb index 2c50d67de3bd..d8698990f9b4 100644 --- a/spec/rubocop/config_loader_spec.rb +++ b/spec/rubocop/config_loader_spec.rb @@ -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') }