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

undefined method `to_sym' for nil:NilClass in some Rails cop #127

Closed
znz opened this issue Sep 10, 2019 · 7 comments · Fixed by rubocop/rubocop#7632
Closed

undefined method `to_sym' for nil:NilClass in some Rails cop #127

znz opened this issue Sep 10, 2019 · 7 comments · Fixed by rubocop/rubocop#7632
Labels
bug Something isn't working

Comments

@znz
Copy link

znz commented Sep 10, 2019

rubocop -r rubocop-rails --only Rails/FilePath dummy.rb -d shows following error:

undefined method `to_sym' for nil:NilClass
.../rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/mixin/configurable_enforced_style.rb:65:in `style'

It seems other Rails cops supporting EnfocedStyle cause same error too.

I cannot reproduce without rubocop-rails.


Expected behavior

No errors.

Actual behavior

Error occurred with some Rails cops.

Steps to reproduce the problem

% rubocop -r rubocop-rails --only Rails/FilePath dummy.rb
Inspecting 1 file
An error occurred while Rails/FilePath cop was inspecting /private/tmp/dummy/dummy.rb:1:0.
To see the complete backtrace run rubocop -d.
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Rails/FilePath cop was inspecting /private/tmp/dummy/dummy.rb:1:0.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop-hq/rubocop/issues

Mention the following information in the issue report:
0.74.0 (using Parser 2.6.4.0, running on ruby 2.6.4 x86_64-darwin18)
% cat dummy.rb
dummy
% cat Gemfile
# frozen_string_literal: true

source "https://rubygems.org"

gem "rubocop"
gem "rubocop-rails"

RuboCop version

Include the output of rubocop -V or bundle exec rubocop -V if using Bundler. Here's an example:

% rubocop -V
0.74.0 (using Parser 2.6.4.0, running on ruby 2.6.4 x86_64-darwin18)
% bundle list
Gems included by the bundle:
  * ast (2.4.0)
  * bundler (1.17.2)
  * jaro_winkler (1.5.3)
  * parallel (1.17.0)
  * parser (2.6.4.0)
  * rack (2.0.7)
  * rainbow (3.0.0)
  * rubocop (0.74.0)
  * rubocop-rails (2.3.2)
  * ruby-progressbar (1.10.1)
  * unicode-display_width (1.6.0)
@koic koic transferred this issue from rubocop/rubocop Sep 10, 2019
@koic
Copy link
Member

koic commented Sep 11, 2019

I cannot reproduce this issue. Is something set in .rubocop.yml?

@znz
Copy link
Author

znz commented Sep 11, 2019

No .rubocop.yml.

I can reproduce it in docker environment:

