Skip to content
New issue

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

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

Already on GitHub? # to your account

Error when running RSpec/MatchArray #1632

Closed
jcraigk opened this issue Apr 25, 2023 · 3 comments · Fixed by #1633
Closed

Error when running RSpec/MatchArray #1632

jcraigk opened this issue Apr 25, 2023 · 3 comments · Fixed by #1633

Comments

@jcraigk
Copy link

jcraigk commented Apr 25, 2023

An error is thrown when running RSpec/MatchArray against files in a large Rails project. For example:

~/code/rails-project ᐅ rubocop spec/services/widen/search_spec.rb
Inspecting 1 file
An error occurred while RSpec/MatchArray cop was inspecting /Users/user/code/rails-project/spec/services/widen/search_spec.rb:37:27.
To see the complete backtrace run rubocop -d.
An error occurred while RSpec/MatchArray cop was inspecting /Users/user/code/rails-project/spec/services/widen/search_spec.rb:48:25.
To see the complete backtrace run rubocop -d.
.

1 file inspected, no offenses detected

2 errors occurred:
An error occurred while RSpec/MatchArray cop was inspecting /Users/user/code/rails-project/spec/services/widen/search_spec.rb:37:27.
An error occurred while RSpec/MatchArray cop was inspecting /Users/user/code/rails-project/spec/services/widen/search_spec.rb:48:25.
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.50.2 (using Parser 3.2.2.1, rubocop-ast 1.28.0, running on ruby 3.2.2) [arm64-darwin22]

1 of the spec lines in question reads as follows: expect(subject).to match_array.

Here is a backtrace:

For /Users/user/code/rails-project: configuration from /Users/user/code/rails-project/.rubocop.yml
Inheriting configuration from /Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/wm-rubocop-0.77.0/default.yml
configuration from /Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-capybara-2.18.0/config/default.yml
Default configuration from /Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/config/default.yml
configuration from /Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-capybara-2.18.0/lib/../config/default.yml
configuration from /Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-performance-1.17.1/config/default.yml
configuration from /Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-performance-1.17.1/config/default.yml
configuration from /Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.19.1/config/default.yml
configuration from /Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.19.1/config/default.yml
configuration from /Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rake-0.6.0/config/default.yml
configuration from /Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rake-0.6.0/config/default.yml
configuration from /Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rspec-2.20.0/config/default.yml
configuration from /Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rspec-2.20.0/config/default.yml
Inheriting configuration from /Users/user/code/rails-project/.rubocop_todo.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /Users/user/code/rails-project/spec/services/widen/search_spec.rb
An error occurred while RSpec/MatchArray cop was inspecting /Users/user/code/rails-project/spec/services/widen/search_spec.rb:37:27.
undefined method `array_type?' for nil:NilClass
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rspec-2.20.0/lib/rubocop/cop/rspec/match_array.rb:37:in `on_send'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/commissioner.rb:143:in `public_send'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/commissioner.rb:143:in `block (2 levels) in trigger_restricted_cops'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/commissioner.rb:171:in `with_cop_error_handling'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/commissioner.rb:142:in `block in trigger_restricted_cops'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/commissioner.rb:141:in `each'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/commissioner.rb:141:in `trigger_restricted_cops'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/commissioner.rb:70:in `on_send'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:159:in `block in on_send'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:156:in `each'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:156:in `each_with_index'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:156:in `on_send'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/commissioner.rb:71:in `on_send'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:158:in `on_block'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/commissioner.rb:71:in `on_block'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:158:in `on_block'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/commissioner.rb:71:in `on_block'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:137:in `each'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:158:in `on_block'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/commissioner.rb:71:in `on_block'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:137:in `each'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:158:in `on_block'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/commissioner.rb:71:in `on_block'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:137:in `each'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-ast-1.28.0/lib/rubocop/ast/traversal.rb:20:in `walk'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/commissioner.rb:87:in `investigate'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/team.rb:156:in `investigate_partial'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/team.rb:98:in `investigate'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:345:in `block in inspect_file'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:344:in `each'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:344:in `flat_map'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:344:in `inspect_file'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:287:in `block in do_inspection_loop'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:321:in `block in iterate_until_no_changes'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:314:in `loop'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:314:in `iterate_until_no_changes'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:283:in `do_inspection_loop'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:164:in `block in file_offenses'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:189:in `file_offense_cache'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:163:in `file_offenses'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:154:in `process_file'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:135:in `block in each_inspected_file'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:134:in `each'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:134:in `reduce'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:134:in `each_inspected_file'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:120:in `inspect_files'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/runner.rb:73:in `run'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cli/command.rb:11:in `run'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cli/environment.rb:18:in `run'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cli.rb:118:in `run_command'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cli.rb:125:in `execute_runners'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cli.rb:51:in `block in run'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cli.rb:77:in `profile_if_needed'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cli.rb:43:in `run'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/exe/rubocop:19:in `block in <top (required)>'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/3.2.0/benchmark.rb:311:in `realtime'
/Users/user/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/exe/rubocop:19:in `<top (required)>'
/Users/user/.asdf/installs/ruby/3.2.2/bin/rubocop:25:in `load'
/Users/user/.asdf/installs/ruby/3.2.2/bin/rubocop:25:in `<main>'

The actual error seems to be undefined method 'array_type?' for nil:NilClass. The only way to stop these errors is to entirely disable the cop.

It appears that this occurs when the cop inspects lines where match_array is called without an argument. Note that this is permissible in Rspec and the method assumes an empty array if no argument is passed. The cop should probably assume the same.

Secondarily, the cop should probably recommend and auto-correct these to be_empty instead of match_array.

@pirj
Copy link
Member

pirj commented Apr 25, 2023 via email

@jcraigk
Copy link
Author

jcraigk commented Apr 25, 2023

Yes it's in my comment above. Here is the excerpt:

1 of the spec lines in question reads as follows: expect(subject).to match_array

And here is the explanation (again from above):

It appears that this occurs when the cop inspects lines where match_array is called without an argument.

@bwanicur-wm

This comment was marked as off-topic.

ydah added a commit to rubocop/rubocop-rspec_rails that referenced this issue Mar 27, 2024
ydah added a commit to rubocop/rubocop-rspec_rails that referenced this issue Mar 27, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants