-
Notifications
You must be signed in to change notification settings - Fork 47
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
Explore plugin configs #498
base: main
Are you sure you want to change the base?
Conversation
- RuboCop config requires only the RuboCop plugins whose gemspec is loaded - Each plugin has a corresponding file of defaults which are inherited - Labels are attached automatically to PRs touching each config file TBD: - Should we have a minimal config? Should we keep the existing config as is, and instead add an "everything.yml" config? - Should we test compatibility with _just_ RuboCop and no plugins? - What cops to enable? Maybe disallowing "pending" shouldn't apply to plugins, so we don't break consumers?
@@ -1 +1,8 @@ | |||
"config change": rubocop.yml | |||
"config change": rubocop*.yml | |||
"hq": rubocop.yml |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't sure of a good label name, so I went with the "cop" analogy of headquarters. 🤷
@@ -8,3 +8,12 @@ gem "diffy" | |||
gem "minitest" | |||
gem "pry-byebug" | |||
gem "rake" | |||
|
|||
group :plugins do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could use this to skip installing these in one of our CI runs, so we can ensure everything still works for consumers only using RuboCop.
Minitest: | ||
Enabled: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't bothered filling out most of these files, but presumably they'd look like:
PluginName:
Enabled: true
# ...config for cops where we want to diverge from the default...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could alternatively call these after the gem names (e.g. rubocop-minitest.yml
).
I just went with what we use in Core. 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to not use defaults. Let's always curate cops.
rubocop.yml
Outdated
<% | ||
# Identify installed RuboCop plugins to configure | ||
plugin_configs = { | ||
"rubocop-graphql" => "rubocop.graphql.yml", | ||
"rubocop-minitest" => "rubocop.minitest.yml", | ||
"rubocop-performance" => "rubocop.performance.yml", | ||
"rubocop-rails" => "rubocop.rails.yml", | ||
"rubocop-rake" => "rubocop.graphql.yml", | ||
"rubocop-rspec" => "rubocop.rspec.yml", | ||
}.select { |plugin_name, _| Gem.loaded_specs.include?(plugin_name) } | ||
%> | ||
|
||
require: <%= plugin_configs.keys.to_json %> | ||
|
||
inherit_from: <%= plugin_configs.values.to_json %> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is where the magic happens. 🪄 ✨ 👮♂️
Are there reasons why this would be a bad idea?
Pros
- Allows us to inject config without forcing the dependency on any plugins, or knowing anything fancy about the host app.
- Removes the need to separate gems (e.g.
rubocop-rails-shopify
or whatever). - Removes the need for consumers to opt-into the configs themselves. They can simply follow the suggestions from Rubocop, and as soon as they add a plugin it will "magically" be configured.
Cons
- Doesn't allow us to enforce the version of the plugins.
- This might get tricky as cops are added/removed.
- We would probably have to fallback to enforcing the version in ERB, which isn't ideal.
- This might get tricky as cops are added/removed.
- Could there be cases where it's unsafe to enable these?
- We could move the magic into a special file (e.g.
infer-rubocop.yml
), and keep this file simple, but that would mean consumers would have to know to switch. - Maybe it's sufficient to require consumers to simply override the config if it isn't compatible.
- We could move the magic into a special file (e.g.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tentatively moved this into rubocop.infer.yml
, since this repo itself ran into inapplicable cops. Although that may be a special case, so we can always put it back. If we went with this separate file, then it would mean consumers could opt into the magic
inherit_gem:
rubocop-shopify: rubocop.infer.yml # or perhaps we call it `rubocop.shopify.yml`
or stick with the current minimal approach:
inherit_gem
rubocop-shopify: rubocop.yml
or take a custom approach
inherit_gem
rubocop-shopify:
- rubocop.yml
- rubocop.rails.yml
"rubocop-rails" => "rubocop.rails.yml", | ||
"rubocop-rake" => "rubocop.graphql.yml", | ||
"rubocop-rspec" => "rubocop.rspec.yml", | ||
}.select { |plugin_name, _| Gem.loaded_specs.include?(plugin_name) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tricky part is that this checks if the gem is installed in the computer, not in the bundler right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. The way it would work is that if a consumer has
# .rubocop.yml
inherit_gem:
rubocop-shopify: rubocop.infer.yml
then when they run rubocop
it will look at their installed gems, and inherit from the applicable configs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But if the app doesn't have rubocop-minitest for example but the person has it installed globally we should not use it.
We should be using what is on the Gemfile.lock of the application, not what is on the gem paths.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, good point. I should have said "in their Gemfile" not "installed". I just confirmed Gem.loaded_specs
handles this the way we want:
% cat Gemfile
source 'https://rubygems.org'
% bundle exec ruby -e 'puts Gem.loaded_specs.key?("rubocop-minitest")'
false
% gem install rubocop-minitest
Successfully installed rubocop-minitest-0.32.2
Parsing documentation for rubocop-minitest-0.32.2
Installing ri documentation for rubocop-minitest-0.32.2
Done installing documentation for rubocop-minitest after 0 seconds
1 gem installed
% bundle exec ruby -e 'puts Gem.loaded_specs.key?("rubocop-minitest")'
false
% bundle add rubocop-minitest > /dev/null && cat Gemfile
source 'https://rubygems.org'
gem "rubocop-minitest", "~> 0.32.2"
% bundle exec ruby -e 'puts Gem.loaded_specs.key?("rubocop-minitest")'
true
% bundle exec ruby -e 'puts Gem.loaded_specs.key?("rubocop")'
true
This PR explores the idea of us also providing configs for other RuboCop plugins. Currently teams at Shopify configure these independently, but maybe we want to take a stance on some of the cops.
TBD: