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

Exclude pattern is not make effect when setting in ~/.rubocop.yml for emacs flycheck. #1368

Closed
zw963 opened this Issue Dec 3, 2017 · 19 comments

Comments

Projects
None yet
4 participants
@zw963

zw963 commented Dec 3, 2017

Please see follow links, thanks

rubocop-hq/rubocop#5139 (comment)

@cpitclaudel

This comment has been minimized.

Member

cpitclaudel commented Dec 3, 2017

Thanks. Would you like to submit a pull request?

@zw963

This comment has been minimized.

zw963 commented Dec 3, 2017

Sorry, I think this feature is added in newest version flycheck.

Thanks very much.

@zw963 zw963 closed this Dec 3, 2017

@zw963

This comment has been minimized.

zw963 commented Dec 3, 2017

Hi, I think something still borken this feature.

when I run follow command, rubocop check is skipped as expect.

rubocop --display-cop-names --force-exclusion --format emacs --cache false --config ~/.rubocop.yml db/schema.rb

But, when I open db/schema.rb with a opened emacs frame, rubocop check is still invoked.
and told me following error:

Warning (flycheck): Syntax checker ruby-rubocop reported too many errors (4929) and is disabled.
Warning (flycheck): Syntax checker ruby-rubocop reported too many errors (4929) and is disabled.

@zw963 zw963 reopened this Dec 3, 2017

@zw963

This comment has been minimized.

zw963 commented Dec 3, 2017

I think, flycheck just not try to mark current buffer to show warning message, but, rubocop is run
against excluded file, my db/schema.rb is a big file, so, mostly, when i open this file, CPU usage
is very high.

@fmdkdd

This comment has been minimized.

Member

fmdkdd commented Dec 3, 2017

Hmm, what happens if you run:

rubocop --display-cop-names --force-exclusion --format emacs --cache false --config ~/.rubocop.yml schema.rb

That is, when you run robocop directly from the db folder? Is the file db/schema.rb excluded, or does rubocop see it?

@zw963

This comment has been minimized.

zw963 commented Dec 4, 2017

@fmdkdd , sorry, the correct command is:

rubocop --display-cop-names --force-exclusion --format emacs --cache false --config ~/.rubocop.yml db/schema.rb

~/.rubocop.yml config like this:

AllCops:
  Include:
    - '**/Rakefile'
    - '**/config.ru'
  Exclude:
  - 'vendor/**/*'
  - 'spec/fixtures/**/*'
  - 'db/schema.rb'
  - 'db/migrate/*'
  - 'bin/bundle'
  - 'bin/rails'
  - 'bin/rake'
  - 'bin/setup'
  - 'bin/spring'
  - 'config/**/*'
  - 'script/**/*'
  - !ruby/regexp /old_and_unused\.rb$/

  TargetRubyVersion:
    2.3

But, if run with .... schema.rb as prevoius reply, --force-exclusion is not work.

@fmdkdd

This comment has been minimized.

Member

fmdkdd commented Dec 4, 2017

But, if run with .... schema.rb as prevoius reply, --force-exclusion is not work.

Okay, so rubocop is confused with relative names. But that shouldn't matter since we are passing the full path to rubocop when called from Flycheck.

Testing on my side, rubocop does exclude the file if I give the full path:

rubocop --display-cop-names --force-exclusion --format emacs --cache false --config rubocop.yml `readlink -f warnings.rb`

The file is ignored, and rubocop reports no warnings. Same inside Emacs.

Are you sure you are using the latest Flycheck version? When browsing db/schema.rb, what does flycheck-compile reports for ruby-rubocop?

@zw963

This comment has been minimized.

zw963 commented Dec 4, 2017

Testing on my side, rubocop does exclude the file if I give the full path:

yes, same as you, following command rubocop work fine.

rubocop --display-cop-names --force-exclusion --format emacs --cache false  `readlink -e db/schema.rb`

