rubocop icon indicating copy to clipboard operation
rubocop copied to clipboard

RuboCop::CLI unexpected behavior when linting files outside the project

Open budu opened this issue 2 years ago • 0 comments

Expected Behavior

There is no obvious expected behavior.

Actual Behavior

I've been having weird issues with slim-lint integration into Emacs flycheck. After some time debugging these problems, it became apparent that the bug lies within slim-lint or some of its dependencies. After reviewing slim-lint I'm quite sure this issue is specific to RuboCop::CLI.

When running slim-lint (which uses RuboCop::CLI) on a file outside the project, it returns unexpected results. This is an issue with flycheck as it is using temporary files in /tmp.

For example, the following code, inside the normal path app/views/... doesn't have any warnings.

p
  | Can't remember your password for
  strong = @user.email

It does output a warning (with rubocop-rails in the project .rubocop.yml) when that file is anywhere outside the project. The slim-lint output also contains weird errors:

$ slim-lint ../password_reset.html.slim
...
175 errors occurred:
An error occurred while RSpec/Capybara/FeatureMethods cop was inspecting /home/budu/projects/password_reset.html.slim.rb.
An error occurred while RSpec/Rails/AvoidSetupHook cop was inspecting /home/budu/projects/password_reset.html.slim.rb.
An error occurred while RSpec/Rails/NegationBeValid cop was inspecting /home/budu/projects/password_reset.html.slim.rb.
...
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.60.1 (using Parser 3.3.0.4, rubocop-ast 1.30.0, running on ruby 3.2.2) [x86_64-linux]
../password_reset.html.slim:3 [W] RuboCop: Rails/HelperInstanceVariable: Do not use instance variables in helpers.

The errors are repeated for every lines in the file.

I've debugged slim-lint and I found out that the RuboCop::CLI instance has a different @config_store and @path_cache. Here's the path cache for the file with the correct path:

   @path_cache=
    {"/home/budu/projects/teki/app/views/user_mailer"=>"/home/budu/projects/teki/.rubocop.yml",
     "/home/budu/projects/teki"=>"/home/budu/projects/teki/.rubocop.yml"},

And here's the one with the wrong path:

   @path_cache=
  {"/home/budu/projects"=>"/home/budu/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.60.1/config/default.yml",
   "/home/budu/projects/teki"=>"/home/budu/projects/teki/.rubocop.yml"},

As you can see the RuboCop::CLI instance is using the gem default configuration. It shouldn't pose a problem in itself, but the requires (rubocop-rails, rubocop-rspec) from the project configuration are still enabled, causing the errors/warnings.

In theory, adding Rails/HelperInstanceVariable to the list of slim-lint ignored cops should fix that particular issue but that behavior looks wrong.

I'm not sure where to go from here.

RuboCop version

$ rubocop -V
1.60.1 (using Parser 3.3.0.4, rubocop-ast 1.30.0, running on ruby 3.2.2) [x86_64-linux]
  - rubocop-minitest 0.34.5
  - rubocop-rails 2.23.1
  - rubocop-rspec 2.26.1

budu avatar Jan 22 '24 00:01 budu