Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

.rubocop.yml exclude doesn't work. #227

Open
ghost opened this issue Nov 29, 2017 · 52 comments · May be fixed by #825
Open

.rubocop.yml exclude doesn't work. #227

ghost opened this issue Nov 29, 2017 · 52 comments · May be fixed by #825
Assignees
Labels
bug Bug reports

Comments

@ghost
Copy link

ghost commented Nov 29, 2017

Your environment

  • vscode-ruby version: 0.15.0
  • Ruby version: ruby 2.4.2p198 (2017-09-14 revision 59899) [x64-mingw32]
  • VS Code version: 1.18.1 and 1.19.0-insider
  • Operating System: Windows 10 latest

Expected behavior

.rubocop.yml like this should work:

AllCops:
  Exclude:
    - 'db/**'
    - '**/Gemfile'
  TargetRubyVersion: 2.4

Actual behavior

db/schema.rb and Gemfile being rubocopped.

Via rubocop db it works. So, it's a bug?

@karuppasamy
Copy link

I don't have any experience with Windows 10. But, I am guessing the below config (in user settings) will solve your problem.

"ruby.rubocop.configFilePath": "path/to/your/.rubocop.yml",

@ghost
Copy link
Author

ghost commented Dec 27, 2017

I can't because today is not possible: #133

@ghost
Copy link
Author

ghost commented Dec 27, 2017

But anyway other settings in my .rubocop.yml are working good!

@wingrunr21
Copy link
Collaborator

Hi @johnunclesam

Is this still an issue for you with the latest version of the extension? Some changes were introduced in how Rubocop is run in v0.16.0 so wondering if that fixed it.

@WA9ACE
Copy link

WA9ACE commented Mar 9, 2018

I personally still have this problem and I'm using version 0.17.0 on macOS.

@msokk
Copy link

msokk commented Mar 10, 2018

Still a problem for me, v0.17.0 with rubocop v0.53.0 on macOS. schema.rb still gets linted by extension.

@ghost
Copy link
Author

ghost commented Mar 10, 2018

Still a problem. schema.rb still gets linted by extension.

@wingrunr21
Copy link
Collaborator

Closing for issue cleanup. Apologies if this is still an issue. We are working to improve the core extension experience.

@garyking
Copy link

Yep still a problem.

@gurgeous
Copy link
Contributor

I bet rubocop doesn't apply Exclude rules when you run it on a single file. Perhaps this will help - rubocop/rubocop#893

@garyking
Copy link

garyking commented Apr 10, 2018

I just ran Rubocop on a single file, and it respected exclusions.

Specifically, certain rules by default in Rubocop, don't apply to files in specs and test folders, yet in this extension, they still run. Like Metrics/ClassLength.

@gurgeous
Copy link
Contributor

@garyking, there are two separate issues:

  1. Things like Metrics/ClassLength are applied when they shouldn't be. Presumably because rubocop isn't getting enough context when linting. Haven't investigated yet.
  2. Files that are listed under Exclude still get linted (like db/schema.rb). I bet adding --force-exclusion will solve this problem.

@wingrunr21
Copy link
Collaborator

@garyking what's your rubocop config for this extension look like? Both of those issues basically mean Rubocop is either not finding the .rubocop.yml or is ignoring the settings. I'm wondering if one of the extension settings is activating a CLI option that causes things to be ignored.

@garyking
Copy link

This ext isn't importing Excludes from inherited rules for some reason.

I have a workspace in VSC. There's a .rubocop.yml in the workspace's root. It just has: inherit_from: /Users/gary/.base-rubocop.yml.

In that inherited file, it has:

Metrics/ClassLength:
  Exclude:
    - 'test/**/*'
  Max: 150

Back in my workspace, in the file test/test_file.rb, if I run Rubocop on the file from Terminal, then I get no errors. But in VSC, this ext gives me the Metrics/ClassLength error, indicating that Exclude is not inherited. But the Max value definitely is, because it says 205/150.

@gurgeous
Copy link
Contributor

I am planning to investigate this and other linting issues, see #317 (comment). Probably need a new tracking issue for that once @wingrunr21 chimes in.

@zackchandler
Copy link

zackchandler commented Aug 1, 2018

Changing:

inherit_from: .rubocop_todo.yml

to

inherit_from: ~/.rubocop_todo.yml

fixed the issue in VS Code. But it breaks running on command line through CLI.

@mpressen
Copy link

mpressen commented Feb 15, 2019

exclude in .rubocop.yml (and .rubocop_todo.yml ) when using vs code formatting on save still doesn't work. Has someone found a workaround ?

