Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop caused by Layout/ArgumentAlignment -> Layout/HashAlignment #10671

Closed
Zopolis4 opened this issue May 26, 2022 · 5 comments · Fixed by #10672
Closed

Infinite loop caused by Layout/ArgumentAlignment -> Layout/HashAlignment #10671

Zopolis4 opened this issue May 26, 2022 · 5 comments · Fixed by #10672
Labels

Comments

@Zopolis4
Copy link

Expected behavior

I expected rubocop not to crash.

Actual behavior

Rubocop crashed on an infinite loop caused by Layout/ArgumentAlignment -> Layout/HashAlignment:

rubocop -a -c .rubocop.yml --debug
...
tools/check.rb:20:17: C: [Corrected] Layout/HashAlignment: Align the separators of a hash literal if they span more than one line.
                url: '',
                ^^^^^^^
tools/check.rb:20:22: C: [Corrected] Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line.
                     url: '',
                     ^^^^^^^
tools/check.rb:21:17: C: [Corrected] Layout/HashAlignment: Align the separators of a hash literal if they span more than one line.
                pattern: '',
                ^^^^^^^^^^^
tools/check.rb:21:18: C: [Corrected] Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line.
                 pattern: '',
                 ^^^^^^^^^^^
tools/check.rb:22:17: C: [Corrected] Layout/HashAlignment: Align the separators of a hash literal if they span more than one line.
                rest: ''
                ^^^^^^^^
tools/check.rb:22:21: C: [Corrected] Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line.
                    rest: ''
                    ^^^^^^^^
tools/check.rb:28:17: C: [Corrected] Layout/HashAlignment: Align the separators of a hash literal if they span more than one line.
                url: 'https://ftp.gnu.org/gnu/a2ps/',
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
tools/check.rb:1314:21: C: [Corrected] Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line.
                    rest: '| sort -t. -k 1,1nr -k 2,2nr -k 3,3nr -k 4,4nr | head -1'
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tools/check.rb:1320:17: C: [Corrected] Layout/HashAlignment: Align the separators of a hash literal if they span more than one line.
                url: 'https://ftp.gnu.org/gnu/units/',
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tools/check.rb:1320:22: C: [Corrected] Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line.
                     url: 'https://ftp.gnu.org/gnu/units/',
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tools/check.rb:1321:17: C: [Corrected] Layout/HashAlignment: Align the separators of a hash literal if they span more than one line.
                pattern: '\\-([\\d.]+)\\.tar\\.gz',
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tools/check.rb:1321:18: C: [Corrected] Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line.
                 pattern: '\\-([\\d.]+)\\.tar\\.gz',
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tools/check.rb:1322:17: C: [Corrected] Layout/HashAlignment: Align the separators of a hash literal if they span more than one line.
                rest: '| sort -t. -k 1,1nr -k 2,2nr -k 3,3nr -k 4,4nr | head -1'
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tools/check.rb:1322:21: C: [Corrected] Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line.
                    rest: '| sort -t. -k 1,1nr -k 2,2nr -k 3,3nr -k 4,4nr | head -1'
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tools/check.rb:1328:17: C: [Corrected] Layout/HashAlignment: Align the separators of a hash literal if they span more than one line.
                url: 'http://fungi.yuggoth.org/weather/src/',
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tools/check.rb:1328:22: C: [Corrected] Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line.
                     url: 'http://fungi.yuggoth.org/weather/src/',
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tools/check.rb:1329:17: C: [Corrected] Layout/HashAlignment: Align the separators of a hash literal if they span more than one line.
                pattern: '\\-([\\d.]+)\\.tar\\.gz',
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tools/check.rb:1329:18: C: [Corrected] Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line.
                 pattern: '\\-([\\d.]+)\\.tar\\.gz',
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tools/check.rb:1330:17: C: [Corrected] Layout/HashAlignment: Align the separators of a hash literal if they span more than one line.
                rest: '| sort -t. -k 1,1nr -k 2,2nr -k 3,3nr -k 4,4nr | head -1'
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tools/check.rb:1330:21: C: [Corrected] Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line.
                    rest: '| sort -t. -k 1,1nr -k 2,2nr -k 3,3nr -k 4,4nr | head -1'
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1890 files inspected, 1414 offenses detected, 954 offenses corrected
Infinite loop detected in /home/zopolis4/chromebrew/tools/check.rb and caused by Layout/HashAlignment -> Layout/ArgumentAlignment
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/runner.rb:296:in `check_for_infinite_loop'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/runner.rb:279:in `block in iterate_until_no_changes'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/runner.rb:278:in `loop'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/runner.rb:278:in `iterate_until_no_changes'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/runner.rb:247:in `do_inspection_loop'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/runner.rb:130:in `block in file_offenses'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/runner.rb:155:in `file_offense_cache'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/runner.rb:129:in `file_offenses'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/runner.rb:120:in `process_file'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/runner.rb:101:in `block in each_inspected_file'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/runner.rb:100:in `each'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/runner.rb:100:in `reduce'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/runner.rb:100:in `each_inspected_file'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/runner.rb:86:in `inspect_files'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/runner.rb:47:in `run'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/cli/command.rb:11:in `run'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/cli/environment.rb:18:in `run'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/cli.rb:71:in `run_command'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/cli.rb:78:in `execute_runners'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/lib/rubocop/cli.rb:47:in `run'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/exe/rubocop:12:in `block in <top (required)>'
/var/lib/gems/2.7.0/gems/benchmark-0.2.0/lib/benchmark.rb:311:in `realtime'
/var/lib/gems/2.7.0/gems/rubocop-1.30.0/exe/rubocop:12:in `<top (required)>'
/usr/local/bin/rubocop:23:in `load'
/usr/local/bin/rubocop:23:in `<main>'
Finished in 49.10484230400016 seconds

Some of the broken area of code in question:

desc 'Check for new version of a2png'
task :a2png do
  check_version pkg_name: 'a2png',
                url: '',
                pattern: '',
                rest: ''
end

desc 'Check for new version of a2ps'
task :a2ps do
  check_version pkg_name: 'a2ps',
                url: 'https://ftp.gnu.org/gnu/a2ps/',
                pattern: '\\-([\\d.]+)\\.tar\\.gz',
                rest: '| sort -t. -k 1,1nr -k 2,2nr -k 3,3nr -k 4,4nr | head -1'
end

The contents of the rubocop.yml file:

AllCops:
  NewCops: enable
  TargetRubyVersion: 3.0

Layout/HashAlignment:
  EnforcedHashRocketStyle:
    - separator
  EnforcedColonStyle:
    - separator

Layout/FirstHashElementIndentation:
  EnforcedStyle: consistent

Style/FrozenStringLiteralComment:
  Enabled: false

Style/Documentation:
  Enabled: false

Naming/VariableNumber:
  Enabled: false

Naming/ClassAndModuleCamelCase:
  Enabled: false

Metrics/MethodLength:
  Enabled: false

Layout/LineLength:
  AllowedPatterns: ['description']

RuboCop version

1.30.0 (using Parser 3.1.2.0, rubocop-ast 1.18.0, running on ruby 2.7.4 x86_64-linux-gnu
@koic koic added the bug label May 26, 2022
@ydah
Copy link
Member

ydah commented May 26, 2022

The following modifications seem to prevent this from occurring.

# Before
Layout/HashAlignment:
  EnforcedHashRocketStyle:
    - separator
  EnforcedColonStyle:
    - separator

# After
Layout/HashAlignment:
  EnforcedHashRocketStyle: separator
  EnforcedColonStyle:  separator

In the Before state, the comparison is always false because the comparison is made here between Array and string.

hash_argument_config[style] == 'separator'

@ydah
Copy link
Member

ydah commented May 26, 2022

I think the essential solution to this problem is to give Enforced***Style the same set value validation as EnforcedStyle.

For example, run rubocop with the following .rubocop.yml.

Style/EmptyMethod:
  EnforcedStyle:
    - expanded

Then I get a validation error as follows

$ bundle exec rubocop -a test.rb
For /ydah/sandbox: configuration from /ydah/sandbox/.rubocop.yml
Default configuration from /ydah/sandbox/vendor/bundle/ruby/3.1.0/gems/rubocop-1.30.0/config/default.yml
Error: invalid EnforcedStyle '["expanded"]' for Style/EmptyMethod found in .rubocop.yml
Valid choices are: compact, expanded

@koic
Copy link
Member

koic commented May 26, 2022

EnforcedColonStyle can return an array value because Layout/HashAlignment cop has AllowMultipleStyles config. I've opened #10672 to resolve the issue.

koic added a commit to koic/rubocop that referenced this issue May 26, 2022
…Alignment`

Fixes rubocop#10671.

This PR fixes an incorrect autocorrect for `EnforcedStyle: with_first_argument` of
`Layout/ArgumentAlignment` and `EnforcedColonStyle: separator` of `Layout/HashAlignment`.

`EnforcedColonStyle` can return an array value because `Layout/HashAlignment` cop has
`AllowMultipleStyles` config.
@ydah
Copy link
Member

ydah commented May 26, 2022

Oh, I was not aware of AllowMultipleStyles. Sorry. Thank you for explaining!

@koic
Copy link
Member

koic commented May 26, 2022

No problem. The setting you show work as a workaround 👍

# Before
Layout/HashAlignment:
  EnforcedHashRocketStyle:
    - separator
  EnforcedColonStyle:
    - separator

# After
Layout/HashAlignment:
  EnforcedHashRocketStyle: separator
  EnforcedColonStyle:  separator

koic added a commit that referenced this issue May 26, 2022
…out_argument_alignment

[Fix #10671] Fix an incorrect autocorrect for `Layout/ArgumentAlignment`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants