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

0.61.0 release breaks Layout/RescueEnsureAlignment cop for indented code #6550

Closed
Kjarrigan opened this issue Dec 5, 2018 · 5 comments
Closed
Assignees
Labels

Comments

@Kjarrigan
Copy link

Kjarrigan commented Dec 5, 2018

Expected behavior

Worked with 0.60.0

Actual behavior

Breaks with 0.61.0

Steps to reproduce the problem

# frozen_string_literal: true

def self.parse(xml)
  io = new
  attributes.each do |key, _val|
    value = begin
              xml.nodes.find { |node| node.value == key.to_s }&.nodes.to_a[0].to_i
            rescue StandardError
              nil
            end
    io.attributes[key] = value if value
  end
  io
end

Backtrace:

holger.arndt@pc-arndt:~/snippets $ rubocop minimal.rb  -d
For /home/holger.arndt/snippets: configuration from /home/holger.arndt/snippets/.rubocop.yml
Default configuration from /home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/config/default.yml
Inspecting 1 file
Scanning /home/holger.arndt/snippets/minimal.rb
An error occurred while Layout/RescueEnsureAlignment cop was inspecting /home/holger.arndt/snippets/minimal.rb:6:12.
undefined method `name' for #<Parser::Source::Map::Collection:0x00005569cf291f10>
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/layout/rescue_ensure_alignment.rb:107:in `alignment_source'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/layout/rescue_ensure_alignment.rb:92:in `format_message'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/layout/rescue_ensure_alignment.rb:82:in `check'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/layout/rescue_ensure_alignment.rb:36:in `on_resbody'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/commissioner.rb:58:in `block (2 levels) in trigger_responding_cops'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/commissioner.rb:106:in `with_cop_error_handling'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/commissioner.rb:57:in `block in trigger_responding_cops'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/commissioner.rb:56:in `each'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/commissioner.rb:56:in `trigger_responding_cops'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/commissioner.rb:34:in `block (2 levels) in <class:Commissioner>'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/ast/traversal.rb:169:in `block in on_case'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/ast/traversal.rb:168:in `each'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/ast/traversal.rb:168:in `on_case'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/commissioner.rb:35:in `block (2 levels) in <class:Commissioner>'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/ast/traversal.rb:50:in `block in on_kwbegin'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/ast/traversal.rb:50:in `each'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/ast/traversal.rb:50:in `on_kwbegin'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/commissioner.rb:35:in `block (2 levels) in <class:Commissioner>'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/ast/traversal.rb:61:in `on_lvasgn'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/commissioner.rb:35:in `block (2 levels) in <class:Commissioner>'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/ast/traversal.rb:50:in `block in on_begin'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/ast/traversal.rb:50:in `each'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/ast/traversal.rb:50:in `on_begin'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/commissioner.rb:35:in `block (2 levels) in <class:Commissioner>'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/ast/traversal.rb:164:in `on_block'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/commissioner.rb:35:in `block (2 levels) in <class:Commissioner>'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/ast/traversal.rb:50:in `block in on_begin'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/ast/traversal.rb:50:in `each'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/ast/traversal.rb:50:in `on_begin'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/commissioner.rb:35:in `block (2 levels) in <class:Commissioner>'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/ast/traversal.rb:129:in `on_defs'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/commissioner.rb:35:in `block (2 levels) in <class:Commissioner>'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/ast/traversal.rb:13:in `walk'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/commissioner.rb:46:in `investigate'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/team.rb:116:in `investigate'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/team.rb:104:in `offenses'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cop/team.rb:44:in `inspect_file'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/runner.rb:280:in `inspect_file'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/runner.rb:227:in `block in do_inspection_loop'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/runner.rb:259:in `block in iterate_until_no_changes'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/runner.rb:252:in `loop'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/runner.rb:252:in `iterate_until_no_changes'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/runner.rb:223:in `do_inspection_loop'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/runner.rb:126:in `block in file_offenses'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/runner.rb:144:in `file_offense_cache'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/runner.rb:124:in `file_offenses'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/runner.rb:112:in `process_file'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/runner.rb:89:in `block in each_inspected_file'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/runner.rb:86:in `each'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/runner.rb:86:in `reduce'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/runner.rb:86:in `each_inspected_file'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/runner.rb:76:in `inspect_files'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/runner.rb:48:in `run'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cli.rb:150:in `execute_runner'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cli.rb:77:in `execute_runners'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/lib/rubocop/cli.rb:42:in `run'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/exe/rubocop:13:in `block in <top (required)>'
/home/holger.arndt/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/benchmark.rb:308:in `realtime'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/exe/rubocop:12:in `<top (required)>'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/bin/rubocop:23:in `load'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/bin/rubocop:23:in `<main>'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/bin/ruby_executable_hooks:15:in `eval'
/home/holger.arndt/.rvm/gems/ruby-2.5.1/bin/ruby_executable_hooks:15:in `<main>'
C

RuboCop version

$ [bundle exec] rubocop -V
0.61.0 (using Parser 2.5.1.2, running on ruby 2.5.1 x86_64-linux)
@Kjarrigan
Copy link
Author

Kjarrigan commented Dec 5, 2018

"Works" if I remove the indentation:

# frozen_string_literal: true

def self.parse(xml)
  io = new
  attributes.each do |key, _val|
    value = begin
      xml.nodes.find { |node| node.value == key.to_s }&.nodes.to_a[0].to_i
    rescue StandardError
      nil
    end
    io.attributes[key] = value if value
  end
  io
end
$ rubocop minimal.rb -d
For /home/holger.arndt/snippets: configuration from /home/holger.arndt/snippets/.rubocop.yml
Default configuration from /home/holger.arndt/.rvm/gems/ruby-2.5.1/gems/rubocop-0.61.0/config/default.yml
Inspecting 1 file
Scanning /home/holger.arndt/snippets/minimal.rb
.

1 file inspected, no offenses detected
Finished in 0.2010454740084242 seconds

@Kjarrigan Kjarrigan changed the title An error occurred while Layout/RescueEnsureAlignment cop 0.61.0 release breaks Layout/RescueEnsureAlignment cop for indented code Dec 5, 2018
@michaelabon
Copy link

Another example from my codebase

$ rubocop -d
Running parallel inspection
An error occurred while Layout/RescueEnsureAlignment cop was inspecting ~/bucket/lib/tasks/cf.rake:7:6.
undefined method `name' for #<Parser::Source::Map::Collection:0x00007ff2af63d7e8>

