You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using a .rubocop.yml in the db/migrate folder to add specific rules for migrations (mainly disabling Metrics/MethodLength and Style/Documentation). It inherits from the project's root .rubocop.yml.
A couple of migrations had timestamps missing, and made it through CI to production. Specifically enabling Rails/CreateTableWithTimestamps in the migration-specific config file did nothing.
Experimentation showed that disabling any other cop in the migration-specific config file also disabled this cop — if the only things happening in the non-root config file were enabling cops or changing their settings, the missing timestamps were detected.
The same behaviour occurs if Rails/CreateTableWithTimestamps is commented out in db/migrate/.rubocop.yml on the grounds that it defaults to being enabled anyway.
Expected behaviour
Disabling other cops in a non-root .rubocop.yml that inherits from the root does not disable Rails/CreateTableWithTimestamps.
Actual behaviour
Rails/CreateTableWithTimestamps is only enabled in db/migrate if no other cops have Enabled: false set on them.
With debug output enabled, the output is as follows:
With no other cops disabled:
└─▷ bundle exec rubocop --debug --only Rails/CreateTableWithTimestamps db/migrate/*.rb
For /private/tmp/rubocoptest: configuration from /private/tmp/rubocoptest/.rubocop.yml
configuration from /Users/unikitty37/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-rails-2.6.0/config/default.yml
configuration from /Users/unikitty37/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-rails-2.6.0/config/default.yml
Default configuration from /Users/unikitty37/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-0.86.0/config/default.yml
Inspecting 1 file
Scanning /private/tmp/rubocoptest/db/migrate/20200705130640_create_pages.rb
For /private/tmp/rubocoptest/db/migrate: configuration from /private/tmp/rubocoptest/db/migrate/.rubocop.yml
Inheriting configuration from /private/tmp/rubocoptest/.rubocop.yml
AllCops/Exclude configuration from /private/tmp/rubocoptest/.rubocop.yml
C
Offenses:
db/migrate/20200705130640_create_pages.rb:3:5: C: Rails/CreateTableWithTimestamps: Add timestamps when creating a new table.
create_table :pages do |t| ...
^^^^^^^^^^^^^^^^^^^^^^^^^^
1 file inspected, 1 offense detected
Finished in 0.4792379999998957 seconds
With other cops disabled:
└─▷ bundle exec rubocop --debug --only Rails/CreateTableWithTimestamps db/migrate/*.rb
For /private/tmp/rubocoptest: configuration from /private/tmp/rubocoptest/.rubocop.yml
configuration from /Users/unikitty37/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-rails-2.6.0/config/default.yml
configuration from /Users/unikitty37/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-rails-2.6.0/config/default.yml
Default configuration from /Users/unikitty37/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-0.86.0/config/default.yml
Inspecting 1 file
Scanning /private/tmp/rubocoptest/db/migrate/20200705130640_create_pages.rb
For /private/tmp/rubocoptest/db/migrate: configuration from /private/tmp/rubocoptest/db/migrate/.rubocop.yml
Inheriting configuration from /private/tmp/rubocoptest/.rubocop.yml
AllCops/Exclude configuration from /private/tmp/rubocoptest/.rubocop.yml
.
1 file inspected, no offenses detected
Finished in 0.4813480000011623 seconds
I'm not sure of the specific reason why the cop gets run or not depending on whether you comment or uncomment other cops, but the reason for the cop being ignored is that the Include patterns are matched relatively to the configuration file.
So, in your case, the default Include pattern for this cop (db/migrate/*.rb) is getting matched against the relative path to the file from the configuration file enabling the cop. Since the configuration file lives in db/migrate/.rubocop.yml, it's getting matched against 20200705130640_create_pages.rb. And since db/migrate/*.rb doesn't match 20200705130640_create_pages.rb, the cop is ignored as not relevant.
This behaviour feels quite weird to me, since it forces you to override all default inclusion and exclusion patterns of all cops that you enable in a file that's not the root folder of your project.
In any case, you should be able to fix this, by either removing the nested configuration and move everything to the root of your project, or by overriding the inclusion pattern inside db/migrate/.rubocop.yml like this:
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!
I am using a
.rubocop.yml
in thedb/migrate
folder to add specific rules for migrations (mainly disablingMetrics/MethodLength
andStyle/Documentation
). It inherits from the project's root.rubocop.yml
.A couple of migrations had timestamps missing, and made it through CI to production. Specifically enabling
Rails/CreateTableWithTimestamps
in the migration-specific config file did nothing.Experimentation showed that disabling any other cop in the migration-specific config file also disabled this cop — if the only things happening in the non-root config file were enabling cops or changing their settings, the missing timestamps were detected.
The same behaviour occurs if
Rails/CreateTableWithTimestamps
is commented out indb/migrate/.rubocop.yml
on the grounds that it defaults to being enabled anyway.Expected behaviour
Disabling other cops in a non-root
.rubocop.yml
that inherits from the root does not disableRails/CreateTableWithTimestamps
.Actual behaviour
Rails/CreateTableWithTimestamps
is only enabled indb/migrate
if no other cops haveEnabled: false
set on them.With debug output enabled, the output is as follows:
With no other cops disabled:
With other cops disabled:
Steps to reproduce the problem
bundle exec rubocop --only Rails/CreateTableWithTimestamps db/migrate/*.rb
.db/migrate/.rubocop.yml
, uncomment either the block disablingMetrics/MethodLength
orStyle/Documentation
.bundle exec rubocop --only Rails/CreateTableWithTimestamps db/migrate/*.rb
.RuboCop version
Include the output of
rubocop -V
orbundle exec rubocop -V
if using Bundler. Here's an example:The text was updated successfully, but these errors were encountered: