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

An error occurred while Style/RescueEnsureAlignment cop was inspecting #2119

Closed
rrosenblum opened this issue Aug 7, 2015 · 4 comments · Fixed by #2137
Closed

An error occurred while Style/RescueEnsureAlignment cop was inspecting #2119

rrosenblum opened this issue Aug 7, 2015 · 4 comments · Fixed by #2137
Labels

Comments

@rrosenblum
Copy link
Contributor

The error message is undefined method 'include?' for nil:NilClass. This is happening in version 0.33.0.

I am trying to limit down the code that causes this issue to provide example source. Placing breakpoints in the code, it appears that investigate isn't being run so @modifier_locations is not set.

A quick fix would be

def modifier?(node)
  @modifier_locations.include?(node.loc.keyword) if @modifier_locations
end

or initializing @modifier_locations to [].

One of the safeguards is worthwhile, but I am more curious why investigate does not appear to be running at all.

@bbatsov bbatsov added the bug label Aug 9, 2015
@lumeet
Copy link
Contributor

lumeet commented Aug 10, 2015

Any news on this, @rrosenblum? I haven't been able to reproduce the issue.

@rrosenblum
Copy link
Contributor Author

I am working on reproducing the issue. I have been unable to boil the code down to a small reproducible state. The error consistently happens on one of my machines, but I can not reproduce it on another.

The issue is that investigate is not being run, before or after on_resbody is being run. One of the fixes listed earlier will fix this issue, but I am unsure why the issue is happening in the first place.

@rrosenblum
Copy link
Contributor Author

I found the issue, and a way to reproduce it. The issue isn't specifically with Style/RescueEnsureAlignment. It seems to be caused by excluding a file from a specific cop not working properly.

# .rubocop.yml
Style/RescueEnsureAlignment:
  Exclude:               
    - 'test.rb' 
# test.rb
begin
  foo 
rescue
  bar 
end

When RuboCop is run, it will wind up inspecting the code in test.rb even though it is excluded. investigate is not run because of the code in Commissioner#invoke_custom_processing which is why @modifier_locations is nil.

This issue does not happen when the file is excluded from AllCops.

I am going to have to do some digging into how to fix this issue.

Looping in @bbatsov and @jonas054.

@rrosenblum
Copy link
Contributor Author

It appears that the way Excluded files are handled is that the cop runs all on_ handlers in that file and then the offenses for that cop are ignored. invoke_custom_processing will skip running the investigate method on the file.

From the commissioner code

  def investigate(processed_source)
    reset_errors
    prepare(processed_source)
    invoke_custom_processing(@cops, processed_source)
    invoke_custom_processing(@forces, processed_source)
    process(processed_source.ast) if processed_source.ast
    @cops.each_with_object([]) do |cop, offenses|
      filename = processed_source.buffer.name
      # ignore files that are of no interest to the cop in question
      offenses.concat(cop.offenses) if cop.relevant_file?(filename)
    end
  end

This doesn't seem like the best way to handle this. It would be preferable if we could avoid running the on_ handlers in these cops all together. I will continue researching, but as of right now, I can not think of a way to implement this.

I will add a bug fix for undefined method 'include?' and continue researching a more thorough solution.

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

Successfully merging a pull request may close this issue.

3 participants