rubocop/rubocop · error · ArgumentError
Passing a project root directory to `inject_defaults!` is no
Error message
Passing a project root directory to `inject_defaults!` is no longer supported.
What it means
`RuboCop::ConfigLoader.inject_defaults!` merges a plugin's `default.yml` into RuboCop's default configuration. Older plugin ecosystems passed the gem's project root directory; the API now requires the exact YAML file path. Passing a directory first prints a deprecation warning, then raises `ArgumentError`. It is a testing/documentation API — with `rubocop/rspec/support` it is usually unneeded.
Source
Thrown at lib/rubocop/config_loader.rb:166
def default_configuration
@default_configuration ||= begin
print 'Default ' if debug?
load_file(DEFAULT_FILE)
end
end
# This API is primarily intended for testing and documenting plugins.
# When testing a plugin using `rubocop/rspec/support`, the plugin is loaded automatically,
# so this API is usually not needed. It is intended to be used only when implementing tests
# that do not use `rubocop/rspec/support`.
def inject_defaults!(config_yml_path)
if Pathname(config_yml_path).directory?
warn Rainbow(<<~MESSAGE).yellow, uplevel: 1
Use config YAML file path instead of project root directory.
e.g., `path/to/config/default.yml`
MESSAGE
raise ArgumentError,
'Passing a project root directory to `inject_defaults!` is no longer supported.'
end
path = config_yml_path.to_s
hash = ConfigLoader.load_yaml_configuration(path)
config = Config.new(hash, path).tap(&:make_excludes_absolute)
@default_configuration = ConfigLoader.merge_with_default(config, path)
end
# Returns the path RuboCop inferred as the root of the project. No file
# searches will go past this directory.
# @deprecated Use `RuboCop::ConfigFinder.project_root` instead.
def project_root
warn Rainbow(<<~WARNING).yellow, uplevel: 1
`RuboCop::ConfigLoader.project_root` is deprecated and will be removed in RuboCop 2.0. \
Use `RuboCop::ConfigFinder.project_root` instead.
WARNINGView on GitHub (pinned to ea712edf2b)
Solutions
- Pass the full path to the YAML file: `RuboCop::ConfigLoader.inject_defaults!(File.join(__dir__, 'config', 'default.yml'))`.
- If the call lives in a third-party plugin gem, upgrade it to a release compatible with current RuboCop.
- If your specs use `rubocop/rspec/support`, delete the `inject_defaults!` call entirely — the plugin loads automatically.
- The deprecation warning printed before the raise shows the exact expected form; mirror it.
Example fix
# before RuboCop::ConfigLoader.inject_defaults!(File.expand_path(__dir__)) # after RuboCop::ConfigLoader.inject_defaults!(File.join(__dir__, 'config', 'default.yml'))
Defensive patterns
Strategy: type-guard
Validate before calling
path = File.expand_path('config/default.yml', __dir__)
raise ArgumentError, 'pass the YAML file, not a directory' if File.directory?(path)
raise ArgumentError, 'missing default.yml' unless File.file?(path)
RuboCop::ConfigLoader.inject_defaults!(path) Type guard
def defaults_yaml_path?(path) path = Pathname(path) path.file? && path.extname == '.yml' end
Try / catch
begin RuboCop::ConfigLoader.inject_defaults!(config_path) rescue ArgumentError => e raise if e.message !~ /project root directory/ # retry with the YAML file inside the passed directory RuboCop::ConfigLoader.inject_defaults!(File.join(config_path, 'config', 'default.yml')) end
Prevention
- Always build the path to `config/default.yml` explicitly; never pass a directory to inject_defaults!.
- Prefer `rubocop/rspec/support` in extension specs — it loads plugins automatically and removes the need for inject_defaults!.
- Read the deprecation warning output; RuboCop prints the exact expected call form before raising.
- When maintaining a plugin, add a spec that exercises the support setup so API changes fail your build first.
When it happens
Trigger: Calling `RuboCop::ConfigLoader.inject_defaults!(File.expand_path(__dir__))` or `inject_defaults!(gem_root)` (a directory) instead of `inject_defaults!('path/to/config/default.yml')`; old README examples from rubocop-extension gems copied into a new plugin.
Common situations: Maintaining a rubocop plugin (e.g. rubocop-foo with its own cops) whose spec support predates the API change; upgrading RuboCop in a gem that still injects defaults via the directory convention.
Related errors
- Ambiguous cop name `%<name>s` used in %<origin>s needs depar
- Configuration file not found: #{absolute_path}
- can't inherit configuration from the rubocop gem
- (obsolete configuration found in #{smart_loaded_path}, pleas
- RuboCop found unknown Ruby version #{target_ruby_version.ins
AI-assisted analysis of rubocop/rubocop@ea712edf2b (2026-08-21).
Data as JSON: /api/errors/ae21ba7a12a11a79.
Report an issue: GitHub.