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

"An error occurred while Rails/HasManyOrHasOneDependent cop was inspecting" since upgrading to 2.12.0 #538

Closed
schinery opened this issue Sep 9, 2021 · 2 comments
Labels
duplicate This issue or pull request already exists

Comments

@schinery
Copy link

schinery commented Sep 9, 2021

After upgrading from 2.11.3 to 2.12.0 I'm getting an "An error occurred while Rails/HasManyOrHasOneDependent cop was inspecting" when inspecting files that have a :has_many with a lambda in.

An example of the code that is now throwing an error is:

has_many :log_entries, -> { where(category: "log") }, as: :subject, class_name: "Activity",  inverse_of: :subject

Expected behavior

Inspecting the code doesn't throw an error.

Actual behavior

rubocop -A -d app/models/user.rb

For /Users/stu/ruby_thing/: configuration from /Users/stu/ruby_thing/.rubocop.yml
configuration from /Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-faker-1.1.0/config/default.yml
configuration from /Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-faker-1.1.0/config/default.yml
Default configuration from /Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/config/default.yml
configuration from /Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-minitest-0.15.0/config/default.yml
configuration from /Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-minitest-0.15.0/config/default.yml
configuration from /Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-rake-0.6.0/config/default.yml
configuration from /Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-rake-0.6.0/config/default.yml
configuration from /Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-rails-2.12.0/config/default.yml
configuration from /Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-rails-2.12.0/config/default.yml
configuration from /Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-rspec-2.4.0/config/default.yml
configuration from /Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-rspec-2.4.0/config/default.yml
Inheriting configuration from /Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/ruby_style_guide-8.3.4/default.yml
Inheriting configuration from /Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/ruby_style_guide-8.3.4/rails.yml
Inheriting configuration from /Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/ruby_style_guide-8.3.4/rspec.yml
Inheriting configuration from /Users/stu/ruby_thing/.rubocop_todo.yml
Inspecting 1 file
Scanning /Users/stu/ruby_thing/app/models/user.rb
An error occurred while Rails/HasManyOrHasOneDependent cop was inspecting /Users/stu/ruby_thing/app/models/user.rb:25:4.
undefined method `first' for nil:NilClass
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-rails-2.12.0/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb:117:in `valid_options?'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-rails-2.12.0/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb:80:in `on_send'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:136:in `public_send'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:136:in `block (2 levels) in trigger_restricted_cops'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:160:in `with_cop_error_handling'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:135:in `block in trigger_restricted_cops'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:134:in `each'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:134:in `trigger_restricted_cops'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:70:in `on_send'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.11.0/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.11.0/lib/rubocop/ast/traversal.rb:137:in `each'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.11.0/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.11.0/lib/rubocop/ast/traversal.rb:154:in `on_class'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:71:in `on_class'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.11.0/lib/rubocop/ast/traversal.rb:138:in `on_while'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:71:in `on_module'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.11.0/lib/rubocop/ast/traversal.rb:20:in `walk'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:86:in `investigate'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cop/team.rb:155:in `investigate_partial'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cop/team.rb:83:in `investigate'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/runner.rb:310:in `inspect_file'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/runner.rb:254:in `block in do_inspection_loop'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/runner.rb:288:in `block in iterate_until_no_changes'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/runner.rb:281:in `loop'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/runner.rb:281:in `iterate_until_no_changes'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/runner.rb:250:in `do_inspection_loop'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/runner.rb:130:in `block in file_offenses'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/runner.rb:155:in `file_offense_cache'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/runner.rb:129:in `file_offenses'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/runner.rb:120:in `process_file'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/runner.rb:101:in `block in each_inspected_file'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/runner.rb:100:in `each'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/runner.rb:100:in `reduce'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/runner.rb:100:in `each_inspected_file'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/runner.rb:86:in `inspect_files'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/runner.rb:47:in `run'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cli/command.rb:11:in `run'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cli/environment.rb:18:in `run'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cli.rb:71:in `run_command'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cli.rb:78:in `execute_runners'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/lib/rubocop/cli.rb:47:in `run'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/exe/rubocop:12:in `block in <top (required)>'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/3.0.0/benchmark.rb:308:in `realtime'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.20.0/exe/rubocop:12:in `<top (required)>'
/Users/stu/.rbenv/versions/3.0.2/bin/rubocop:23:in `load'
/Users/stu/.rbenv/versions/3.0.2/bin/rubocop:23:in `<top (required)>'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/3.0.0/bundler/cli/exec.rb:63:in `load'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/3.0.0/bundler/cli/exec.rb:63:in `kernel_load'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/3.0.0/bundler/cli/exec.rb:28:in `run'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/3.0.0/bundler/cli.rb:474:in `exec'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/3.0.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/3.0.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/3.0.0/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/3.0.0/bundler/cli.rb:30:in `dispatch'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/3.0.0/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/3.0.0/bundler/cli.rb:24:in `start'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/bundler-2.2.22/libexec/bundle:49:in `block in <top (required)>'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/3.0.0/bundler/friendly_errors.rb:128:in `with_friendly_errors'
/Users/stu/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/bundler-2.2.22/libexec/bundle:37:in `<top (required)>'
/Users/stu/.rbenv/versions/3.0.2/bin/bundle:23:in `load'
/Users/stu/.rbenv/versions/3.0.2/bin/bundle:23:in `<main>'
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Rails/HasManyOrHasOneDependent cop was inspecting /Users/stu/ruby_thing/app/models/user.rb:25:4.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.20.0 (using Parser 3.0.2.0, rubocop-ast 1.11.0, running on ruby 3.0.2 x86_64-darwin20)
Finished in 0.7643180000013672 seconds

Steps to reproduce the problem

  • Create a model with a :has_many that uses a lambda (like the example above)
  • Run rubocop on the file

RuboCop version

Include the output of rubocop -V or bundle exec rubocop -V if using Bundler. Here's an example:
You can see extension cop versions (e.g. rubocop-rails, rubocop-performance, and others) output by rubocop -V,
include them as well. Here's an example:

bundle exec rubocop -V
1.20.0 (using Parser 3.0.2.0, rubocop-ast 1.11.0, running on ruby 3.0.2 x86_64-darwin20)
  - rubocop-faker 1.1.0
  - rubocop-minitest 0.15.0
  - rubocop-rails 2.12.0
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.4.0
@dvandersluis dvandersluis added the duplicate This issue or pull request already exists label Sep 9, 2021
@dvandersluis
Copy link
Member

Duplicate of #535.

@schinery
Copy link
Author

schinery commented Sep 9, 2021

Sorry my bad 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants