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

class Foo; bar do will crash Metrics/BlockLength #12873

Closed
rellampec opened this issue Apr 29, 2024 · 2 comments · Fixed by #12874
Closed

class Foo; bar do will crash Metrics/BlockLength #12873

rellampec opened this issue Apr 29, 2024 · 2 comments · Fixed by #12874

Comments

@rellampec
Copy link

rellampec commented Apr 29, 2024

Running rubocop on Windows 11 against a file named (rubocop_fails.rb) with the content below:

class Foo
  bar do

    one_line
    one_line
    one_line
    one_line
    one_line
  end
end

The .rubocop.yml file (in the same folder) contains this:

AllCops:
  TargetRubyVersion: 2.7.2
  NewCops: enable

Metrics/BlockLength:
  CountAsOne: [config]
  Max: 5

Expected behavior

Rubocop should just say that the block is too long

Actual behavior

It displays the results of some cops an when inspecting Metrics/BlockLength fails with error:

1 error occurred:
An error occurred while Metrics/BlockLength cop was inspecting C:/test/rubocop_fails.rb:2:2.

Full result:

For C:/test: configuration from C:/test/.rubocop.yml
Default configuration from C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/config/default.yml
Inspecting 1 file
Scanning C:/test/rubocop_fails.rb
An error occurred while Metrics/BlockLength cop was inspecting C:/test/rubocop_fails.rb:2:2.
Unknown foldable type: :config. Valid foldable types are: array, hash, heredoc, send, csend.
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/metrics/utils/code_length_calculator.rb:54:in `block in build_foldable_checks'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/metrics/utils/code_length_calculator.rb:43:in `map'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/metrics/utils/code_length_calculator.rb:43:in `build_foldable_checks'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/metrics/utils/code_length_calculator.rb:20:in `initialize'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/mixin/code_length.rb:50:in `new'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/mixin/code_length.rb:50:in `build_code_length_calculator'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/mixin/code_length.rb:35:in `check_code_length'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/metrics/block_length.rb:56:in `on_block'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/commissioner.rb:107:in `public_send'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/commissioner.rb:107:in `block (2 levels) in trigger_responding_cops'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/commissioner.rb:171:in `with_cop_error_handling'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/commissioner.rb:106:in `block in trigger_responding_cops'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/commissioner.rb:105:in `each'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/commissioner.rb:105:in `trigger_responding_cops'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/commissioner.rb:69:in `on_block'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-ast-1.31.2/lib/rubocop/ast/traversal.rb:154:in `on_class'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/commissioner.rb:71:in `on_class'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-ast-1.31.2/lib/rubocop/ast/traversal.rb:20:in `walk'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/commissioner.rb:87:in `investigate'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/team.rb:156:in `investigate_partial'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cop/team.rb:98:in `investigate'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:349:in `block in inspect_file'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:348:in `each'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:348:in `flat_map'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:348:in `inspect_file'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:291:in `block in do_inspection_loop'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:325:in `block in iterate_until_no_changes'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:318:in `loop'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:318:in `iterate_until_no_changes'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:287:in `do_inspection_loop'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:168:in `block in file_offenses'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:193:in `file_offense_cache'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:167:in `file_offenses'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:158:in `process_file'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:139:in `block in each_inspected_file'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:138:in `each'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:138:in `reduce'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:138:in `each_inspected_file'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:124:in `inspect_files'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/runner.rb:77:in `run'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cli/command.rb:11:in `run'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cli/environment.rb:18:in `run'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cli.rb:118:in `run_command'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cli.rb:125:in `execute_runners'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cli.rb:51:in `block in run'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cli.rb:77:in `profile_if_needed'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/lib/rubocop/cli.rb:43:in `run'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/exe/rubocop:19:in `block in <top (required)>'
C:/ruby/Ruby27-x64/lib/ruby/2.7.0/benchmark.rb:308:in `realtime'
C:/ruby/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rubocop-1.63.4/exe/rubocop:19:in `<top (required)>'
C:/ruby/Ruby27-x64/bin/rubocop:32:in `load'
C:/ruby/Ruby27-x64/bin/rubocop:32:in `<main>'
C

Offenses:

rubocop_fails.rb:1:1: C: Style/Documentation: Missing top-level documentation comment for class Foo.
class Foo
^^^^^^^^^
rubocop_fails.rb:1:1: C: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
class Foo
^
rubocop_fails.rb:3:1: C: [Correctable] Layout/EmptyLinesAroundBlockBody: Extra empty line detected at block body beginning.

1 file inspected, 3 offenses detected, 2 offenses autocorrectable

1 error occurred:
An error occurred while Metrics/BlockLength cop was inspecting C:/test/rubocop_fails.rb:2:2.
configuration from C:/test/.rubocop.yml
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.63.4 (using Parser 3.3.1.0, rubocop-ast 1.31.2, running on ruby 2.7.2) [x64-mingw32]
Finished in 0.5653301000129431 seconds

Steps to reproduce the problem

Prepare the sample ruby file and the .rubocop.yml as outlined at the beginning of this report.
Next run:

> rubocop -d .\rubocop_fails.rb

You should see the same result as outlined in the Actual behaviour section above

RuboCop version

> rubocop -V
1.63.4 (using Parser 3.3.1.0, rubocop-ast 1.31.2, running on ruby 2.7.2) [x64-mingw32]
  • Note that this also fails on ruby 3.2.2 (tested)
@rellampec
Copy link
Author

rellampec commented Apr 29, 2024

Hmmm, not a bug, I think that the issue pointed in the error. It comes from the .rubocop.yml file itself (not from the ruby code).

Unknown foldable type: :config. Valid foldable types are: array, hash, heredoc, send, csend.

It's rather confusing it doesn't just crash when parsing the config file? (rather than throwing the error when actually inspecting the ruby code)

koic added a commit to koic/rubocop that referenced this issue Apr 29, 2024
Fix rubocop#12873.

This PR fixes an error for `Metrics/BlockLength` when the `CountAsOne` config is invalid.

## Before

Without the `-d` option, the cause is unclear:

```console
$ bundle exec rubocop --only Metrics/BlockLength
Inspecting 1 file
An error occurred while Metrics/BlockLength cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2.
To see the complete backtrace run rubocop -d.
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Metrics/BlockLength cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.63.4 (using Parser 3.3.0.5, rubocop-ast 1.31.2, running on ruby 3.3.0) [x86_64-darwin22]
```

When the `-d` option is used, hint appears hidden within the backtrace:

```console
$ bundle exec rubocop --only Metrics/BlockLength -d
For /Users/koic/src/github.com/koic/rubocop-issues/12873: configuration from /Users/koic/src/github.com/koic/rubocop-issues/12873/.rubocop.yml
Default configuration from /Users/koic/src/github.com/rubocop/rubocop/config/default.yml
AllCops/Exclude configuration from /Users/koic/src/github.com/koic/rubocop-issues/.rubocop.yml
configuration from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/rubocop-minitest-0.35.0/config/default.yml
configuration from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/rubocop-minitest-0.35.0/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-performance/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-performance/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-rails/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-rails/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb
An error occurred while Metrics/BlockLength cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2.
Unknown foldable type: :config. Valid foldable types are: array, hash, heredoc, send, csend.
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/metrics/utils/code_length_calculator.rb:54:in `block in build_foldable_checks'
```

## After

Even without the `-d` option, only the hint is displayed:

```console
$ bundle exec rubocop --only Metrics/BlockLength
Inspecting 1 file
Unknown foldable type: :config. Valid foldable types are: array, hash, heredoc, send, csend.
(from file: /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2)
.