Where the cf.rake file consists of:

 1 namespace :cf do
 2   desc 'Only run on the first application instance'
 3   task :on_first_instance do
 4     instance_index =
 5       begin
 6         JSON.parse(ENV['VCAP_APPLICATION'])['instance_index']
 7       rescue StandardError
 8         nil
 9       end
10     exit(0) unless instance_index.zero?
11   end
12 end

@donoghuc
Copy link

donoghuc commented Dec 5, 2018

I have encountered the same issue. Indentation exactly the same as OP.

Mention the following information in the issue report:
0.61.0 (using Parser 2.5.3.0, running on ruby 2.5.1 x86_64-linux)

donoghuc added a commit to donoghuc/bolt that referenced this issue Dec 5, 2018
The `0.61.0` Rubocop release has a bug tracked with rubocop/rubocop#6550.

Pin to `0.60.0` until resolved.
@Drenmi Drenmi added the bug label Dec 6, 2018
@Drenmi
Copy link
Collaborator

Drenmi commented Dec 6, 2018

Thanks for the report!

Looks like we're missing some test cases for this cop. I will have a look at this.

Minimum reproducible example:

foo =
  begin
    bar
  rescue BazError
    qux
  end

@Drenmi Drenmi self-assigned this Dec 6, 2018
Drenmi added a commit to Drenmi/rubocop that referenced this issue Dec 6, 2018
… on assigned begin-end

We were missing some test cases for this cop, and accidentally introduced a
regression in rubocop#6437.

This change adds a relevant test case and fixes the issue.
@bbatsov bbatsov closed this as completed in 54db237 Dec 6, 2018
@joshuapinter
Copy link
Contributor

FYI, this was fixed in version 0.61.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants