Skip to content

Commit

Permalink
Fix an error when .rubocop.yml is empty
Browse files Browse the repository at this point in the history
This PR fixes the following error when .rubocop.yml is empty.

```console
% touch .rubocop.yml

% rubocop
/Users/koic/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.34.1/lib/rubocop/server/cache.rb:75:in
`block in cache_root_dir_from_config': undefined method `dig' for nil:NilClass (NoMethodError)

            config_yaml.dig('AllCops', 'CacheRootDirectory')
                       ^^^^
        from /Users/koic/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.34.1/lib/rubocop/cache_config.rb:9:in
                       `root_dir'
        from /Users/koic/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.34.1/lib/rubocop/server/cache.rb:61:in
                       `cache_root_dir_from_config'
        from /Users/koic/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.34.1/lib/rubocop/server/cache.rb:54:in
                       `cache_path'
        from /Users/koic/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.34.1/lib/rubocop/server/cache.rb:45:in
                       `dir'
        from /Users/koic/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.34.1/lib/rubocop/server.rb:36:in
                       `running?'
        from /Users/koic/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.34.1/exe/rubocop:11:in
                       `<top (required)>'
        from /Users/koic/.rbenv/versions/3.1.2/bin/rubocop:25:in `load'
        from /Users/koic/.rbenv/versions/3.1.2/bin/rubocop:25:in `<main>'
```
  • Loading branch information
koic authored and bbatsov committed Aug 12, 2022
1 parent 3ff77a3 commit 71ea79c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_an_error_when_dot_rubocop_yml_is_empty.md
@@ -0,0 +1 @@
* [#10916](https://github.com/rubocop/rubocop/pull/10916): Fix an error when .rubocop.yml is empty. ([@koic][])
6 changes: 4 additions & 2 deletions lib/rubocop/server/cache.rb
Expand Up @@ -69,10 +69,12 @@ def cache_root_dir_from_config
config_yaml = if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('4.0.0')
YAML.safe_load_file(config_path, permitted_classes: [Regexp, Symbol])
else
YAML.load_file(config_path)
config = YAML.load_file(config_path)

config == false ? nil : config
end

config_yaml.dig('AllCops', 'CacheRootDirectory')
config_yaml&.dig('AllCops', 'CacheRootDirectory')
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/rubocop/server/cache_spec.rb
Expand Up @@ -205,5 +205,19 @@
end
end
end

context 'when .rubocop.yml is empty', :isolated_environment do
context 'when cache root path is not specified path' do
before do
cache_class.cache_root_path = nil
end

it 'does not raise an error' do
create_file('.rubocop.yml', '')

expect { cache_class.cache_path }.not_to raise_error
end
end
end
end
end

0 comments on commit 71ea79c

Please sign in to comment.