rubocop / rubocop-rails

A RuboCop extension focused on enforcing Rails best practices and coding conventions.
https://docs.rubocop.org/rubocop-rails
MIT License
819 stars 263 forks source link

Enable non-default locations of checked files #234

Open ustimchik opened 4 years ago

ustimchik commented 4 years ago

When using engines, the path for controllers, models, etc. are different from the core application which forces gem users to enable "Include" for all cops with this configurable option.

It would be great if there was a possibility to have some configurable global parameters, for instance:

Rails:
  -controllers_path: '**/app/controllers'
  -models_path: '**/app/{models,decorators}'
ianyamey commented 4 years ago

@ustimchik: consider adding this to your .rubocop.yml:

inherit_mode:
    merge:
        - Exclude
        - Include

AllCops:
    Include:
        - lib/path/to/your/engines/**/*.rb

See: https://rubocop.readthedocs.io/en/latest/configuration/#unusual-files-that-would-not-be-included-by-default

iuriialekseev commented 4 years ago

@ianyamey, it seems that this config somehow conflicts with rubocop-rails default config:

Finds 36 Rails/HasAndBelongsToMany offences

require:
  - ./linters/custom_cops.rb
  - rubocop-rails

inherit_mode:
  merge:
    - Exclude
    - Include

Still finds 36 Rails/HasAndBelongsToMany offences

require:
  - ./linters/custom_cops.rb
  - rubocop-rails

inherit_mode:
  merge:
    - Exclude
    - Include

AllCops:
  Include:
    - 'plugins/**/*.rb'

Only with specific rule overwrite it finds 50 Rails/HasAndBelongsToMany offenses and actually applies this rule to plugins/**/*.rb files

require:
  - ./linters/custom_cops.rb
  - rubocop-rails

inherit_mode:
  merge:
    - Exclude
    - Include

Rails/HasAndBelongsToMany:
  Include:
    - 'plugins/**/*.rb'
jmuhlfel commented 2 years ago

I'm having this issue as well. It may be more straightforward to include engine paths (ex. engines/**/app/models/**/*) for the relevant cops in default.yml, instead of adding new config features.

hlbowen commented 1 year ago

I am also having this issue, not with Engines but with Packwerk, which uses a similar subfolder structure - in my case it looks like packs/**/app/models/**/*. I'd like to apply the same rubocop config to all of my packs, but when I put this config in a root level .rubocop.yml, rubocop-rails doesn't apply the model/controller/etc-specific cops to any files within packs.

The only solution I've found so far is to copy my root-level .rubocop.yml into every single pack directory (packs/**/.rubocop.yml). This isn't sustainable and I would love to find a better option.