Are you sure you are using the latest Flycheck version? When browsing db/schema.rb

I think i use newest flycheck, d2b3c32

what does flycheck-compile reports for ruby-rubocop?

rubocop --display-cop-names --force-exclusion --format emacs --cache false --config /home/zw963/.rubocop.yml --stdin /home/zw963/IKCRM/ikcrm_cms/db/schema.rb < /home/zw963/IKCRM/ikcrm_cms/db/schema.rb

@fmdkdd

This comment has been minimized.

Member

fmdkdd commented Dec 4, 2017

rubocop --display-cop-names --force-exclusion --format emacs --cache false --config /home/zw963/.rubocop.yml --stdin /home/zw963/IKCRM/ikcrm_cms/db/schema.rb < /home/zw963/IKCRM/ikcrm_cms/db/schema.rb

That's it? No output, no warnings?

@zw963

This comment has been minimized.

zw963 commented Dec 4, 2017

@fmdkdd, sure it have.

4929 line warn, should I post that here?

following is some example:

/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:2:1: C: Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.
/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:15:1: C: Layout/EmptyLinesAroundBlockBody: Extra empty line detected at block body beginning.
/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:16:16: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:17:16: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:18:16: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:19:16: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:20:16: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:21:16: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:22:16: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
.....
@fmdkdd

This comment has been minimized.

Member

fmdkdd commented Dec 4, 2017

Okay so rubocop is generating warnings even though it shouldn't, but only when run from Emacs?

Perhaps Emacs is picking up a different rubocop executable? Can you check the rubocop version that Emacs is using with:

