Skip to content

Commit

Permalink
[Fix #10921] Fix an error when processing configuration file
Browse files Browse the repository at this point in the history
Fixes #10921.

This PR fixes an error when ERB pre-processing of the configuration file.
  • Loading branch information
koic authored and bbatsov committed Aug 19, 2022
1 parent 04dd7ad commit ad4a8ca
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog/fix_an_error_when_using_erb_in_confi.md
@@ -0,0 +1 @@
* [#10921](https://github.com/rubocop/rubocop/issues/10921): Fix an error when ERB pre-processing of the configuration file. ([@koic][])
17 changes: 9 additions & 8 deletions lib/rubocop/server/cache.rb
Expand Up @@ -62,17 +62,18 @@ def cache_root_dir_from_config
# `RuboCop::ConfigStore` has heavy dependencies, this is a lightweight implementation
# so that only the necessary `CacheRootDirectory` can be obtained.
require 'yaml'

config_path = ConfigFinder.find_config_path(Dir.pwd)

# Ruby 3.1+
config_yaml = if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('4.0.0')
YAML.safe_load_file(config_path, permitted_classes: [Regexp, Symbol])
else
config = YAML.load_file(config_path)
require 'erb'
file_contents = File.read(config_path)
yaml_code = ERB.new(file_contents).result

config_yaml = YAML.safe_load(yaml_code, permitted_classes: [Regexp, Symbol])

config == false ? nil : config
end
# For compatibility with Ruby 3.0 or lower.
if Gem::Version.new(Psych::VERSION) < Gem::Version.new('4.0.0')
config_yaml == false ? nil : config_yaml
end

config_yaml&.dig('AllCops', 'CacheRootDirectory')
end
Expand Down
21 changes: 21 additions & 0 deletions spec/rubocop/server/cache_spec.rb
Expand Up @@ -219,5 +219,26 @@
end
end
end

context 'when ERB pre-processing of the configuration file', :isolated_environment do
context 'when cache root path is not specified path' do
before do
cache_class.cache_root_path = nil
end

it 'does not raise an error' do
create_file('.rubocop.yml', <<~YAML)
Style/Encoding:
Enabled: <%= 1 == 1 %>
Exclude:
<% Dir['*.rb'].sort.each do |name| %>
- <%= name %>
<% end %>
YAML

expect { cache_class.cache_path }.not_to raise_error
end
end
end
end
end

0 comments on commit ad4a8ca

Please sign in to comment.