% docker run -it --rm ruby:2.6.4 /bin/bash -exc 'gem i rubocop-rails; echo dummy | rubocop -r rubocop-rails --only Rails/FilePath --stdin dummy.rb -d'
+ gem i rubocop-rails
Fetching rack-2.0.7.gem
Fetching jaro_winkler-1.5.3.gem
Fetching parallel-1.17.0.gem
Fetching ast-2.4.0.gem
Fetching parser-2.6.4.0.gem
Fetching rainbow-3.0.0.gem
Fetching unicode-display_width-1.6.0.gem
Fetching ruby-progressbar-1.10.1.gem
Fetching rubocop-rails-2.3.2.gem
Fetching rubocop-0.74.0.gem
Successfully installed rack-2.0.7
Building native extensions. This could take a while...
Successfully installed jaro_winkler-1.5.3
Successfully installed parallel-1.17.0
Successfully installed ast-2.4.0
Successfully installed parser-2.6.4.0
Successfully installed rainbow-3.0.0
Successfully installed ruby-progressbar-1.10.1
Successfully installed unicode-display_width-1.6.0
Successfully installed rubocop-0.74.0
Successfully installed rubocop-rails-2.3.2
10 gems installed
+ echo dummy
+ rubocop -r rubocop-rails --only Rails/FilePath --stdin dummy.rb -d
For /: configuration from /usr/local/bundle/gems/rubocop-0.74.0/config/default.yml
Inspecting 1 file
Scanning /dummy.rb
An error occurred while Rails/FilePath cop was inspecting /dummy.rb:1:0.
undefined method `to_sym' for nil:NilClass
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/cop/mixin/configurable_enforced_style.rb:65:in `style'
/usr/local/bundle/gems/rubocop-rails-2.3.2/lib/rubocop/cop/rails/file_path.rb:83:in `check_for_rails_root_join_with_slash_separated_path'
/usr/local/bundle/gems/rubocop-rails-2.3.2/lib/rubocop/cop/rails/file_path.rb:59:in `on_send'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/cop/commissioner.rb:57:in `block (2 levels) in trigger_responding_cops'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/cop/commissioner.rb:128:in `with_cop_error_handling'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/cop/commissioner.rb:56:in `block in trigger_responding_cops'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/cop/commissioner.rb:55:in `each'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/cop/commissioner.rb:55:in `trigger_responding_cops'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/cop/commissioner.rb:32:in `block (2 levels) in <class:Commissioner>'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/ast/traversal.rb:13:in `walk'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/cop/commissioner.rb:44:in `investigate'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/cop/team.rb:119:in `investigate'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/cop/team.rb:107:in `offenses'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/cop/team.rb:44:in `inspect_file'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/runner.rb:277:in `inspect_file'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/runner.rb:225:in `block in do_inspection_loop'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/runner.rb:257:in `block in iterate_until_no_changes'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/runner.rb:250:in `loop'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/runner.rb:250:in `iterate_until_no_changes'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/runner.rb:221:in `do_inspection_loop'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/runner.rb:124:in `block in file_offenses'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/runner.rb:142:in `file_offense_cache'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/runner.rb:122:in `file_offenses'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/runner.rb:110:in `process_file'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/runner.rb:87:in `block in each_inspected_file'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/runner.rb:86:in `each'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/runner.rb:86:in `reduce'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/runner.rb:86:in `each_inspected_file'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/runner.rb:73:in `inspect_files'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/runner.rb:39:in `run'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/cli.rb:210:in `execute_runner'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/cli.rb:80:in `execute_runners'
/usr/local/bundle/gems/rubocop-0.74.0/lib/rubocop/cli.rb:51:in `run'
/usr/local/bundle/gems/rubocop-0.74.0/exe/rubocop:13:in `block in <top (required)>'
/usr/local/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
/usr/local/bundle/gems/rubocop-0.74.0/exe/rubocop:12:in `<top (required)>'
/usr/local/bundle/bin/rubocop:23:in `load'
/usr/local/bundle/bin/rubocop:23:in `<main>'
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Rails/FilePath cop was inspecting /dummy.rb:1:0.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop-hq/rubocop/issues

Mention the following information in the issue report:
0.74.0 (using Parser 2.6.4.0, running on ruby 2.6.4 x86_64-linux)
Finished in 0.3084206999919843 seconds

@koic
Copy link
Member

koic commented Sep 11, 2019

Thanks for the reproduction step with Docker environment. I can reproduce it.

@pocke
Copy link
Contributor

pocke commented Sep 19, 2019

I can reproduce the bug only if the .rubocop.yml exists.

$ ls -A
dummy.rb
$ rubocop -r rubocop-rails --only Rails/FilePath dummy.rb --cache false
# => display the error

$ touch .rubocop.yml
$ rubocop -r rubocop-rails --only Rails/FilePath dummy.rb --cache false
# => no error

And I can avoid the error with --force-default-config.

$ ls -A
dummy.rb
$ rubocop -r rubocop-rails --only Rails/FilePath dummy.rb --cache false
# => display the error

$ rubocop -r rubocop-rails --only Rails/FilePath dummy.rb --cache false --force-default-config
# => no error

I can also reproduce the error with Rails/HttpStatus cop.
So I guess other rubocop plugins also have the error.

@pocke pocke added the bug Something isn't working label Sep 19, 2019
@pluton-house
Copy link

