To not to throw any error but show warning or style errors if any
Throws an error and is not able to continue to check for style problems
Any addition of code to one my model files in Rails is throwing an error
When I run bundle exec rubocop -a --fail-level E
I get
91 files inspected, 402 offenses detected, 21 offenses corrected
comparison of Parser::Source::Range with Parser::Source::Range failed
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/parser-2.5.1.0/lib/parser/source/tree_rewriter/action.rb:38:in `sort_by'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/parser-2.5.1.0/lib/parser/source/tree_rewriter/action.rb:38:in `ordered_replacements'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/parser-2.5.1.0/lib/parser/source/tree_rewriter.rb:191:in `process'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/cop/corrector.rb:68:in `rewrite'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/cop/team.rb:126:in `autocorrect_all_cops'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/cop/team.rb:70:in `autocorrect'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/cop/team.rb:98:in `block in offenses'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/cop/team.rb:115:in `investigate'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/cop/team.rb:94:in `offenses'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/cop/team.rb:44:in `inspect_file'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/runner.rb:265:in `inspect_file'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/runner.rb:212:in `block in do_inspection_loop'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/runner.rb:244:in `block in iterate_until_no_changes'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/runner.rb:237:in `loop'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/runner.rb:237:in `iterate_until_no_changes'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/runner.rb:208:in `do_inspection_loop'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/runner.rb:111:in `block in file_offenses'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/runner.rb:129:in `file_offense_cache'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/runner.rb:109:in `file_offenses'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/runner.rb:100:in `process_file'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/runner.rb:78:in `block in each_inspected_file'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/runner.rb:75:in `each'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/runner.rb:75:in `reduce'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/runner.rb:75:in `each_inspected_file'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/runner.rb:67:in `inspect_files'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/runner.rb:39:in `run'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/cli.rb:160:in `execute_runner'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/cli.rb:88:in `execute_runners'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/lib/rubocop/cli.rb:41:in `run'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/exe/rubocop:13:in `block in <top (required)>'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/2.4.0/benchmark.rb:308:in `realtime'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rubocop-0.57.1/exe/rubocop:12:in `<top (required)>'
/Users/piktochart/.rbenv/versions/2.4.2/bin/rubocop:23:in `load'
/Users/piktochart/.rbenv/versions/2.4.2/bin/rubocop:23:in `<top (required)>'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.16.2/lib/bundler/cli/exec.rb:74:in `load'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.16.2/lib/bundler/cli/exec.rb:74:in `kernel_load'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.16.2/lib/bundler/cli/exec.rb:28:in `run'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.16.2/lib/bundler/cli.rb:424:in `exec'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.16.2/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.16.2/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.16.2/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.16.2/lib/bundler/cli.rb:27:in `dispatch'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.16.2/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.16.2/lib/bundler/cli.rb:18:in `start'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.16.2/exe/bundle:30:in `block in <top (required)>'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.16.2/lib/bundler/friendly_errors.rb:124:in `with_friendly_errors'
/Users/piktochart/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/bundler-1.16.2/exe/bundle:22:in `<top (required)>'
/Users/piktochart/.rbenv/versions/2.4.2/bin/bundle:23:in `load'
/Users/piktochart/.rbenv/versions/2.4.2/bin/bundle:23:in `<main>'
$ rubocop -V
0.57.1 (using Parser 2.5.1.0, running on ruby 2.4.2 x86_64-darwin17)
Same here with:
$ rubocop -V
0.57.2 (using Parser 2.5.1.0, running on ruby 2.4.4 x86_64-linux)
I've found that this error occurs when both the first and last lines of the target file are empty/blank.
$ ls -a
. .. sample.rb
$ cat sample.rb
puts 'foo'
$ rubocop -a -d sample.rb
For /tmp/tes: configuration from ~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/config/default.yml
Inheriting configuration from ~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/config/enabled.yml
Inheriting configuration from ~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/config/disabled.yml
Inspecting 1 file
Scanning /tmp/tes/sample.rb
0 files inspected, no offenses detected
comparison of Parser::Source::Range with Parser::Source::Range failed
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/parser-2.5.1.0/lib/parser/source/tree_rewriter/action.rb:38:in `sort_by'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/parser-2.5.1.0/lib/parser/source/tree_rewriter/action.rb:38:in `ordered_replacements'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/parser-2.5.1.0/lib/parser/source/tree_rewriter.rb:191:in `process'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/corrector.rb:68:in `rewrite'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/team.rb:126:in `autocorrect_all_cops'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/team.rb:70:in `autocorrect'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/team.rb:98:in `block in offenses'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/team.rb:115:in `investigate'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/team.rb:94:in `offenses'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/team.rb:44:in `inspect_file'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/runner.rb:265:in `inspect_file'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/runner.rb:212:in `block in do_inspection_loop'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/runner.rb:244:in `block in iterate_until_no_changes'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/runner.rb:237:in `loop'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/runner.rb:237:in `iterate_until_no_changes'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/runner.rb:208:in `do_inspection_loop'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/runner.rb:111:in `block in file_offenses'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/runner.rb:129:in `file_offense_cache'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/runner.rb:109:in `file_offenses'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/runner.rb:100:in `process_file'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/runner.rb:78:in `block in each_inspected_file'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/runner.rb:75:in `each'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/runner.rb:75:in `reduce'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/runner.rb:75:in `each_inspected_file'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/runner.rb:67:in `inspect_files'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/runner.rb:39:in `run'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cli.rb:160:in `execute_runner'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cli.rb:88:in `execute_runners'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cli.rb:41:in `run'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/exe/rubocop:13:in `block in <top (required)>'
~/.rbenv/versions/2.4.4/lib/ruby/2.4.0/benchmark.rb:308:in `realtime'
~/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/rubocop-0.57.2/exe/rubocop:12:in `<top (required)>'
~/.rbenv/versions/2.4.4/bin/rubocop:23:in `load'
~/.rbenv/versions/2.4.4/bin/rubocop:23:in `<main>'
thank you! Removing the empty line at the top fixed this issue for me.
I can't reproduce this on current master, with the default configuration. Could you please post your .rubocop.yml as well?
I can reproduce with current master and default configuration:
$ git clone https://github.com/rubocop-hq/rubocop
$ cd rubocop
~/rubocop$ bundle install
~/rubocop$ cat test.rb
class T
end
~/rubocop$ bundle exec ./exe/rubocop --debug --force-default-config --auto-correct test.rb
Default configuration from /home/user/rubocop/config/default.yml
Inheriting configuration from /home/user/rubocop/config/enabled.yml
Inheriting configuration from /home/user/rubocop/config/disabled.yml
Inspecting 1 file
Scanning /home/user/rubocop/test.rb
0 files inspected, no offenses detected
comparison of Parser::Source::Range with Parser::Source::Range failed
/q/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/parser-2.5.1.0/lib/parser/source/tree_rewriter/action.rb:38:in `sort_by'
/q/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/parser-2.5.1.0/lib/parser/source/tree_rewriter/action.rb:38:in `ordered_replacements'
/q/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/parser-2.5.1.0/lib/parser/source/tree_rewriter.rb:191:in `process'
/home/user/rubocop/lib/rubocop/cop/corrector.rb:68:in `rewrite'
/home/user/rubocop/lib/rubocop/cop/team.rb:126:in `autocorrect_all_cops'
/home/user/rubocop/lib/rubocop/cop/team.rb:70:in `autocorrect'
/home/user/rubocop/lib/rubocop/cop/team.rb:98:in `block in offenses'
/home/user/rubocop/lib/rubocop/cop/team.rb:115:in `investigate'
/home/user/rubocop/lib/rubocop/cop/team.rb:94:in `offenses'
/home/user/rubocop/lib/rubocop/cop/team.rb:44:in `inspect_file'
/home/user/rubocop/lib/rubocop/runner.rb:265:in `inspect_file'
/home/user/rubocop/lib/rubocop/runner.rb:212:in `block in do_inspection_loop'
/home/user/rubocop/lib/rubocop/runner.rb:244:in `block in iterate_until_no_changes'
/home/user/rubocop/lib/rubocop/runner.rb:237:in `loop'
/home/user/rubocop/lib/rubocop/runner.rb:237:in `iterate_until_no_changes'
/home/user/rubocop/lib/rubocop/runner.rb:208:in `do_inspection_loop'
/home/user/rubocop/lib/rubocop/runner.rb:111:in `block in file_offenses'
/home/user/rubocop/lib/rubocop/runner.rb:129:in `file_offense_cache'
/home/user/rubocop/lib/rubocop/runner.rb:109:in `file_offenses'
/home/user/rubocop/lib/rubocop/runner.rb:100:in `process_file'
/home/user/rubocop/lib/rubocop/runner.rb:78:in `block in each_inspected_file'
/home/user/rubocop/lib/rubocop/runner.rb:75:in `each'
/home/user/rubocop/lib/rubocop/runner.rb:75:in `reduce'
/home/user/rubocop/lib/rubocop/runner.rb:75:in `each_inspected_file'
/home/user/rubocop/lib/rubocop/runner.rb:67:in `inspect_files'
/home/user/rubocop/lib/rubocop/runner.rb:39:in `run'
/home/user/rubocop/lib/rubocop/cli.rb:160:in `execute_runner'
/home/user/rubocop/lib/rubocop/cli.rb:88:in `execute_runners'
/home/user/rubocop/lib/rubocop/cli.rb:41:in `run'
/home/user/rubocop/exe/rubocop:13:in `block in <top (required)>'
/q/rbenv/versions/2.4.1/lib/ruby/2.4.0/benchmark.rb:308:in `realtime'
/home/user/rubocop/exe/rubocop:12:in `<top (required)>'
/q/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/cli/exec.rb:74:in `load'
/q/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/cli/exec.rb:74:in `kernel_load'
/q/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/cli/exec.rb:27:in `run'
/q/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/cli.rb:362:in `exec'
/q/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/q/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/q/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
/q/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/cli.rb:22:in `dispatch'
/q/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
/q/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/cli.rb:13:in `start'
/q/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/exe/bundle:30:in `block in <top (required)>'
/q/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/friendly_errors.rb:121:in `with_friendly_errors'
/q/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/exe/bundle:22:in `<top (required)>'
/q/rbenv/versions/2.4.1/bin/bundle:22:in `load'
/q/rbenv/versions/2.4.1/bin/bundle:22:in `<main>'
I tried digging into problem with pry. The problem is triggered by the trailing_blank_lines cops, which somehow results in one of the replacement ranges being undefined (and therefore not comparable):
[4] pry(#<Parser::Source::TreeRewriter::Action>)> @children
=> [#<Parser::Source::TreeRewriter::Action:0x00007faeebd0af38
@children=[],
@enforcer=
#<Method: Parser::Source::TreeRewriter#enforce_policy>,
@insert_after="",
@insert_before="",
@range=#<Parser::Source::Range:0x3fd775e89770>,
@replacement="">,
#<Parser::Source::TreeRewriter::Action:0x00007faeebd0ad08
@children=[],
@enforcer=
#<Method: Parser::Source::TreeRewriter#enforce_policy>,
@insert_after="",
@insert_before="",
@range=
#<Parser::Source::Range /Users/matthi/projects/rubocop/test.rb 12...15>,
@replacement="\n">]
Sorry for not having a patch.
Thanks for the feedback. I confirmed this reproduction.
Although this is an assumption, it may be that some kind of conflict is occurring by auto-correct of Layout/LeadingBlankLines cop and Layout/TrailingBlankLines cop 馃 It is because there is no problem with auto-corrected by individually cop.
Looks like error is produced only if Layout/LeadingBlankLines is the first cop to make modifications.
For example:
# (blank line above this comment)
# frozen_string_literal: true
def bar(arg =1); end
also triggers the error with default config, but with comments first offence to be fixed is Style/FrozenStringLiteralComment and then Layout/LeadingBlankLines works fine.
Trying to debug.
Found the culprit, Layout/LeadingBlankLines constructed Parser::Source::Range from raw_source instead of buffer which caused comparison error (Parser::Source::Range checks the other to be on the same buffer).
PR is in progress, tests for this are going to take more time than actual fix.
Most helpful comment
thank you! Removing the empty line at the top fixed this issue for me.