Skip to content

Rubocop BackgroundMigration/DictionaryFile caches incorrect results

The cop Background/MigrationDictionaryFile runs against batched background migrations in db/post_migrate. For a given Ruby migration file it checks the contents of the related YAML file in db/docs/batched_background_migrations/. Usually if the cop fails on the Ruby file, the fix will need to be applied in the related YAML file.

Rubocop caches results based on a SHA of the Ruby file contents. Unfortunately this will not change if a change is made to the related YAML file. There are reports of flaky false positives from lefthook (internal slack discussion) which may be caused by this.

Steps to reproduce

Run rubocop to warm the cache for the file.

$ bundle exec rubocop db/post_migrate/20240113071052_queue_populate_topics_slug_column.rb
Inspecting 1 file
.

1 file inspected, no offenses detected

Remove the related YAML file

$ rm db/docs/batched_background_migrations/populate_topics_slug_column.yml

The cop should fail now, but doesn't because the Ruby file has not been modified

$ bundle exec rubocop db/post_migrate/20240113071052_queue_populate_topics_slug_column.rb
Inspecting 1 file
.

1 file inspected, no offenses detected

The cop will fail if run without caching

$ bundle exec rubocop --cache=false db/post_migrate/20240113071052_queue_populate_topics_slug_column.rb
Inspecting 1 file
C

Offenses:

db/post_migrate/20240113071052_queue_populate_topics_slug_column.rb:3:1: C: BackgroundMigration/DictionaryFile: Missing /home/malc/gitlab/gitlab-development-kit/gitlab/db/docs/batched_background_migrations/populate_topics_slug_column.yml. Use the generator 'batched_background_migration' to create dictionary files automatically. For more details refer: https://docs.gitlab.com/ee/development/database/batched_background_migrations.html#generator
class QueuePopulateTopicsSlugColumn < Gitlab::Database::Migration[2.2] ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected