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

Make RuboCop Capybara work as a RuboCop plugin #144

Merged
merged 1 commit into from
Mar 6, 2025
Merged

Conversation

bquorning
Copy link
Contributor

@bquorning bquorning commented Feb 16, 2025

This pull request adds support for RuboCop's plugin feature, added in rubocop/rubocop#13792, released in v1.72.

It replaces the ad-hoc inject_defaults! with RuboCop plugins.

Some Rake tasks may still need to use inject_defaults!.


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

@bquorning
Copy link
Contributor Author

bquorning commented Feb 16, 2025

@koic I tried replicating your pull requests rubocop/rubocop-performance#490 and rubocop/rubocop-rspec#2042, but both locally and on CI I find that bundle exec rake internal_investigation is failing, while bundle exec rubocop will pass. I was hoping you could help me find out what is going wrong?

The error suggests that the rubocop-capybara cops is being run, even though they are not listed under require or plugins in .rubocop.yml.

I have a similar branch with similar changes in rubocop-factory_bot, and the same thing happens – exceptions from one of the FactoryBot cops, even though those cops should not be loaded. And it’s only a problem when running rake internal_investigation.

@bquorning bquorning marked this pull request as ready for review February 17, 2025 07:53
@bquorning bquorning requested a review from a team as a code owner February 17, 2025 07:53
@bquorning bquorning force-pushed the rubocop-plugin branch 2 times, most recently from 8895af6 to a974f3b Compare February 17, 2025 08:45
Rakefile Outdated
Comment on lines 26 to 28
RuboCop::RakeTask.new(:internal_investigation) do
RuboCop::ConfigLoader.inject_defaults!("#{__dir__}/config/default.yml")
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t understand why this is necessary, and hope to remove it before merging the PR.

@koic
Copy link
Member

koic commented Mar 5, 2025

Wouldn't using the plugin_name: 'rubocop-capybara' option in the CopsDocumentationGenerator, as in rubocop/rubocop-rspec#2057, resolve the issue?
This eliminates the need to use RuboCop::ConfigLoader.inject_defaults!, as I understand it.

@bquorning
Copy link
Contributor Author

Using plugin_name made tasks/cops_documentation.rake cleaner, but I still need to patch Rakefile (the 2nd commit) to make bundle exec rake internal_investigation work. Could you have another look please, @koic?

@koic
Copy link
Member

koic commented Mar 5, 2025

@bquorning

As a short answer, the following addition to .rubocop.yml is required.

diff --git a/.rubocop.yml b/.rubocop.yml
index 6507792..af0958b 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -1,6 +1,7 @@
 inherit_from: .rubocop_todo.yml

 plugins:
+  - rubocop-capybara
   - rubocop-performance
   - rubocop-rake
   - rubocop-rspec
diff --git a/Rakefile b/Rakefile
index 22cbb49..7455206 100644
--- a/Rakefile
+++ b/Rakefile
@@ -23,9 +23,7 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
 end

 desc 'Run RuboCop over this gem'
-RuboCop::RakeTask.new(:internal_investigation) do
-  RuboCop::ConfigLoader.inject_defaults!("#{__dir__}/config/default.yml")
-end
+RuboCop::RakeTask.new(:internal_investigation)

 desc 'Build config/default.yml'
 task :build_config do

Technical Note: This is due to the change from require to plugins.

With the previous the Inject hack, since require was used, loading the config/default.yml configuration was triggered by rubocop-capybara.

Now that it has become a plugin, config/default.yml is loaded based on the plugins specified in .rubocop.yml, with RuboCop as the starting trigger point.

Therefore, to load the configuration, .rubocop.yml must include plugins: rubocop-capybara. Since rubocop-capybara itself doesn't depend on Capybara, this may be considered redundant. However, from the user's perspective, it can also be seen as a necessary configuration example.

Additionally, rubocop-factory_bot rubocop/rubocop-factory_bot#139 is likely to be resolved in the same way. This issue specifically affects cops that provide options like EnforcedStyle in config/default.yml. In other words, cops that don't have configuration options in their configuration files will not encounter this error.

This pull request adds support for RuboCop's plugin feature, added in
rubocop/rubocop#13792, released in v1.72.

It replaces the ad-hoc `inject_defaults!` with RuboCop plugins.

Some Rake tasks may still need to use `inject_defaults!`.
@bquorning
Copy link
Contributor Author

Thank you @koic, that solved my problems.

koic added a commit to koic/rubocop-extension-generator that referenced this pull request Mar 5, 2025
…options

This PR prevents errors when internally using cops that rely on configuration options.

Please see here for details: rubocop/rubocop-capybara#144 (comment)
@bquorning
Copy link
Contributor Author

cc @ydah

Copy link
Member

@ydah ydah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@ydah ydah merged commit e07422d into main Mar 6, 2025
28 checks passed
@ydah ydah deleted the rubocop-plugin branch March 6, 2025 00:57
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.

3 participants