diff --git a/changelog/fix_unknown_env_causing_cache_issues.md b/changelog/fix_unknown_env_causing_cache_issues.md new file mode 100644 index 0000000000..f3e6469fa4 --- /dev/null +++ b/changelog/fix_unknown_env_causing_cache_issues.md @@ -0,0 +1 @@ +* [#1260](https://github.com/rubocop/rubocop-rails/issues/1260): Fix a performance regression caused by `Rails/UnknownEnv` when using Rails 7.1. ([@lukasfroehlich1][]) diff --git a/lib/rubocop/cop/rails/unknown_env.rb b/lib/rubocop/cop/rails/unknown_env.rb index ebb62ce908..873c2c3177 100644 --- a/lib/rubocop/cop/rails/unknown_env.rb +++ b/lib/rubocop/cop/rails/unknown_env.rb @@ -87,7 +87,7 @@ def unknown_env_name?(name) def environments @environments ||= begin - environments = cop_config['Environments'] || [] + environments = cop_config['Environments'].dup || [] environments << 'local' if target_rails_version >= 7.1 environments end diff --git a/spec/rubocop/cop/rails/unknown_env_spec.rb b/spec/rubocop/cop/rails/unknown_env_spec.rb index 1965000916..660ada1a44 100644 --- a/spec/rubocop/cop/rails/unknown_env_spec.rb +++ b/spec/rubocop/cop/rails/unknown_env_spec.rb @@ -92,6 +92,15 @@ RUBY end + it 'does not mutate the cop config' do + expect_no_offenses(<<~RUBY) + Rails.env.local? + Rails.env == 'local' + RUBY + + expect(cop_config['Environments'].include?('local')).to be(false) + end + context 'when `Environments` is nil' do let(:cop_config) do {