1 file inspected, no offenses detected
```

This behavior is the same as when the `Notice` config in the `Style/Copyright` cop is not valid.
This clarifies what needs to be addressed first to achieve the expected behavior.
koic added a commit to koic/rubocop that referenced this issue Apr 29, 2024
Fix rubocop#12873.

This PR fixes an error for `Metrics/BlockLength` when the `CountAsOne` config is invalid.

## Before

Without the `-d` option, the cause is unclear:

```console
$ bundle exec rubocop --only Metrics/BlockLength
Inspecting 1 file
An error occurred while Metrics/BlockLength cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2.
To see the complete backtrace run rubocop -d.
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Metrics/BlockLength cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.63.4 (using Parser 3.3.0.5, rubocop-ast 1.31.2, running on ruby 3.3.0) [x86_64-darwin22]
```

When the `-d` option is used, hint appears hidden within the backtrace:

```console
$ bundle exec rubocop --only Metrics/BlockLength -d
For /Users/koic/src/github.com/koic/rubocop-issues/12873: configuration from /Users/koic/src/github.com/koic/rubocop-issues/12873/.rubocop.yml
Default configuration from /Users/koic/src/github.com/rubocop/rubocop/config/default.yml
AllCops/Exclude configuration from /Users/koic/src/github.com/koic/rubocop-issues/.rubocop.yml
configuration from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/rubocop-minitest-0.35.0/config/default.yml
configuration from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/rubocop-minitest-0.35.0/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-performance/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-performance/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-rails/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-rails/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb
An error occurred while Metrics/BlockLength cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2.
Unknown foldable type: :config. Valid foldable types are: array, hash, heredoc, send, csend.
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/metrics/utils/code_length_calculator.rb:54:in `block in build_foldable_checks'
```

## After

Even without the `-d` option, only the hint is displayed:

```console
$ bundle exec rubocop --only Metrics/BlockLength
Inspecting 1 file
Unknown foldable type: :config. Valid foldable types are: array, hash, heredoc, send, csend.
(from file: /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2)
.

1 file inspected, no offenses detected
```

This behavior is the same as when the `Notice` config in `Style/Copyright` cop is not valid.
This clarifies what needs to be addressed first to achieve the expected behavior.
@koic
Copy link
Member

koic commented Apr 29, 2024

Yes, it's not a bug, but I've opened issue #12874 to make some adjustments to the unclear parts.

koic added a commit to koic/rubocop that referenced this issue Apr 29, 2024
Fixes rubocop#12873.

This PR fixes an error for `Metrics/BlockLength` when the `CountAsOne` config is invalid.

## Before

Without the `-d` option, the cause is unclear:

```console
$ bundle exec rubocop --only Metrics/BlockLength
Inspecting 1 file
An error occurred while Metrics/BlockLength cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2.
To see the complete backtrace run rubocop -d.
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Metrics/BlockLength cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.63.4 (using Parser 3.3.0.5, rubocop-ast 1.31.2, running on ruby 3.3.0) [x86_64-darwin22]
```

When the `-d` option is used, hint appears hidden within the backtrace:

```console
$ bundle exec rubocop --only Metrics/BlockLength -d
For /Users/koic/src/github.com/koic/rubocop-issues/12873: configuration from /Users/koic/src/github.com/koic/rubocop-issues/12873/.rubocop.yml
Default configuration from /Users/koic/src/github.com/rubocop/rubocop/config/default.yml
AllCops/Exclude configuration from /Users/koic/src/github.com/koic/rubocop-issues/.rubocop.yml
configuration from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/rubocop-minitest-0.35.0/config/default.yml
configuration from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/rubocop-minitest-0.35.0/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-performance/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-performance/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-rails/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-rails/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb
An error occurred while Metrics/BlockLength cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2.
Unknown foldable type: :config. Valid foldable types are: array, hash, heredoc, send, csend.
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/metrics/utils/code_length_calculator.rb:54:in `block in build_foldable_checks'
```

## After

Even without the `-d` option, only the hint is displayed:

```console
$ bundle exec rubocop --only Metrics/BlockLength
Inspecting 1 file
Unknown foldable type: :config. Valid foldable types are: array, hash, heredoc, send, csend.
(from file: /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2)
.

1 file inspected, no offenses detected
```

This behavior is the same as when the `Notice` config in `Style/Copyright` cop is not valid.
This clarifies what needs to be addressed first to achieve the expected behavior.
koic added a commit that referenced this issue Apr 29, 2024
…ngth

[Fix #12873] Fix an error for `Metrics/BlockLength`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants