I’d like to disable one rule for Sonar C# for my Codacy project. According to the SonarQube docs, I have to create an .editorconfig file, so I did (see here).
However, Codacy does not seem to pick up the .editorconfig file - at least the warnings that should be disabled are still there.
What do I have to do to disable certain rules for Sonar C#?
Currently, Codacy does not support disabling rules for Sonar C# through the .editorconfig file. We have proposed this feature to the Product Team for further consideration.
However, you can explicitly create a file named SonarLint.xml and configure it with only the rules that you want to include as a workaround.
Could you please describe what I have to put into SonarLint.xml to disable certain rules? Because the guys from Sonar told me that this is not possible…
Basically, the SonarLint.xml file should contain all and only the rules that you want to use in your repository.
One of our engineers developed a script that’s going to the public repository codacy-sonar-csharp, fetches all the patterns used by default in Codacy’s Sonar tool, and creates a SonarLint.xml file with them.
#!/bin/env bash
container_id=$(docker create codacy-csharp:latest)
codacy_rules_file=/tmp/codacy-rules.json
# get all the rules defined for the tool from inside the docker container
docker cp $container_id:/docs/patterns.json $codacy_rules_file
docker rm $container_id
# obtain all ids of the rules we have enabled by default
rules_ids=$(cat $codacy_rules_file | jq -r '.patterns[] | select (.enabled == false) | .patternId')
cat << EOF > SonarLint.xml
<?xml version="1.0" encoding="UTF-8"?>
<AnalysisInput>
<Rules>
EOF
for rule_id in $rules_ids
do
echo -e "\t<Rule>\n\t\t<Key>$rule_id</Key>\n\t</Rule>\n" >> SonarLint.xml
done
cat << EOF >> SonarLint.xml
</Rules>
</AnalysisInput>
EOF
Once you have the XML file, it’s just a matter of removing the rules that you don’t want to use anymore.
My mistake, I added a bugged script. Below is the correct one:
#!/bin/env bash
container_id=$(docker create codacy-csharp:latest)
codacy_rules_file=/tmp/codacy-rules.json
# get all the rules defined for the tool from inside the docker container
docker cp $container_id:/docs/patterns.json $codacy_rules_file
docker rm $container_id
# obtain all ids of the rules we have enabled by default
rules_ids=$(cat $codacy_rules_file | jq -r '.patterns[] | select (.enabled == true) | .patternId')
cat << EOF > SonarLint.xml
<?xml version="1.0" encoding="UTF-8"?>
<AnalysisInput>
<Rules>
EOF
for rule_id in $rules_ids
do
echo -e "\t<Rule>\n\t\t<Key>$rule_id</Key>\n\t</Rule>\n" >> SonarLint.xml
done
cat << EOF >> SonarLint.xml
</Rules>
</AnalysisInput>
EOF
Moreover, I attached as well a SonarLint.xml file already created for reference.