#11011·rubocop

Be able to run rubocop in a "nested project" without picking up config in parent directories.

Author: timdigginsCreated Sep 23, 2022Updated Sep 20, 2026

I'm not 100% sure if this is a feature request or a bug report (see below). But here's the issue:

I have a project where I'm trying to add rubocop to a nested project (which is a dependency of the other project but can be run independently - they might be better set up as siblings, but for various reasons the "parent" project is in the parent directory). The rubocop in the nested project adds the configuration of the parent directory, including the "require" - but these are not present in the nested project.

I can exclude the nested project from the parent's rubocop (or include it -- either way are fine and controllable) however there's no way to say to rubocop - run it in this directory and don't reference the parent directory's .rubocp.yml.

Feature request or bug?

As was stated in #9879 :

... the configuration system doesn't have the concept of project root, only of ancestor directories

However #8314 seems to imply there is a concept of project root (and in fact that it shouldn't look in parent directories for .rubocop.yml)

I've used the sections from both feature request and bug report to cover all the bases

Describe the solution you'd like

I think rubocop should have some way of differentiating between traversing down to pick up new configs from a directory (which might well be its own project - it's up to the parent to sensibly include or exclude those directories) and traversing up to look for parent project's config.

When you invoke rubocop in a directory that has a .rubocop.yml and that is recognised as a project root (by the same tokens that bundler does as per #8314

There's would then be a difference between

  • running rubocop nested-project and
  • cd nested-project; rubocop`

Expected behavior

I've documented a toy example in https://github.com/timdiggins/rubocop-nested-test

The top level directory has a .rubocop.yml and excludes the nested-project. It requires 'rspec-performance', and the Gemfile specifies it also.

When you run rubocop in the top directory it works as expected.

The nested-project directory represents a project nested within the top level project (see below for why it might occur).

The nested-project's .rubocop.yml does not require 'rspec-performance', and the Gemfile does not specify it.

Expectation: cd nested-project; rubocop would run without error, using just the configuration from the nested-project/.rubocop.yml

Actual behavior

if you run rubocop in the nested-project directory, it fails because rspec-performance gem can't be found.

output of `bundle exec rubocop --debug` from nested-project
For /rubocop-nested-test/nested-project: configuration from /rubocop-nested-test/nested-project/.rubocop.yml
Default configuration from ...../gems/rubocop-1.36.0/config/default.yml
AllCops/Exclude configuration from /rubocop-nested-test/.rubocop.yml
cannot load such file -- rubocop-performance
...../gems/rubocop-1.36.0/lib/rubocop/feature_loader.rb:46:in `rescue in rescue in load'
...../gems/rubocop-1.36.0/lib/rubocop/feature_loader.rb:39:in `rescue in load'
...../gems/rubocop-1.36.0/lib/rubocop/feature_loader.rb:32:in `load'
...../gems/rubocop-1.36.0/lib/rubocop/feature_loader.rb:21:in `load'
...../gems/rubocop-1.36.0/lib/rubocop/config_loader_resolver.rb:14:in `block (2 levels) in resolve_requires'
...../gems/rubocop-1.36.0/lib/rubocop/config_loader_resolver.rb:13:in `each'
...../gems/rubocop-1.36.0/lib/rubocop/config_loader_resolver.rb:13:in `block in resolve_requires'
...../gems/rubocop-1.36.0/lib/rubocop/config_loader_resolver.rb:12:in `tap'
...../gems/rubocop-1.36.0/lib/rubocop/config_loader_resolver.rb:12:in `resolve_requires'
...../gems/rubocop-1.36.0/lib/rubocop/config_loader.rb:46:in `load_file'
...../gems/rubocop-1.36.0/lib/rubocop/config_loader.rb:130:in `add_excludes_from_files'
...../gems/rubocop-1.36.0/lib/rubocop/config_loader.rb:110:in `configuration_from_file'
...../gems/rubocop-1.36.0/lib/rubocop/config_store.rb:68:in `for_dir'
...../gems/rubocop-1.36.0/lib/rubocop/config_store.rb:47:in `for_pwd'
...../gems/rubocop-1.36.0/lib/rubocop/cli.rb:99:in `parallel_by_default!'
...../gems/rubocop-1.36.0/lib/rubocop/cli.rb:46:in `run'
...../gems/rubocop-1.36.0/exe/rubocop:19:in `block in <top (required)>'
...../lib/ruby/2.7.0/benchmark.rb:308:in `realtime'
...../gems/rubocop-1.36.0/exe/rubocop:19:in `<top (required)>'
...../bin/rubocop:25:in `load'
...../bin/rubocop:25:in `<main>'
...../bin/ruby_executable_hooks:22:in `eval'
...../bin/ruby_executable_hooks:22:in `<main>'

Steps to reproduce the problem

see above or see https://github.com/timdiggins/rubocop-nested-test

RuboCop version

╰─ bundle exec rubocop -V                                                                                              ─╯
1.36.0 (using Parser 3.1.2.1, rubocop-ast 1.21.0, running on ruby 2.7.5) [x86_64-darwin21]
  - rubocop-performance 1.15.0

Workarounds:

The main workaround is to run rubocop only from the parent project, and allow the nested-project to define a .rubocop.yml overriding config as and when appropriate, and never running rubocop when in the nested-project(and probably excluding rubocop from the nested-project's Gemfile).