M-x eval-expression RET (flycheck-find-checker-executable 'ruby-rubocop) RET

Is it the same as which rubocop from your shell?

@zw963

This comment has been minimized.

zw963 commented Dec 4, 2017

Perhaps Emacs is picking up a different rubocop executable? Can you check the rubocop version that Emacs is using with:

I only one rubocop executable.

-*- mode: compilation; default-directory: "~/IKCRM/ikcrm_cms/db/" -*-
Compilation started at Tue Dec  5 00:38:09

rubocop --display-cop-names --force-exclusion --format emacs --cache false --config /home/zw963/.rubocop.yml --stdin /home/zw963/IKCRM/ikcrm_cms/db/schema.rb < /home/zw963/IKCRM/ikcrm_cms/db/schema.rb

Above is full report when I open db/schema from dired-mode, i notice the default-directory is: ~/IKCRM/ikcrm_cms/db/, so I run following within db folder, i can reproduce this now.

$: cd  /home/zw963/IKCRM/ikcrm_cms/db/
$: rubocop --display-cop-names --force-exclusion --format emacs --cache false --config /home/zw963/.rubocop.yml --stdin /home/zw963/IKCRM/ikcrm_cms/db/schema.rb < /home/zw963/IKCRM/ikcrm_cms/db/schema.rb

/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:2:1: C: Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.
/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:15:1: C: Layout/EmptyLinesAroundBlockBody: Extra empty line detected at block body beginning.
/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:16:16: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:17:16: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:18:16: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:19:16: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:20:16: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:21:16: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:22:16: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:23:16: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
/home/zw963/IKCRM/ikcrm_cms/db/schema.rb:24:16: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

...
@fmdkdd

This comment has been minimized.

Member

fmdkdd commented Dec 4, 2017

Ahah! So it was an issue with the handling of paths in rubocop after all.

So rubocop does not see /home/zw963/IKCRM/ikcrm_cms/db/schema.rb and db/schema.rb as the same files. It seems db/schema.rb from the config file is resolved relatively to the folder where rubocop is run, although you could expect it to be resolved relatively to the location of the config file.

You might want to signal this issue with the rubocop developers.

Otherwise, I think we can fix this in Flycheck by specifying as :working-directory the directory where the config file resides.

cc @Simplify

@Simplify

This comment has been minimized.

Member

Simplify commented Dec 6, 2017

@fmdkdd :working-directory, in this situation, can't be place where config file resides. @zw963 keeps his .rubocop.yml in ~/.rubocop.yml and paths in config are relative to Rails root directory, in this case: /home/zw963/IKCRM/ikcrm_cms/.

According to the rubocop documentation Include/Exclude rules are relative to the location of config file, so no wonder that it does not work.

In one of my old projects that still have .rubocop.yml file in Rails root directory everything works as intended. There is nothing to fix here, --force-exclusion just works.

@zw963 Copy your ~/.rubycop.yml file to /home/zw963/IKCRM/ikcrm_cms/. Apparently you can't just create new .rubocop.yml file in Rails root and include your ~/.rubocop.yml in there, that will not work:

Note: The Include and Exclude parameters are special. They are valid for the directory tree starting where they are defined. They are not shadowed by the setting of Include and Exclude in other .rubocop.yml files in subdirectories. This is different from all other parameters, who follow RuboCop's general principle that configuration for an inspected file is taken from the nearest .rubocop.yml, searching upwards.

@fmdkdd

This comment has been minimized.

Member

fmdkdd commented Dec 6, 2017

@Simplify Indeed, thank you for correcting me. Specifying project-specific exclude/include paths into a global config file seems like a bad idea (although, I understand the file db/schema.rb is frequent).

@Simplify

This comment has been minimized.

Member

Simplify commented Dec 6, 2017

Yes, that one is almost always present. In some cases where old non-rails app is ported to rails or if you use multiple databases you may not need/use it, at least not as db/schema.rb.
Personally, I never open that file.

@zw963

This comment has been minimized.

zw963 commented Dec 7, 2017

@zw963 keeps his .rubocop.yml in ~/.rubocop.yml and paths in config are relative to Rails root directory, in this case: /home/zw963/IKCRM/ikcrm_cms/.

@Simplify , Yes, I did like that, but following command is worked.

╭─ 10:08  zw963 ⮀ ~/IKCRM/ikcrm_cms ⮀ 
 ╰─ $ rubocop --display-cop-names --force-exclusion --format emacs --cache false  db/schema.rb

It is not worked only when working-directory in ~/IKCRM/ikcrm/cms/db, and file name pass in as
schema.rb instead of db/schema.rb, as flycheck invoke rubocop is done.

Thanks, I have resolve this problem now follow your's advise, but i am not copy ~/.rubocop into my
project, i just add a new option into ~/.rubocop.yml as follow:

  Exclude:
  - 'schema.rb'
  - 'db/schema.rb'
...

@zw963 zw963 closed this Dec 7, 2017

@Simplify

This comment has been minimized.

Member

Simplify commented Dec 7, 2017

Ok, I see. Not expected behavior after reading rubocop docs...

The problem is that flycheck sets default directory where your configuration file is located, so that becomes /home/zw963/ in your case. I guess we can set :working-directory to directory where Gemfile is located with fallback to current behavior. No harm done in that case.

@Simplify Simplify reopened this Dec 7, 2017

Simplify added a commit that referenced this issue Dec 7, 2017

@Simplify

This comment has been minimized.

Member

Simplify commented Dec 7, 2017

Ok, pull request tested with several scenarios:

  • Still works when .rubocop.yml is located in ruby project directory.
  • Works when .rubocop.yml is located in home directory
  • Works when .rubocop.yml is located in subdirectory in ruby project and Exclude point to files relative to that .rubocop.yml file.
  • Tested with combination of last two

Simplify added a commit that referenced this issue Dec 11, 2017

Simplify added a commit that referenced this issue Dec 13, 2017

@fmdkdd fmdkdd closed this in #1371 Dec 14, 2017

fmdkdd added a commit that referenced this issue Dec 14, 2017

Merge pull request #1371 from flycheck/GH-1368
GH-1368 - Run rubocop from ruby project directory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment