rubocop: Cops with "Include" param in default config break Ale integration
In my opinion this is not a bug, but a request for advice on how to get the ALE plugin for vim running better with RuboCop.
Currently, the default configuration has an Include param for a few cops (all Rails related as far as I can see), e.g.:
Rails/InverseOf:
Include:
- app/models/**/*.rb
So if I copy a file from e.g. app/models/user.rb to tmp/models/user.rb, the InverseOf cop will not run on it, since Include matches relative to the .rubocop.yml, and not simply on the filename.
What’s worse, if there is a rubocop:disable Rails/InverseOf directive, it will be automatically deleted. This happens because the cop is not running, so the Lint/UnneededCopDisableDirective / Lint/UnneededDisable cop thinks the disable is unneeded.
All of this is very understandable, but it breaks in combination with the popular ALE linter / fixer for vim. The way it works is that it write the current buffer to a file under /tmp/some_tmp_dir/some_tmp_dir/filename.rb and runs rubocop on that file, with the correct .rubocop.yml and using the --stdin option. (Note that it does not write to /tmp/some_tmp_dir/some_tmp_dir/app/models/filename.rb, which might have to be fixed in ALE).
A workaround would be to overwrite Include in .rubocop.yml for every affected cop, but then that will need to be done manually for new cops, and I think currently absolute paths are not supported.
A different idea would be try and match the Include pattern to the of the inspected file path (combined from the root .rubocop.yml), but that would probably be some implentation effort, because the current File.fnmatch? logic does not support it.
Third idea: remove the Includes from the default config. I have no idea what side effects this will have, apart from a very small perfomance impact.
Do you have any ideas on how to improve on this?
For reference, an example issue on ALE.
Expected behavior
RuboCop runs with the same configuration will lint a file the same regardless of it’s location.
Actual behavior
Some cops are only enabled only for certain file patterns.
Steps to reproduce the problem
In some directory:
mkdir -p app/models
mkdir -p /tmp/app/models
cat > app/models/my_model.rb <<MODEL
class MyModel < ApplicationRecord
# rubocop:disable Rails/InverseOf
has_one :something
# rubocop:enable Rails/InverseOf
end
MODEL
cat > /tmp/app/models/my_model.rb <<MODEL
class MyModel < ApplicationRecord
# rubocop:disable Rails/InverseOf
has_one :something
# rubocop:enable Rails/InverseOf
end
MODEL
cat > .rubocop.yml <<RUBOCOP
AllCops:
DisabledByDefault: true
Rails/InverseOf:
Enabled: true
RUBOCOP
rubocop app/models/my_model.rb
rubocop /tmp/app/models/my_model.rb
RuboCop version
Include the output of rubocop -V or bundle exec rubocop -V if using Bundler. Here’s an example:
0.58.2, but I suspect it affects many / all versions.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 2
- Comments: 19 (7 by maintainers)
Here is some activity.
Here is some activity. Don’t use bots to close issues automatically. It doesn’t make sense, and there’s nothing wrong with having a lot of issues open. ALE has 293 open issues. The oldest issue is from February 2017, and it’s still valid. Software issues don’t go “stale.” The concept is ridiculous. They remain until someone feels like resolving them.
I believe this issue is resolved since
--stdintakes a path argument.