Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an error when .rubocop.yml is empty #10916

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, this is to avoid the following incompatibility.

% ruby -ryaml -ve "p YAML.load('')"
ruby 3.0.4p208 (2022-04-12 revision 3fa771dded) [x86_64-darwin19]
false

% ruby -ryaml -ve "p YAML.load('')"
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-darwin19]
nil

The added test will not pass the CI matrix if this statement doesn't exist.

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