I confess I'm swimming in a sea of confusion, but I wonder if I've stumbled across something relevant to this thread.
I tried following the seemingly straightforward instructions to include rubocop-rails in my Rails app, and when I tried to run it, I was awash with errors similar to that described above.
It seems to be that rubocop-rails is running using the default.yml from the rubocop gem instead of the one from the rubocop-rails gem. When I force it to use the config file from rubocop-rails gem, it works. What is the mechanism for it to switch which default config file to use, because it seems that isn't working. I concur that adding --force-default-config does make it work.

@znz
Copy link
Author

znz commented Jan 6, 2020

I try to add default value to .rubocop.yml explicitly, but it does not solved.

Rails/FilePath:
  EnforcedStyle: "slashes"

hanachin added a commit to hanachin/rubocop that referenced this issue Jan 7, 2020
When the config file path is same as the default config file,
use `ConfigLoader.default_configuration` instead of create a new config.

Because some plugins add their default config values to
`ConfigLoader.default_configuration`. So the Cops of the plugins expect
that default config values exists.

Fix rubocop/rubocop-rails#127
@znz
Copy link
Author

znz commented Jan 7, 2020

I found my situation caused by --stdin /path/to/without/yml/file.

Test environment:

% cat Dockerfile
FROM ruby:2.7.0
RUN gem install rubocop-rails
% docker run --rm -it rubocop-error rubocop -V
0.79.0 (using Parser 2.7.0.1, running on ruby 2.7.0 x86_64-linux)

Without .rubocop.yml in ancestors of dummy file path.

% docker run --rm -it rubocop-error bash -c 'rubocop -r rubocop-rails --stdin /path/to/dummy.rb <<<"x"'
Inspecting 1 file
An error occurred while Rails/ActionFilter cop was inspecting /path/to/dummy.rb:1:0.
To see the complete backtrace run rubocop -d.
An error occurred while Rails/DynamicFindBy cop was inspecting /path/to/dummy.rb:1:0.
To see the complete backtrace run rubocop -d.
An error occurred while Rails/FilePath cop was inspecting /path/to/dummy.rb:1:0.
To see the complete backtrace run rubocop -d.
An error occurred while Rails/UniqBeforePluck cop was inspecting /path/to/dummy.rb:1:0.
To see the complete backtrace run rubocop -d.
C

Offenses:

ath/to/dummy.rb:1:1: C: Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.
x
^

1 file inspected, 1 offense detected

4 errors occurred:
An error occurred while Rails/ActionFilter cop was inspecting /path/to/dummy.rb:1:0.
An error occurred while Rails/DynamicFindBy cop was inspecting /path/to/dummy.rb:1:0.
An error occurred while Rails/FilePath cop was inspecting /path/to/dummy.rb:1:0.
An error occurred while Rails/UniqBeforePluck cop was inspecting /path/to/dummy.rb:1:0.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop-hq/rubocop/issues

Mention the following information in the issue report:
0.79.0 (using Parser 2.7.0.1, running on ruby 2.7.0 x86_64-linux)

With .rubocop.yml in ancestors of dummy file path.

% docker run --rm -it rubocop-error bash -c 'mkdir -p /path/to; touch /path/to/.rubocop.yml; rubocop -r rubocop-rails --stdin /path/to/dummy.rb <<<"x"'
Inspecting 1 file
C

Offenses:

ath/to/dummy.rb:1:1: C: Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.
x
^

1 file inspected, 1 offense detected
% docker run --rm -it rubocop-error bash -c 'mkdir -p /path; touch /path/.rubocop.yml; rubocop -r rubocop-rails --stdin /path/to/dummy.rb <<<"x"'
Inspecting 1 file
C

Offenses:

ath/to/dummy.rb:1:1: C: Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.
x
^

1 file inspected, 1 offense detected

bbatsov added a commit to rubocop/rubocop that referenced this issue Apr 16, 2020
When the config file path is same as the default config file,
use `ConfigLoader.default_configuration` instead of create a new config.

Because some plugins add their default config values to
`ConfigLoader.default_configuration`. So the Cops of the plugins expect
that default config values exists.

Fix rubocop/rubocop-rails#127

Co-authored-by: Bozhidar Batsov <bozhidar@batsov.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants