From 71ea79c6a50c88b1e39eb83c41af9fb0cdc35c40 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 12 Aug 2022 01:26:33 +0900 Subject: [PATCH] Fix an error when .rubocop.yml is empty 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 `' 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 `
' ``` --- .../fix_an_error_when_dot_rubocop_yml_is_empty.md | 1 + lib/rubocop/server/cache.rb | 6 ++++-- spec/rubocop/server/cache_spec.rb | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_an_error_when_dot_rubocop_yml_is_empty.md diff --git a/changelog/fix_an_error_when_dot_rubocop_yml_is_empty.md b/changelog/fix_an_error_when_dot_rubocop_yml_is_empty.md new file mode 100644 index 00000000000..fb879f72dd6 --- /dev/null +++ b/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][]) diff --git a/lib/rubocop/server/cache.rb b/lib/rubocop/server/cache.rb index 1f707466182..ce1861eebef 100644 --- a/lib/rubocop/server/cache.rb +++ b/lib/rubocop/server/cache.rb @@ -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 diff --git a/spec/rubocop/server/cache_spec.rb b/spec/rubocop/server/cache_spec.rb index a10198168e6..5a3487a9db6 100644 --- a/spec/rubocop/server/cache_spec.rb +++ b/spec/rubocop/server/cache_spec.rb @@ -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