@wingrunr21
Copy link
Collaborator

It will be fixed soon

@cliffrowley
Copy link

Any news on this?

@wingrunr21
Copy link
Collaborator

The language server support for RuboCop should support the whole config file

@cliffrowley
Copy link

cliffrowley commented Oct 9, 2019

I am still getting warnings in files that I have specifically excluded on my .rubocop.yml.

@mattalat
Copy link

mattalat commented Nov 7, 2019

I'll chime in and say that the AllCops ignore doesn't seem to have any effect, but other settings in the .rubocop.yml file are respected by the extension.

@nikukyugamer
Copy link

The Exclude: settings in my .rubocop.yml doesn't work, too.
It's tough for me.

@jacek213
Copy link

Same here, not cool at all.

@nemoDreamer
Copy link

Yup, still an issue with a config file that inherits from other files...

@wingrunr21
Copy link
Collaborator

Can I get some more examples of RuboCop configs? I'm wondering if this is a confluence of three things:

  • The file name is passed into RuboCop's stdin input as an absolute path
  • RuboCop configs are probably configured as relative paths
  • The working directory RuboCop is run against

Note that other than how the language server invokes RuboCop, there's little that I can do here. I need to be able to invoke RuboCop consistently and have it give me an appropriate response in whatever circumstance. This is why I think there's some kind of mismatch between how everyone's configuring their linters and how RuboCop is being invoked.

@pre
Copy link

pre commented Apr 7, 2020

This issue was closed so I explained my findings in #566 (comment)

TL;DR

  • inherit_from works with relative paths for me (when code is executed in the project's root folder)
  • relative paths in the rubocop.yml referred by inherit_from do not work as the Current Working Directory is not respected

@dansteele
Copy link

I get this issue too. It's wonderful most of the time but in those 1% of files where it's not folloing the .yml it becomes a pain and I can't format on save

@wingrunr21 here's my .rubocop.yml.

@mmirus
Copy link

mmirus commented Sep 17, 2020

👋 I'm running into this as well!

.rubocop.yml

inherit_gem:
  rubocop-shopify: rubocop.yml

AllCops:
  TargetRubyVersion: 2.5
  Exclude:
    - 'spec/**/*'
    - 'db/migrate/**/*'

@907th
Copy link

907th commented Feb 12, 2021

Still doesn't work for me (v0.28.1). Even with forceExclusion: true option.

Non-working rule:

Metrics/BlockLength:
  Max: 30
  Exclude:
    - "spec/**/*.rb"
    - "config/routes/*.rb"
    - "engines/member_api/config/routes.rb"
    - "engines/admin_api/config/routes.rb"

Run log:

Lint: executing bundle exec rubocop -s '/Users/laise/Work/firm/proj/spec/services/ticket_service/file_uploader_spec.rb' -f json --force-exclusion...

Error sown in editor: Block has too many lines. [62/30]Metrics/BlockLength(RuboCop)

@jensljungblad
Copy link

jensljungblad commented Mar 4, 2021

Yep, stopped working for me again, even with forceExclusion... Running rubocop manually with --force-exclusion works though. Is the option not being passed to the command correctly?

@FooBarWidget
Copy link
Contributor

FooBarWidget commented Mar 25, 2021

I think I found the cause of this bug. See #717.

Update: looks like 717 isn't the problem after all. The root cause is #719.

@ssoulless
Copy link

In my .rubocop.yml I have this

Metrics/BlockLength:
  IgnoredMethods: ["describe", "context"]

And in my VScode settings Json I have this

"ruby.lint": {
        "rubocop": {
            "forceExclusion": true
        }
    }

it still shows the Block has too many lines. Metrics/BlockLength(RuboCop) in the editor.

@jensljungblad
Copy link

I happened to notice something regarding this. The forceExclusion: true actually does work if I also turn off ruby.useLanguageServer. Seems like the combination of those two options (for me at least) causes the problem.

@mmirus
Copy link

mmirus commented Jul 1, 2021

The forceExclusion option is no longer solving this for me, unfortunately. Disabling ruby.useLanguageServer does stop the problem, but that's obviously not a good solution.

@fedeth
Copy link

fedeth commented Jul 13, 2021

The forceExclusion option combined with "useBundler": true works for me.

@ghost ghost mentioned this issue Jul 16, 2021
3 tasks
@peternixey
Copy link

peternixey commented Sep 14, 2021

I had the same problem but noticed that when it was run in Visual Studio, Rubocop wasn't referencing the same root directory as my project. So I tried making the exclusion root-independent and it then worked fine:

# Doesn't work :(
AllCops:
  Exclude:
    - db/schema.rb
# Works :)
AllCops:
  Exclude:
    - */schema.rb

@heragu
Copy link

heragu commented Nov 9, 2021

@peternixey I tried your solution and works in vs code but then it does not work in the command line. When I tried to run in the terminal bundle exec rubocop I got an error like the following:

/path/to/your/project/.rubocop.yml): did not find expected alphabetic or numeric character while scanning an alias at line 5 column 7
/path/to/your/home/.rbenv/versions/2.7.1/lib/ruby/2.7.0/psych.rb:456:in `parse'
/path/to/your/home/.rbenv/versions/2.7.1/lib/ruby/2.7.0/psych.rb:456:in `parse_stream'
/path/to/your/home/.rbenv/versions/2.7.1/lib/ruby/2.7.0/psych.rb:390:in `parse'
...

This helps while developing with VS code but if you have rubocop integrated in your deployment pipeline it is not a final solution.

@peternixey
Copy link

@heragu - it actually doesn’t even work in Vscode any more for me. I find anything to do with Ruby (solargraph etc too) a bit of a black art to get working!

@evgenyneu
Copy link

The "forceExclusion": true solution does not work for me. What's the status of this bug? Is anyone working on it?

@shrkw
Copy link

shrkw commented Apr 19, 2022

This behavior is really strange. I created experiment project: https://github.com/shrkw/vscode-ruby-dev-env-sample , and rubocop and "Exclude" settings works very well yesterday.
I opened it today, "Exclude" settings does not work at all!

スクリーンショット 2022-04-19 16 10 04

Debug Output view for "Ruby Language Server" shows rubocop has correct option...

Lint: executing bundle exec rubocop -s '/Users/shirakawa2/Development/sandbox-ruby-vscode/src/apple.rb' -f json --force-exclusion...
Lint: executing bundle exec rubocop -s '/Users/shirakawa2/Development/sandbox-ruby-vscode/src_excl/cherry.rb' -f json --force-exclusion...

and same command returns no offenses.

{"metadata":{"rubocop_version":"1.27.0","ruby_engine":"ruby","ruby_version":"2.6.8","ruby_patchlevel":"205","ruby_platform":"universal.arm64e-darwin21"},"files":[],"summary":{"offense_count":0,"target_file_count":0,"inspected_file_count":0}}%      

It's really difficult to understand.

@evgenyneu
Copy link

evgenyneu commented Apr 19, 2022

I found what caused the problem, it's this setting:

"ruby.lint": {
   "rubocop": {
      "useBundler": true // enable rubocop via bundler
   }
}

I removed it and it worked! It should be ruby.rubocop.useBundler and NOT ruby.lint.rubocop.useBundler.

@benzado
Copy link

benzado commented Apr 20, 2022

Since this is an old issue with lots of comments, it's possible there are multiple bugs being represented here. However, I strongly suspect this issue is a duplicate of #578 and #719, and is fixed by PR #720. However, the fix hasn't made it into a release yet.

senhalil added a commit to senhalil/vscode-ruby that referenced this issue Jun 16, 2022
@senhalil senhalil linked a pull request Jun 16, 2022 that will close this issue
@senhalil
Copy link

Since this is an old issue with lots of comments, it's possible there are multiple bugs being represented here. However, I strongly suspect this issue is a duplicate of #578 and #719, and is fixed by PR #720. However, the fix hasn't made it into a release yet.

@benzado if I am not mistaken PR #720 fixes only language-server-ruby/src/linters/ and this issue is related to language-server-ruby/src/formatters/ (which has exactly the same issue -- extra single quotes). I created PR #825 and appreciate it if you can review it.

@qwell
Copy link

qwell commented Dec 8, 2022

Even if PR #825 gets merged and doesn't work, we're no worse off than we are right now with 0.28.1. Could #641 even be reproduced at this point, given the 78 (I counted) releases rubocop has had since August 01, 2020?

Rather than dealing with the fallout of that commit for 2 more years, can PR #647 just be reverted until it can be shown that there actually is a problem in the first place?

@qwell
Copy link

qwell commented Dec 8, 2022

#840 (which could supersede #825 - although @senhalil should feel free to merge this and let things continue over there) finishes reverting all of the changes from #647, which hadn't already been reverted elsewhere.

@celesteking
Copy link

You should really also document the pattern syntax. Whether it's a Dir.glob or fnmatch, or shell glob, explicitly state so. If it's a custom syntax, document it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug reports
Projects
None yet