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

NoMethodError is raised from Rails/UniqueValidationWithoutIndex cop when db/schema.rb is empty #1036

Closed
tk0miya opened this issue Jun 23, 2023 · 1 comment · Fixed by #1038

Comments

@tk0miya
Copy link
Contributor

tk0miya commented Jun 23, 2023

Expected behavior

rubocop will be succeeded with no errors.

Actual behavior

Since rubocop-rails-2.20.1, I got the following error on running rubocop:

An error occurred while Rails/UniqueValidationWithoutIndex cop was inspecting /app/.../...rb:NNN:NNN.

Note: I got the same result when I used the latest release; rubocop-rails-2.20.2.

This is the stacktrace when I gave -d option to rubocop:

An error occurred while Rails/UniqueValidationWithoutIndex cop was inspecting /app/.../...rb:NNN:NNN.
undefined method `type' for nil:NilClass
/usr/local/bundle/gems/rubocop-rails-2.20.1/lib/rubocop/rails/schema_loader/schema.rb:47:in `each_table'
/usr/local/bundle/gems/rubocop-rails-2.20.1/lib/rubocop/rails/schema_loader/schema.rb:34:in `build!'
/usr/local/bundle/gems/rubocop-rails-2.20.1/lib/rubocop/rails/schema_loader/schema.rb:14:in `initialize'
/usr/local/bundle/gems/rubocop-rails-2.20.1/lib/rubocop/rails/schema_loader.rb:46:in `new'
/usr/local/bundle/gems/rubocop-rails-2.20.1/lib/rubocop/rails/schema_loader.rb:46:in `load!'
/usr/local/bundle/gems/rubocop-rails-2.20.1/lib/rubocop/rails/schema_loader.rb:18:in `load'
/usr/local/bundle/gems/rubocop-rails-2.20.1/lib/rubocop/cop/mixin/active_record_helper.rb:42:in `schema'
/usr/local/bundle/gems/rubocop-rails-2.20.1/lib/rubocop/cop/rails/unique_validation_without_index.rb:36:in `on_send'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:143:in `public_send'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:143:in `block (2 levels) in trigger_restricted_cops'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:171:in `with_cop_error_handling'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:142:in `block in trigger_restricted_cops'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:141:in `each'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:141:in `trigger_restricted_cops'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:70:in `on_send'
/usr/local/bundle/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
/usr/local/bundle/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:137:in `each'
/usr/local/bundle/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/usr/local/bundle/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:154:in `on_class'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:71:in `on_class'
/usr/local/bundle/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:20:in `walk'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:87:in `investigate'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cop/team.rb:156:in `investigate_partial'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cop/team.rb:98:in `investigate'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:345:in `block in inspect_file'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:344:in `each'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:344:in `flat_map'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:344:in `inspect_file'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:287:in `block in do_inspection_loop'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:321:in `block in iterate_until_no_changes'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:314:in `loop'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:314:in `iterate_until_no_changes'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:283:in `do_inspection_loop'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:164:in `block in file_offenses'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:189:in `file_offense_cache'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:163:in `file_offenses'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:154:in `process_file'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:135:in `block in each_inspected_file'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:134:in `each'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:134:in `reduce'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:134:in `each_inspected_file'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:120:in `inspect_files'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/runner.rb:73:in `run'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cli/command.rb:11:in `run'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cli/environment.rb:18:in `run'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cli.rb:118:in `run_command'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cli.rb:125:in `execute_runners'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cli.rb:51:in `block in run'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cli.rb:77:in `profile_if_needed'
/usr/local/bundle/gems/rubocop-1.52.1/lib/rubocop/cli.rb:43:in `run'
/usr/local/bundle/gems/rubocop-1.52.1/exe/rubocop:19:in `block in <top (required)>'
/usr/local/bundle/gems/benchmark-0.2.1/lib/benchmark.rb:311:in `realtime'
/usr/local/bundle/gems/rubocop-1.52.1/exe/rubocop:19:in `<top (required)>'
/usr/local/bundle/bin/rubocop:23:in `load'
/usr/local/bundle/bin/rubocop:23:in `<top (required)>'
/usr/local/bundle/gems/bundler-2.2.28/lib/bundler/cli/exec.rb:58:in `load'
/usr/local/bundle/gems/bundler-2.2.28/lib/bundler/cli/exec.rb:58:in `kernel_load'
/usr/local/bundle/gems/bundler-2.2.28/lib/bundler/cli/exec.rb:23:in `run'
/usr/local/bundle/gems/bundler-2.2.28/lib/bundler/cli.rb:478:in `exec'
/usr/local/bundle/gems/bundler-2.2.28/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/bundle/gems/bundler-2.2.28/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/usr/local/bundle/gems/bundler-2.2.28/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
/usr/local/bundle/gems/bundler-2.2.28/lib/bundler/cli.rb:31:in `dispatch'
/usr/local/bundle/gems/bundler-2.2.28/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
/usr/local/bundle/gems/bundler-2.2.28/lib/bundler/cli.rb:25:in `start'
/usr/local/bundle/gems/bundler-2.2.28/exe/bundle:49:in `block in <top (required)>'
/usr/local/bundle/gems/bundler-2.2.28/lib/bundler/friendly_errors.rb:128:in `with_friendly_errors'
/usr/local/bundle/gems/bundler-2.2.28/exe/bundle:37:in `<top (required)>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>'
.

1 file inspected, no offenses detected

Steps to reproduce the problem

Sorry, I can't find a way to reproduce it from scratch.

But it seems the error came from the empty db/scheme.rb.

In my application, it does not have any tables:

ActiveRecord::Schema.define(version: 0) do

end

And, the error will be resolved when I added the following line to rubocop-rails/lib/rubocop/rails/schema_loader/schema.rb:

          def build!(ast)
            raise "Unexpected type: #{ast.type}" unless ast.block_type?
+           return unless ast.body

            each_table(ast) do |table_def|
              next unless table_def.method?(:create_table)

              @tables << Table.new(table_def)
            end

Therefore, I think this is a bug of the schema loader, not Rails/UniqueValidationWithoutIndex cop's.

RuboCop version

root@e26db528aef8> bundle exec rubocop -V
1.52.1 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 2.7.5) [x86_64-linux]
  - rubocop-factory_bot 2.23.1
  - rubocop-performance 1.18.0
  - rubocop-rails 2.20.1
  - rubocop-rspec 2.22.0
@tk0miya
Copy link
Contributor Author

tk0miya commented Jun 30, 2023

Thanks!

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 a pull request may close this issue.

1 participant