Skip to content

Commit

Permalink
[Fix #12695] Handle Include from inherited file in parent directory
Browse files Browse the repository at this point in the history
When we inherit configuration from a file in a parent or ancestor
directory, and that file is called `.rubocop.yml`, the paths in
its Include parameters are interpreted as being relative to the
file, or rather its directory. This is specific for file names
starting with `.rubocop`. The problem is that file name matching
that we do when searching for target files to inspect doesn't work
for patterns that start with `..`. Solve the problem by matching
absolute paths in these corner cases.
  • Loading branch information
jonas054 committed Feb 25, 2024
1 parent 4c5fce1 commit 36370f2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_config_include_bug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12695](https://github.com/rubocop/rubocop/issues/12695): Fix bug in `Include` from inherited file in a parent directory. ([@jonas054][])
8 changes: 6 additions & 2 deletions lib/rubocop/path_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ def smart_path(path)
end
end

# rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
# rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def match_path?(pattern, path)
case pattern
when String
matches =
if pattern == path
true
elsif glob?(pattern)
# File name matching doesn't really work with relative patterns the start with "..". We
# get around that problem by converting the pattern to an absolute path.
pattern = File.expand_path(pattern) if pattern.start_with?('..')

File.fnmatch?(pattern, path, File::FNM_PATHNAME | File::FNM_EXTGLOB)
end

Expand All @@ -66,7 +70,7 @@ def match_path?(pattern, path)
end
end
end
# rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity
# rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

# Returns true for an absolute Unix or Windows path.
def absolute?(path)
Expand Down
25 changes: 25 additions & 0 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,31 @@ def meow_at_4am?
abs('regexp')])
end

context 'when a .rubocop.yml is included from an ancestor directory' do
before do
create_file('child/grandkid/.rubocop.yml', <<~YAML)
inherit_from:
- ../../.rubocop.yml
YAML
end

context 'and it specifies an Include pattern' do
before do
create_file('.rubocop.yml', <<~YAML)
AllCops:
Include:
- "**/*.rbi"
YAML
end

it 'finds files included through inheritance' do
create_file('child/grandkid/file.rbi', 'x=0')
Dir.chdir('child/grandkid') { expect(cli.run(['-L'])).to eq(0) }
expect($stdout.string).to eq("file.rbi\n")
end
end
end

it 'ignores excluded files' do
create_file('example.rb', ['x = 0', 'puts x'])
create_file('regexp.rb', ['x = 0', 'puts x'])
Expand Down

0 comments on commit 36370f2

Please sign in to comment.