rmosolgo / graphql-ruby

Ruby implementation of GraphQL
http://graphql-ruby.org
MIT License
5.38k stars 1.39k forks source link

Rubocop errors on GraphQL/FieldTypeInBlock for array types #5105

Closed lasseebert closed 1 month ago

lasseebert commented 2 months ago

Describe the bug

Thanks for the fix for https://github.com/rmosolgo/graphql-ruby/issues/5097. I tried updating to the new version and bumped into another issue.

This is the minimal type I can replicate the bug with. It seems to only fail on array return types.

class Types::FooType < Types::BaseObject
  field :other, String
  field :bar, [String], null: false do
    argument :baz, String
  end
end

This fails with this. See full stack trace at the bottom.

$ bin/rubocop app/graphql/types/foo_type.rb
Inspecting 1 file
C

Offenses:

app/graphql/types/foo_type.rb:6:3: C: [Correctable] GraphQL/OrderedFields: Fields should be sorted in an alphabetical order within their section. Field bar should appear before other.
  field :bar, [String], null: false do ...
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense autocorrectable
An error occurred while GraphQL/FieldTypeInBlock cop was inspecting /home/me/code/myapp/apps/api/app/graphql/types/foo_type.rb:6:2.
To see the complete backtrace run rubocop -d.

1 error occurred:
An error occurred while GraphQL/FieldTypeInBlock cop was inspecting /home/me/code/myapp/apps/api/app/graphql/types/foo_type.rb:6:2.
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.66.1 (using Parser 3.3.5.0, rubocop-ast 1.32.3, running on ruby 3.3.5) +server [x86_64-linux]

Versions

graphql version: 2.3.16 rails (or other framework): 7.2.1

GraphQL schema

class Types::FooType < Types::BaseObject
  field :other, String
  field :bar, [String], null: false do
    argument :baz, String
  end
end

Steps to reproduce

Expected behavior

No exceptions

Actual behavior

An exception

Click to view exception backtrace ``` $ bin/rubocop app/graphql/types/foo_type.rb -d For /home/me/code/myapp/apps/api: configuration from /home/me/code/myapp/apps/api/.rubocop.yml Inheriting configuration from /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/test-prof-1.4.2/config/rubocop-rspec.yml Inheriting configuration from /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/syntax_tree-6.1.1/config/rubocop.yml Inheriting configuration from /home/me/code/myapp/apps/api/.rubocop_backlog.yml Inheriting configuration from /home/me/code/myapp/apps/api/.rubocop_todo.yml .rubocop.yml: Metrics/PerceivedComplexity:Exclude overrides the same parameter in .rubocop_backlog.yml Inspecting 1 file Scanning /home/me/code/myapp/apps/api/app/graphql/types/foo_type.rb Invariant: Something went wrong in GraphQL-Ruby, couldn't find surrounding class definition for field ((send nil :field (sym :bar) (array (const nil :String)) (hash (pair (sym :null) (false))))). Please report this error on GitHub. /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/graphql-2.3.16/lib/graphql/rubocop/graphql/field_type_in_block.rb:119:in `determine_field_indent' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/graphql-2.3.16/lib/graphql/rubocop/graphql/field_type_in_block.rb:65:in `block (2 levels) in on_send' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cop/base.rb:416:in `correct' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cop/base.rb:210:in `add_offense' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/graphql-2.3.16/lib/graphql/rubocop/graphql/field_type_in_block.rb:63:in `block in on_send' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/graphql-2.3.16/lib/graphql/rubocop/graphql/field_type_in_block.rb:33:in `field_config_with_inline_type' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/graphql-2.3.16/lib/graphql/rubocop/graphql/field_type_in_block.rb:57:in `on_send' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cop/commissioner.rb:107:in `public_send' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cop/commissioner.rb:107:in `block (2 levels) in trigger_responding_cops' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cop/commissioner.rb:171:in `with_cop_error_handling' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cop/commissioner.rb:106:in `block in trigger_responding_cops' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cop/commissioner.rb:105:in `each' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cop/commissioner.rb:105:in `trigger_responding_cops' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cop/commissioner.rb:69:in `on_send' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-ast-1.32.3/lib/rubocop/ast/traversal.rb:154:in `on_block' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cop/commissioner.rb:71:in `on_block' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-ast-1.32.3/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-ast-1.32.3/lib/rubocop/ast/traversal.rb:137:in `each' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-ast-1.32.3/lib/rubocop/ast/traversal.rb:137:in `on_dstr' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cop/commissioner.rb:71:in `on_begin' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-ast-1.32.3/lib/rubocop/ast/traversal.rb:154:in `on_class' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cop/commissioner.rb:71:in `on_class' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-ast-1.32.3/lib/rubocop/ast/traversal.rb:20:in `walk' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cop/commissioner.rb:87:in `investigate' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cop/team.rb:168:in `investigate_partial' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cop/team.rb:102:in `investigate' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/runner.rb:349:in `block in inspect_file' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/runner.rb:348:in `each' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/runner.rb:348:in `flat_map' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/runner.rb:348:in `inspect_file' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/runner.rb:291:in `block in do_inspection_loop' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/runner.rb:325:in `block in iterate_until_no_changes' :187:in `loop' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/runner.rb:318:in `iterate_until_no_changes' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/runner.rb:287:in `do_inspection_loop' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/runner.rb:168:in `block in file_offenses' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/runner.rb:193:in `file_offense_cache' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/runner.rb:167:in `file_offenses' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/runner.rb:158:in `process_file' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/runner.rb:139:in `block in each_inspected_file' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/runner.rb:138:in `each' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/runner.rb:138:in `reduce' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/runner.rb:138:in `each_inspected_file' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/runner.rb:124:in `inspect_files' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/runner.rb:77:in `run' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cli/command/execute_runner.rb:17:in `run' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cli/command.rb:11:in `run' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cli/environment.rb:18:in `run' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cli.rb:122:in `run_command' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cli.rb:129:in `execute_runners' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cli.rb:51:in `block in run' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cli.rb:81:in `profile_if_needed' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/cli.rb:43:in `run' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/server_command/exec.rb:22:in `run' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/server_command/base.rb:24:in `block in run' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/server_command/base.rb:23:in `chdir' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/server_command/base.rb:23:in `run' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/socket_reader.rb:33:in `block in read!' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/helper.rb:26:in `redirect' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/socket_reader.rb:28:in `read!' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/core.rb:90:in `read_socket' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/core.rb:65:in `block in process_input' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/cache.rb:150:in `write_pid_file' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/core.rb:64:in `process_input' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/core.rb:53:in `block in detach_server' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/core.rb:46:in `fork' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/core.rb:46:in `detach_server' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/core.rb:38:in `start' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/client_command/start.rb:42:in `block in run' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/cache.rb:137:in `acquire_lock' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/client_command/start.rb:29:in `run' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/client_command/exec.rb:40:in `ensure_server!' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/lib/rubocop/server/client_command/exec.rb:19:in `run' /home/me/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rubocop-1.66.1/exe/rubocop:12:in `' /home/me/code/myapp/apps/api/bin/rubocop:31:in `load' /home/me/code/myapp/apps/api/bin/rubocop:31:in `
' C Offenses: app/graphql/types/foo_type.rb:6:3: C: [Correctable] GraphQL/OrderedFields: Fields should be sorted in an alphabetical order within their section. Field bar should appear before other. field :bar, [String], null: false do ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1 file inspected, 1 offense detected, 1 offense autocorrectable configuration from /home/me/code/myapp/apps/api/.rubocop.yml An error occurred while GraphQL/FieldTypeInBlock cop was inspecting /home/me/code/myapp/apps/api/app/graphql/types/foo_type.rb:6:2. 1 error occurred: An error occurred while GraphQL/FieldTypeInBlock cop was inspecting /home/me/code/myapp/apps/api/app/graphql/types/foo_type.rb:6:2. 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.66.1 (using Parser 3.3.5.0, rubocop-ast 1.32.3, running on ruby 3.3.5) +server [x86_64-linux] ```
rmosolgo commented 1 month ago

Hey, thanks for reporting this and sorry for the trouble. I added a test case with the same definition in your example, but (unfortunately...) it passed: 5dcdb597c

I see there is another cop reporting an error in your example -- I wonder if these two are somehow conflicting. Could you share the source for that cop, or try disabling it locally to see if this example still fails?

lasseebert commented 1 month ago

When I clone this repo and try to run the tests, I seem to always get zero rubocop errors (even for the tests that asserts that there should be rubocop errors).

I tried to debug and figured out that the new fixture probably should be added to the config, right?

( I didn't solve the always-zero-errors thing above, but that is problably something in my setup)

lasseebert commented 1 month ago

Btw, the other cop reporting an error is a cop that sorts the fields of a graphql type alphabetically. I get the same error if they are sorted correctly.

rmosolgo commented 1 month ago

🤦 Derp, thanks for debugging that. I worked up a fix in #5107 (and improved my testing helpers to make sure I don't overlook that configuration issue next time)!

lasseebert commented 1 month ago

Thanks for the fix again! :tada: I tested on master and it fixed this problem.

This revealed another problem though. Will open another issue for that :grin: