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

Crash with some formatters in v1.63.2 #12852

Closed
verdammelt opened this issue Apr 17, 2024 · 5 comments · Fixed by #12853
Closed

Crash with some formatters in v1.63.2 #12852

verdammelt opened this issue Apr 17, 2024 · 5 comments · Fixed by #12853

Comments

@verdammelt
Copy link

verdammelt commented Apr 17, 2024

After upgrading to v1.63.2 from 1.62.1 I found that the default formatter resulted in a crash:

...
142 files inspected, 135 offenses detected, 105 offenses autocorrectable
index 0 outside of array bounds: 0...0
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/parser-3.3.0.5/lib/parser/source/buffer.rb:274:in `fetch'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/parser-3.3.0.5/lib/parser/source/buffer.rb:274:in `source_line'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/parser-3.3.0.5/lib/parser/source/range.rb:126:in `source_line'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/formatter/clang_style_formatter.rb:34:in `valid_line?'
...

I went through all the documented formatters and found the following results:

| formatter   | crash? |
|-------------+--------|
| autogenconf | no     |
| clang       | yes    |
| emacs       | no     |
| files       | no     |
| fuubar      | yes    |
| github      | no     |
| html        | yes    |
| json        | no     |
| junit       | no     |
| markdown    | yes    |
| offenses    | no     |
| pacman      | yes    |
| progress    | yes    |
| quiet       | no     |
| simple      | no     |
| tap         | yes    |
| worst       | no     |

I then spot checked clang and markdown in previous versions. I found that clang started failing in 1.63.1 while markdown was failing since at least 1.62.1

Expected behavior

I expect rubocop not to crash when using some formatters.

Actual behavior

(not including --debug output here as the file is 2.1Mb... but I can provide that data if needed)

Rubocop will run and report errors for a while and then crash with an error: index 0 outside of array bounds: 0...0 and a stack trace:

/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/parser-3.3.0.5/lib/parser/source/buffer.rb:274:in `fetch'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/parser-3.3.0.5/lib/parser/source/buffer.rb:274:in `source_line'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/parser-3.3.0.5/lib/parser/source/range.rb:126:in `source_line'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/formatter/clang_style_formatter.rb:34:in `valid_line?'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/formatter/clang_style_formatter.rb:27:in `report_offense'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/formatter/clang_style_formatter.rb:12:in `block in report_file'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/formatter/clang_style_formatter.rb:12:in `each'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/formatter/clang_style_formatter.rb:12:in `report_file'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/formatter/simple_text_formatter.rb:33:in `file_finished'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/formatter/formatter_set.rb:51:in `block in file_finished'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/formatter/formatter_set.rb:51:in `each'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/formatter/formatter_set.rb:51:in `file_finished'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/runner.rb:250:in `file_finished'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/runner.rb:162:in `process_file'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/runner.rb:138:in `block in each_inspected_file'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/runner.rb:137:in `each'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/runner.rb:137:in `reduce'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/runner.rb:137:in `each_inspected_file'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/runner.rb:123:in `inspect_files'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/runner.rb:76:in `run'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/cli/command.rb:11:in `run'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/cli/environment.rb:18:in `run'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/cli.rb:118:in `run_command'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/cli.rb:125:in `execute_runners'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/cli.rb:51:in `block in run'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/cli.rb:77:in `profile_if_needed'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/lib/rubocop/cli.rb:43:in `run'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/exe/rubocop:19:in `block in <top (required)>'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/3.1.0/benchmark.rb:311:in `realtime'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/rubocop-1.63.2/exe/rubocop:19:in `<top (required)>'
/Users/mark/.rbenv/versions/3.1.3/bin/rubocop:25:in `load'
/Users/mark/.rbenv/versions/3.1.3/bin/rubocop:25:in `<top (required)>'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/3.1.0/bundler/cli/exec.rb:58:in `load'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/3.1.0/bundler/cli/exec.rb:58:in `kernel_load'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/3.1.0/bundler/cli/exec.rb:23:in `run'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/3.1.0/bundler/cli.rb:486:in `exec'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/3.1.0/bundler/cli.rb:31:in `dispatch'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/3.1.0/bundler/cli.rb:25:in `start'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/bundler-2.3.26/libexec/bundle:48:in `block in <top (required)>'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/3.1.0/bundler/friendly_errors.rb:120:in `with_friendly_errors'
/Users/mark/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/bundler-2.3.26/libexec/bundle:36:in `<top (required)>'
/Users/mark/.rbenv/versions/3.1.3/bin/bundle:25:in `load'
/Users/mark/.rbenv/versions/3.1.3/bin/bundle:25:in `<main>'

Most of the tested formatters that failed did have this failure coming from clang_style_formatter but markdown and html have failures starting in their formatters.

Steps to reproduce the problem

I am unsure which rule is triggering the failure but it seems that most commonly the error seen just before the crash is: Lint/EmptyFile: Empty file detected.

RuboCop version

> bundle exec rubocop -V
1.63.2 (using Parser 3.3.0.5, rubocop-ast 1.31.2, running on ruby 3.1.3) [arm64-darwin22]
  - rubocop-capybara 2.20.0
  - rubocop-factory_bot 2.25.1
  - rubocop-rails 2.24.1
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.29.1
@verdammelt
Copy link
Author

verdammelt commented Apr 17, 2024

I noticed when looking at the --debug output that the file with the Lint/EmptyFile error was being loaded from the rubocop cache. So I turned off the cache and rubocop ran successfully to completion.

So it would seem that perhaps this is problem with the data I have in my cache.

Is that sufficient resolution to this? Shall I just close the issue?

The contents of that cached file is:
[{"severity":"warning","location":{"begin_pos":0,"end_pos":0},"message":"Lint/EmptyFile: Empty file detected.","cop_name":"Lint/EmptyFile","status":"unsupported"}]

@Earlopain
Copy link
Contributor

I can reproduce this by creating a new empty file and running rubocop on it twice. Only the second run will error. Lint/EmptyFile creates a global offense which I presume doesn't roundtrip well. This is now exposed because of #12804.

@verdammelt
Copy link
Author

@Earlopain interestingly (to me at least) This problem only happens when caching is involved.

I moved aside my cache directory and ran rubocop with the debug flag. It ran fine and I could see my empty file was being scanned. I ran a few more times immediately and the next 2 or 3 times the file was still scanned but then the file was being picked up by the cache and rubocop was crashing.

@Earlopain
Copy link
Contributor

Check out the attached PR, that should fix this. Let me know if that works for you.

@verdammelt
Copy link
Author

Check out the attached PR, that should fix this. Let me know if that works for you.

This does appear to correct the problem.

Earlopain added a commit to Earlopain/rubocop that referenced this issue Apr 17, 2024
koic added a commit that referenced this issue Apr 17, 2024
[Fix #12852] Correctly deserialize a global offense
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