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

Conversation

koic
Copy link
Member

@koic koic commented Aug 12, 2022

This PR fixes the following error when .rubocop.yml is empty.

% 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>'

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@koic koic force-pushed the fix_an_error_when_dot_rubocop_yml_is_empty branch from f52b553 to 04427cc Compare August 12, 2022 01:05
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>'
```
@koic koic force-pushed the fix_an_error_when_dot_rubocop_yml_is_empty branch from 04427cc to 47b1d78 Compare August 12, 2022 01:11
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.

@bbatsov bbatsov merged commit 71ea79c into rubocop:master Aug 12, 2022
@bbatsov
Copy link
Collaborator

bbatsov commented Aug 12, 2022

Thanks!

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 12, 2022

Might also be a good idea to issue some warning when this happens as an empty .rubocop.yml doesn't make much sense in general.

@koic koic deleted the fix_an_error_when_dot_rubocop_yml_is_empty branch August 12, 2022 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants