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

Qualify Struct reference to avoid collisions with possible cop namespace #1135

Merged
merged 1 commit into from
Sep 29, 2023

Conversation

technicalpickles
Copy link
Contributor

@technicalpickles technicalpickles commented Sep 29, 2023

We were trying to add a custom cop called Struct/RequiresTypedStructHelper, but ran into a problem where we got an error like:

undefined method `new' for RuboCop::Cop::Struct:Module
/Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop/cop/mixin/index_method.rb:105:in `module:IndexMethod'
/Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop/cop/mixin/index_method.rb:6:in `module:Cop'
/Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop/cop/mixin/index_method.rb:4:in `module:RuboCop'
/Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop/cop/mixin/index_method.rb:3:in `top (required)'
/Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop/cop/rails_cops.rb:7:in `require_relative'
/Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop/cop/rails_cops.rb:7:in `top (required)'
/Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop-rails.rb:16:in `require_relative'

It turned out this module has method that were calling Struct.new, but Ruby constant lookup ended up using our Rubocop::Cop::Struct, instead of the actual Struct class.

We were able to work around it by changing the constant name, but Struct is always going to be part of the stdlib so a possible name collision.

Replace this text with a summary of the changes in your PR.
The more detailed you are, the better.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

We were trying to add a custom cop called `Struct/RequiresTypedStructHelper`, but ran into a problem where we got an error like:

```
undefined method `new' for RuboCop::Cop::Struct:Module
/Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop/cop/mixin/index_method.rb:105:in `module:IndexMethod'
/Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop/cop/mixin/index_method.rb:6:in `module:Cop'
/Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop/cop/mixin/index_method.rb:4:in `module:RuboCop'
/Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop/cop/mixin/index_method.rb:3:in `top (required)'
/Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop/cop/rails_cops.rb:7:in `require_relative'
/Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop/cop/rails_cops.rb:7:in `top (required)'
/Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop-rails.rb:16:in `require_relative'
```

It turned out this module has method that were calling `Struct.new`, but Ruby
constant lookup ended up using our `Rubocop::Cop::Struct`, instead of the actual
`Struct` class.

We were able to work around it by changing the constant name, but
`Struct` is always going to be part of the stdlib so a possible name
collision.
@technicalpickles
Copy link
Contributor Author

Added tests.

The main way I can think to test this is to have a new cop that uses the Struct namespace, and is setup to run. I wasn't able to figure out how to set that up just for testing though.

Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

I don't think this is user observable, unless you tried to use make a cop with the Struct namespace.

@koic koic merged commit 8d63d0e into rubocop:master Sep 29, 2023
@koic
Copy link
Member

koic commented Sep 29, 2023

Thanks!

@technicalpickles technicalpickles deleted the qualify-struct branch October 2, 2023 22:22
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 this pull request may close these issues.

2 participants