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