pocke / rubocop-grep

MIT License
20 stars 3 forks source link

`Grep/Grep` cop occurs an error when running #4

Open ydah opened 1 year ago

ydah commented 1 year ago

Hello.

Grep/Grep cop occurs an error when running. I have prepared .rubocop.yml as follows from the README:

require:
  - rubocop-grep

Grep/Grep:
  Rules:
    # The simplest rule definition. It warns if the regexp matches your code.
    - Pattern: '\bENV\b'
      Message: Do not refer ENV directly.

The following error occurs:

An error occurred while Grep/Grepx cop was inspecting /Users/ydah/sandbox-rubocop-grep/target.rb.
undefined method `each' for nil:NilClass
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-grep-0.1.0/lib/rubocop/cop/grep/grep.rb:13:in `on_new_investigation'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cop/commissioner.rb:160:in `block (2 levels) in invoke'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cop/commissioner.rb:171:in `with_cop_error_handling'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cop/commissioner.rb:160:in `block in invoke'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cop/commissioner.rb:160:in `each'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cop/commissioner.rb:160:in `invoke'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cop/commissioner.rb:84:in `investigate'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cop/team.rb:156:in `investigate_partial'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cop/team.rb:98:in `investigate'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:345:in `block in inspect_file'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:344:in `each'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:344:in `flat_map'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:344:in `inspect_file'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:287:in `block in do_inspection_loop'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:321:in `block in iterate_until_no_changes'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:314:in `loop'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:314:in `iterate_until_no_changes'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:283:in `do_inspection_loop'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:164:in `block in file_offenses'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:189:in `file_offense_cache'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:163:in `file_offenses'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:154:in `process_file'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:135:in `block in each_inspected_file'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:134:in `each'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:134:in `reduce'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:134:in `each_inspected_file'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:120:in `inspect_files'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/runner.rb:73:in `run'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cli/command.rb:11:in `run'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cli/environment.rb:18:in `run'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cli.rb:118:in `run_command'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cli.rb:125:in `execute_runners'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cli.rb:51:in `block in run'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cli.rb:77:in `profile_if_needed'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/lib/rubocop/cli.rb:43:in `run'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/exe/rubocop:19:in `block in <top (required)>'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/3.2.0/benchmark.rb:311:in `realtime'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/exe/rubocop:19:in `<top (required)>'
/Users/ydah/.rbenv/versions/3.2.0/bin/rubocop:25:in `load'
/Users/ydah/.rbenv/versions/3.2.0/bin/rubocop:25:in `<top (required)>'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/3.2.0/bundler/cli/exec.rb:58:in `load'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/3.2.0/bundler/cli/exec.rb:58:in `kernel_load'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/3.2.0/bundler/cli/exec.rb:23:in `run'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/3.2.0/bundler/cli.rb:491:in `exec'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/3.2.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/3.2.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/3.2.0/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/3.2.0/bundler/cli.rb:34:in `dispatch'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/3.2.0/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/3.2.0/bundler/cli.rb:28:in `start'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.4.1/libexec/bundle:45:in `block in <top (required)>'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/3.2.0/bundler/friendly_errors.rb:117:in `with_friendly_errors'
/Users/ydah/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.4.1/libexec/bundle:33:in `<top (required)>'
/Users/ydah/.rbenv/versions/3.2.0/bin/bundle:25:in `load'
/Users/ydah/.rbenv/versions/3.2.0/bin/bundle:25:in `<main>'
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Grep/Grepx cop was inspecting /Users/ydah/sandbox-rubocop-grep/target.rb.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

Maybe, It appears that https://github.com/pocke/rubocop-grep/commit/efca29388d13257b217dc03cb432abaab722f00e, which hacked for steep, is the cause. It appears that an error is occurring because the each method is being called for nil because it references cop_config in Grep/Grepx at runtime. https://github.com/pocke/rubocop-grep/blob/aed0a2734d6f1d0b4926550a51e63e2bcc31d803/lib/rubocop/cop/grep/grep.rb#L13

This will work with the following fixes, but the cop name in the offense log is still wrong (Grep/Grepx).

- cop_config['Rules'].each do |rule|
+ config['Grep/Grep']['Rules'].each do |rule|

The following offense logs occur:

target.rb:3:1: C: Grep/Grepx: Do not refer ENV directly.
                       ^^^^^

Steps to reproduce the problem

  1. Check out the https://github.com/ydah/sandbox-rubocop-grep
  2. Run bundle install.
  3. Run bundle exec rubocop.

RuboCop version

❯ bundle exec rubocop -V
1.48.1 (using Parser 3.2.1.1, rubocop-ast 1.27.0, running on ruby 3.2.0) [x86_64-darwin21]
  - rubocop-grep 0.1.0
hoshinotsuyoshi commented 1 year ago

I ran into this too.

And my patch is below :

--- a/lib/rubocop/cop/grep/grep.rb
+++ b/lib/rubocop/cop/grep/grep.rb
@@ -54,6 +54,15 @@ module RuboCop
           opt |= Regexp::IGNORECASE if rule['Ignorecase']
           opt
         end
+
+        # workaround for https://github.com/pocke/rubocop-grep/issues/4
+        def self.badge
+          if name == "RuboCop::Cop::Grep::Grepx"
+            @badge ||= Badge.for("RuboCop::Cop::Grep::Grep")
+          else
+            super
+          end
+        end
       end

We have confirmed that this will correctly output Grep/Grep instead of Grep/Grepx in the offence message.

hoshinotsuyoshi commented 1 year ago

@pocke I sent a pull request. https://github.com/pocke/rubocop-grep/pull/6

I would be very happy if you could take a look at it ! :)

hoshinotsuyoshi commented 12 months ago

I thought updating steep gem would solve this problem. Submitted another pull request #8 🙏 (As far as I tested, it seems like this will probably be fixed in steep 1.4.0.dev.5 or later.)