'Rubocop: Exclude files from a list given on command line
I run rubocop from my pre-commit hook script:
bin/bundle exec rubocop ${FILES}
with ${FILES} being the list of files staged for commit. This does not, however, honour the contents of the AllCops/Exclude section in rubocop.yml.
Since there are files that will always generate offences (e.g. schema.rb), is there a way to make the config Exclude section precede the command line parameters?
Solution 1:[1]
I edited my pre-commit script to generate a temporary Rubocop config, that inherits from the main one. The temporary config only contains the AllCopsInclude section, which seems to work fine with AllCops/Exclude in the main config. No need to tamper with inherit_mode.
TEMP_CONFIG=/tmp/rubocop-`date +%s`.yml
RUBOCOP_CONFIG=`realpath .rubocop.yml`
cat << EOF > $TEMP_CONFIG
inherit_from: $RUBOCOP_CONFIG
AllCops:
Include:
EOF
for file in "${FILES[@]}"; do
echo " - $file" >> $TEMP_CONFIG
done
bin/bundle exec rubocop -a -c $TEMP_CONFIG
rm $TEMP_CONFIG
Solution 2:[2]
Not that I know, but I agree there should be (or it should be easier).
A few issues are already opened about that. Maybe splitting that setting in two (e.g. with a AllCops/AlwaysExclude or a AllCops/ExcludeByDefault) is needed.
There is a inherit_mode option but I'm not 100% sure how it works or if it could help
Solution 3:[3]
rubocop has command-line option --force-exclusion which excludes files specified in the .rubocop.yml Exclude section. This was added on pull request 922.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ohaleck |
| Solution 2 | Marc-André Lafortune |
| Solution 3 | sunzoje |
