Rubocop Indention/Layout of multiline array failing with Infinite Loop Error

74 views Asked by At

I am using rubocop to apply some auto-formatting to arrays that span multiple lines. I have seen the same error with rubocop 1.31.2 and rubocop 1.59.0

I have tried including and excluding each of these properties without success.

rubocop.yml

Layout/FirstArrayElementIndentation:
  Enabled: true
Layout/FirstArrayElementLineBreak:
  Enabled: true
Layout/ArrayAlignment:
  EnforcedStyle: with_fixed_indentation
Layout/MultilineArrayBraceLayout:
  EnforcedStyle: new_line

AllCops:
  NewCops: enable

robo.rb

# frozen_string_literal: true

# test an issue with robocop rules
class RoboTest
  def initialize
    puts(['aaaaaaaaaaa', 'bbbb', 'the quick brown fox jumped over the lazy dogs', 'one two three four five six seven eight nine ten' ])
  end
end

Run rubocop without autocorrect

bash-4.2$ rubocop robo.rb
Inspecting 1 file
C

Offenses:

robo.rb:6:121: C: [Correctable] Layout/LineLength: Line is too long. [135/120]
    puts(['aaaaaaaaaaa', 'bbbb', 'the quick brown fox jumped over the lazy dogs', 'one two three four five six seven eight nine ten' ])
                                                                                                                        ^^^^^^^^^^^^^^^
robo.rb:6:133: C: [Correctable] Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets.
    puts(['aaaaaaaaaaa', 'bbbb', 'the quick brown fox jumped over the lazy dogs', 'one two three four five six seven eight nine ten' ])
                                                                                                                                    ^

1 file inspected, 2 offenses detected, 2 offenses autocorrectable

Command fails with autocorrect is enabled

bash-4.2$ rubocop -a robo.rb
Inspecting 1 file
C

Offenses:

robo.rb:7:7: C: [Corrected] Layout/FirstArrayElementIndentation: Use 2 spaces for indentation in an array, relative to the first position after the preceding left parenthesis.
      'aaaaaaaaaaa', 'bbbb', 'the quick brown fox jumped over the lazy dogs',
      ^^^^^^^^^^^^^
robo.rb:7:12: C: [Corrected] Layout/ArrayAlignment: Use one level of indentation for elements following the first line of a multi-line array.
           'aaaaaaaaaaa', 'bbbb', 'the quick brown fox jumped over the lazy dogs',
           ^^^^^^^^^^^^^

0 files inspected, 2 offenses detected, 2 offenses corrected
Infinite loop detected in /home/me/mrt-cron/coll-health-obj-analysis/robo.rb and caused by Layout/FirstArrayElementIndentation -> Layout/ArrayAlignment
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:296:in `check_for_infinite_loop'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:279:in `block in iterate_until_no_changes'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:278:in `loop'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:278:in `iterate_until_no_changes'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:247:in `do_inspection_loop'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:130:in `block in file_offenses'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:155:in `file_offense_cache'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:129:in `file_offenses'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:120:in `process_file'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:101:in `block in each_inspected_file'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:100:in `each'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:100:in `reduce'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:100:in `each_inspected_file'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:86:in `inspect_files'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/runner.rb:47:in `run'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/cli/command.rb:11:in `run'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/cli/environment.rb:18:in `run'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/cli.rb:71:in `run_command'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/cli.rb:78:in `execute_runners'
/home/me/.gem/ruby/gems/rubocop-1.31.2/lib/rubocop/cli.rb:47:in `run'
/home/me/.gem/ruby/gems/rubocop-1.31.2/exe/rubocop:19:in `block in <top (required)>'
/usr/share/ruby/benchmark.rb:308:in `realtime'
/home/me/.gem/ruby/gems/rubocop-1.31.2/exe/rubocop:19:in `<top (required)>'
/home/me/bin/rubocop:23:in `load'
/home/me/bin/rubocop:23:in `<main>'

Interestingly, the issue does not occur when auto-correcting hashes

I am able to add the following rules to rubocop.yml

Layout/FirstHashElementIndentation:
  Enabled: true
  EnforcedStyle: consistent
Layout/MultilineHashKeyLineBreaks:
  Enabled: true
Layout/MultilineHashBraceLayout:
  EnforcedStyle: new_line

robohash.rb

# frozen_string_literal: true

# test an issue with robocop rules
class RoboHashTest
  def initialize
    puts({cat: 'aaaaaaaaaaa', bird: 'bbbb', fox: 'the quick brown fox jumped over the lazy dogs', count: 'one two three four five six seven eight nine ten' })
  end
end

Formatting robohash.rb

bash-4.2$ rubocop -a robohash.rb
Inspecting 1 file
C

Offenses:

robohash.rb:6:10: C: [Corrected] Layout/SpaceInsideHashLiteralBraces: Space inside { missing.
    puts({cat: 'aaaaaaaaaaa', bird: 'bbbb', fox: 'the quick brown fox jumped over the lazy dogs', count: 'one two three four five six seven eight nine ten' })
         ^
robohash.rb:6:99: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.
    puts({ cat: 'aaaaaaaaaaa', bird: 'bbbb', fox: 'the quick brown fox jumped over the lazy dogs', 
                                                                                                  ^
robohash.rb:6:121: C: [Corrected] Layout/LineLength: Line is too long. [158/120]
    puts({cat: 'aaaaaaaaaaa', bird: 'bbbb', fox: 'the quick brown fox jumped over the lazy dogs', count: 'one two three four five six seven eight nine ten' })
                                                                                                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
robohash.rb:7:1: C: [Corrected] Layout/HashAlignment: Align the keys of a hash literal if they span more than one line.
count: 'one two three four five six seven eight nine ten' })
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 4 offenses detected, 4 offenses corrected
0

There are 0 answers