From 9451170890c9f092c53dfa172c13db6f009d00a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Sun, 5 Jul 2020 10:40:35 +0200 Subject: [PATCH 1/2] Don't load exclusions from personal files when passing a config with custom name If passing a configuration with a custom name, like `rubocop --config-file .my_custom_project_config.yml`, that configuration should act as the project configuration and we want personal configuration files disregarded. That's actually the only case where `find_files_upwards` can return an empty list, because when given an existing `.rubocop.yml` as the starting point, it will at least return that file, and the fallback to personal config files will not be applied. Well, there's another case where `find_files_upwards` could return an empty list, which is when giving it a non existent file as the starting point. However, I don't think that ever happens in real life, and in tests it was only happening once, and due to a typo I believe. So, we can completely remove the explicit fallback to look for exclusions in personal configuration files. --- CHANGELOG.md | 1 + lib/rubocop/config_loader.rb | 1 - spec/rubocop/config_loader_spec.rb | 29 ++++++++++++++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8df42ba20a0..3576749bba4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Bug fixes * [#8252](https://github.com/rubocop-hq/rubocop/issues/8252): Fix a command line option name from `--safe-autocorrect` to `--safe-auto-correct`, which is compatible with RuboCop 0.86 and lower. ([@koic][]) +* [#8239](https://github.com/rubocop-hq/rubocop/pull/8239): Don't load `.rubocop.yml` from personal folders to check for exclusions if given a custom configuration file. ([@deivid-rodriguez][]) ## 0.87.0 (2020-07-06) diff --git a/lib/rubocop/config_loader.rb b/lib/rubocop/config_loader.rb index 1188ec0c233..d8044bfe3f2 100644 --- a/lib/rubocop/config_loader.rb +++ b/lib/rubocop/config_loader.rb @@ -119,7 +119,6 @@ def possible_new_cops?(config) def add_excludes_from_files(config, config_file) found_files = find_files_upwards(DOTFILE, config_file) - found_files = [find_user_dotfile, find_user_xdg_config].compact if found_files.empty? return if found_files.empty? return if PathUtil.relative_path(found_files.last) == diff --git a/spec/rubocop/config_loader_spec.rb b/spec/rubocop/config_loader_spec.rb index 020e452531a..e9cef8f3d97 100644 --- a/spec/rubocop/config_loader_spec.rb +++ b/spec/rubocop/config_loader_spec.rb @@ -170,6 +170,33 @@ end end + context 'when configuration has a custom name' do + let(:file_path) { '.custom_rubocop.yml' } + + before do + create_file(file_path, <<~YAML) + AllCops: + Exclude: + - vendor/** + YAML + end + + context 'and there is a personal config file in the home folder' do + before do + create_file('~/.rubocop.yml', <<~YAML) + AllCops: + Exclude: + - tmp/** + YAML + end + + it 'ignores personal AllCops/Exclude' do + excludes = configuration_from_file['AllCops']['Exclude'] + expect(excludes).to eq([File.expand_path('vendor/**')]) + end + end + end + context 'when a parent file specifies DisabledByDefault: true' do let(:file_path) { '.rubocop.yml' } @@ -965,7 +992,7 @@ class Loop < Cop end context 'when a file inherits from a url inheriting from another file' do - let(:file_path) { '.robocop.yml' } + let(:file_path) { '.rubocop.yml' } let(:cache_file) { '.rubocop-http---example-com-rubocop-yml' } let(:cache_file_2) { '.rubocop-http---example-com-inherit-yml' } From 2e02a042f331837950c44a7260b1e02f12d5c127 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Tue, 7 Jul 2020 10:43:02 +0200 Subject: [PATCH 2/2] Add a note about the `--config` flag --- docs/modules/ROOT/pages/configuration.adoc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/modules/ROOT/pages/configuration.adoc b/docs/modules/ROOT/pages/configuration.adoc index 9fb44942b10..efa060ebff7 100644 --- a/docs/modules/ROOT/pages/configuration.adoc +++ b/docs/modules/ROOT/pages/configuration.adoc @@ -51,6 +51,10 @@ files: * `~/.config/rubocop/config.yml` * https://github.com/rubocop-hq/rubocop/blob/master/config/default.yml[RuboCop's default configuration] +All the previous logic does not apply if a specific configuration file is passed +on the command line through the `--config` flag. In that case, the resolved +configuration file will be the one passed to the CLI. + == Inheritance All configuration inherits from https://github.com/rubocop-hq/rubocop/blob/master/config/default.yml[RuboCop's default configuration] (See