-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Lint/EmptyFile causes error when I run RuboCop with --format=json
#8718
Comments
This issue will happen for any cop that registers an offense for a file rather than for a code range. I have a quick fix for this, however I'd like to discuss the potential impact and how exactly we'd like to fix this. The problem is straight forward, the formatter references The easiest solution seems to be to add diff --git a/lib/rubocop/cop/offense.rb b/lib/rubocop/cop/offense.rb
index 0da21b0fe..1d0adabee 100644
--- a/lib/rubocop/cop/offense.rb
+++ b/lib/rubocop/cop/offense.rb
@@ -63,10 +63,10 @@ module RuboCop
attr_reader :corrector
PseudoSourceRange = Struct.new(:line, :column, :source_line, :begin_pos,
- :end_pos)
+ :end_pos, :last_line, :last_column)
private_constant :PseudoSourceRange
- NO_LOCATION = PseudoSourceRange.new(1, 0, '', 0, 1).freeze
+ NO_LOCATION = PseudoSourceRange.new(1, 0, '', 0, 1, 0, 0).freeze
# @api private
def initialize(severity, location, message, cop_name, # rubocop:disable Metrics/ParameterLists
|
I found a workaround to avoid the error: disabling the via a configuration file: AllCops:
NewCops: enable
Lint/EmptyFile:
Enabled: false via CLI: rubocop --enable-pending-cops --except=Lint/EmptyFile This error occurs only on cops using the The cops are:
For third-party cops, I found the |
I retested on master - no errors. You, probably, would need to clear local rubocop cache, by |
👍
Not sure. Being lazy I'd do it on a need only basis. |
@fatkodima Thanks for your response, but I could not verify this issue was fixed via c70badc... 😕 # Reproduction for https://github.com/rubocop-hq/rubocop/issues/8718
#
# Usage:
# docker build -t rubocop-8718 .
# docker run --rm -it rubocop-8718
FROM ruby:2.7.1-buster
WORKDIR /work
RUN bundle init
RUN bundle add --git https://github.com/rubocop-hq/rubocop rubocop
RUN bundle list
RUN touch a.rb
ENTRYPOINT ["bundle", "exec", "rubocop", "a.rb", "--format=json", "--cache=false", "--enable-pending-cops"] $ docker build -t rubocop-8718 .
(...omitted...)
$ docker run --rm -it rubocop-8718
{"metadata":{"rubocop_version":"0.91.0","ruby_engine":"ruby","ruby_version":"2.7.1","ruby_patchlevel":"83","ruby_platform":"x86_64-linux"},"files":[],"summary":{"offense_count":0,"target_file_count":1,"inspected_file_count":0}}undefined method `last_line' for #<RuboCop::Cop::Offense::PseudoSourceRange:0x00005607779570c0>
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/cop/offense.rb:175:in `last_line'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/formatter/json_formatter.rb:70:in `hash_for_location'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/formatter/json_formatter.rb:61:in `hash_for_offense'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/formatter/json_formatter.rb:50:in `block in hash_for_file'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/formatter/json_formatter.rb:50:in `map'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/formatter/json_formatter.rb:50:in `hash_for_file'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/formatter/json_formatter.rb:28:in `file_finished'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/formatter/formatter_set.rb:49:in `block in file_finished'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/formatter/formatter_set.rb:49:in `each'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/formatter/formatter_set.rb:49:in `file_finished'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/runner.rb:214:in `file_finished'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/runner.rb:123:in `process_file'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/runner.rb:97:in `block in each_inspected_file'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/runner.rb:96:in `each'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/runner.rb:96:in `reduce'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/runner.rb:96:in `each_inspected_file'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/runner.rb:82:in `inspect_files'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/runner.rb:43:in `run'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/cli/command.rb:11:in `run'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/cli/environment.rb:18:in `run'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/cli.rb:65:in `run_command'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/cli.rb:72:in `execute_runners'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/lib/rubocop/cli.rb:41:in `run'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/exe/rubocop:13:in `block in <top (required)>'
/usr/local/lib/ruby/2.7.0/benchmark.rb:308:in `realtime'
/usr/local/bundle/bundler/gems/rubocop-c70badc16842/exe/rubocop:12:in `<top (required)>'
/usr/local/bundle/bin/rubocop:23:in `load'
/usr/local/bundle/bin/rubocop:23:in `<top (required)>'
/usr/local/lib/ruby/2.7.0/bundler/cli/exec.rb:63:in `load'
/usr/local/lib/ruby/2.7.0/bundler/cli/exec.rb:63:in `kernel_load'
/usr/local/lib/ruby/2.7.0/bundler/cli/exec.rb:28:in `run'
/usr/local/lib/ruby/2.7.0/bundler/cli.rb:476:in `exec'
/usr/local/lib/ruby/2.7.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/lib/ruby/2.7.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/usr/local/lib/ruby/2.7.0/bundler/vendor/thor/lib/thor.rb:399:in `dispatch'
/usr/local/lib/ruby/2.7.0/bundler/cli.rb:30:in `dispatch'
/usr/local/lib/ruby/2.7.0/bundler/vendor/thor/lib/thor/base.rb:476:in `start'
/usr/local/lib/ruby/2.7.0/bundler/cli.rb:24:in `start'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/libexec/bundle:46:in `block in <top (required)>'
/usr/local/lib/ruby/2.7.0/bundler/friendly_errors.rb:123:in `with_friendly_errors'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/libexec/bundle:34:in `<top (required)>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>' |
Same here. I updated this repo but can still reproduce the problem without cache: $ rm -rf ~/.cache/rubocop_cache && bundle exec rubocop --cache=false --format=json a.rb
{"metadata":{"rubocop_version":"0.91.0","ruby_engine":"ruby","ruby_version":"2.7.1","ruby_patchlevel":"83","ruby_platform":"x86_64-linux"},"files":[],"summary":{"offense_count":0,"target_file_count":1,"inspected_file_count":0}}undefined method `last_line' for #<RuboCop::Cop::Offense::PseudoSourceRange:0x0000562bbe567a30>
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/cop/offense.rb:175:in `last_line'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/formatter/json_formatter.rb:70:in `hash_for_location'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/formatter/json_formatter.rb:61:in `hash_for_offense'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/formatter/json_formatter.rb:50:in `block in hash_for_file'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/formatter/json_formatter.rb:50:in `map'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/formatter/json_formatter.rb:50:in `hash_for_file'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/formatter/json_formatter.rb:28:in `file_finished'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/formatter/formatter_set.rb:49:in `block in file_finished'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/formatter/formatter_set.rb:49:in `each'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/formatter/formatter_set.rb:49:in `file_finished'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/runner.rb:214:in `file_finished'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/runner.rb:123:in `process_file'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/runner.rb:97:in `block in each_inspected_file'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/runner.rb:96:in `each'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/runner.rb:96:in `reduce'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/runner.rb:96:in `each_inspected_file'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/runner.rb:82:in `inspect_files'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/runner.rb:43:in `run'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/cli/command.rb:11:in `run'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/cli/environment.rb:18:in `run'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/cli.rb:65:in `run_command'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/cli.rb:72:in `execute_runners'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/lib/rubocop/cli.rb:41:in `run'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/exe/rubocop:13:in `block in <top (required)>'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/2.7.0/benchmark.rb:308:in `realtime'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rubocop-c70badc16842/exe/rubocop:12:in `<top (required)>'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bin/rubocop:23:in `load'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bin/rubocop:23:in `<top (required)>'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:63:in `load'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:63:in `kernel_load'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:28:in `run'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/cli.rb:476:in `exec'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor.rb:399:in `dispatch'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/cli.rb:30:in `dispatch'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/base.rb:476:in `start'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/cli.rb:24:in `start'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/exe/bundle:46:in `block in <top (required)>'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/friendly_errors.rb:123:in `with_friendly_errors'
/home/kamei/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/exe/bundle:34:in `<top (required)>'
/home/kamei/.rbenv/versions/2.7.1/bin/bundle:23:in `load'
/home/kamei/.rbenv/versions/2.7.1/bin/bundle:23:in `<main>' |
Hi, I cannot update RuboCop to the latest version due to thi issue, I'd like to really fix it. The patch in #8718 (comment) by @rrosenblum seems good to me. Can I open a new PR to apply the patch? Or, is there anything else to consider? |
@ybiquitous Yes, the patch that I mentioned can be applied and opened as a PR to fix this. Maybe there is some sort of test case that we'll want to introduce to show the fixed behavior. Feel free to create the PR, otherwise I can take care of it. |
This change aims to fix the error due to undefined methods of `RuboCop::Cop::Offence::PseudoSourceRange`. The `RuboCop::Cop::Offence::PseudoSourceRange` class emulates the `Parser::Source::Range` class in the `parser` gem. (see <https://github.com/whitequark/parser/blob/v2.7.1.5/lib/parser/source/range.rb>) The `RuboCop::Cop::Offence#location` attribute can have an instance of both these classes. So, these classes need to have the same interface and semantics for the usage in RuboCop.
@rrosenblum Thanks for your help! I've opened the PR #8823. |
The combination with
Lint/EmptyFile
and the--format=json
option seems to cause error. Perhaps, this relates thePseudoSourceRange
here, but I'm not sure how to fix the problem.Expected behavior
RuboCop finishes the analysis without error.
Actual behavior
Error is thrown when processing
Lint/EmptyFile
.Steps to reproduce the problem
You can reproduce this problem with this repository.
bundle install
(Maybe, you should set up your Ruby with rbenv)bundle exec rubocop --cache=false --format=json a.rb
RuboCop version
Master: 03e83a2
The text was updated successfully, but these errors were encountered: