How to disable Sonar rules for specific files? - sonarqube

I've got a project I'm working on and some of the files violate some of the rules, but in ways that are not real issues, and are thus distracting noise. However, I don't want to disable these rules globally, and I would prefer not to have to mark 'em as false positives one by one.
Is there a way to disable Sonar rules for specific files, and if so, how?

Since SonarQube 4.0, you can define issue exclusion patterns based on rule key and file path pattern.
On previous versions, you can rely upon the Switch Off Violations plugin.

You can annotate a class or a method with SuppressWarnings.
Here is an example:
#java.lang.SuppressWarnings("squid:S00111")
squid:S00111 in this case is a Sonar issue ID. You can find this issue id from the Sonar web ui.

Building on Mithfindel's answer and dpk's comment, this removes the warning
System.out and System.err should not be used as loggers
from all classes in packages named log (or any subpackage thereof) by adding an ignore pattern for the rule squid:S106:
For a list of the keys to all your rules go to your profile under Quality Profiles and select Download to get a .csv file containing all the rules' keys.
I'm using SonarQube version 4.1.1.

You can set specific files and rules under sonar-project.properties with following contents:
# Ignore Issues
sonar.issue.ignore.multicriteria=e1,e2
# Skip Bold Check
sonar.issue.ignore.multicriteria.e1.ruleKey=Web:BoldCheck
sonar.issue.ignore.multicriteria.e1.resourceKey=**/*.html
# Skip Tag Check
sonar.issue.ignore.multicriteria.e2.ruleKey=Web:S1234
sonar.issue.ignore.multicriteria.e2.resourceKey=**/*.html
Change the ruleKey and resourceKey based on your specific need.

Using below annotation we can ignore the rule from the specific files
For one rule
#java.lang.SuppressWarnings("squid:S2696")
#Slf4j
#Service
public class PaymentServiceImpl implements PaymentService {....
For more than one rule
#java.lang.SuppressWarnings({"squid:S2696", "squid:S1172", "squid:CommentedOutCodeLine"})
#Slf4j
#Service
public class PaymentServiceImpl implements PaymentService {...

Yes, it is possible.
1.Goto Administration tab->Analysis Scope->Issues
2.There , you will find "Ignore Issues on Multiple Criteria".
3.Provide Rule ID in "Rule Key pattern" textbox [Rule ID can be found by clicking on the particular rule and find it in top right corner]
4.Provide Filepath for which you need to ignore rule in "File Path Pattern" textbox
5.Click on Save Issues settings
Image to know where Rule ID will be present in the page
Image where Rule key pattern and File Key pattern text boxes are present

Related

Configure sonar.issue.ignore.multicriteria for multiple paths

I want to disable a SonarQube rule for multiple files at different paths. Also I want to make this configuration within the pom.xml and to through the Sonar UI.
I couldn't find any clue on how to do this. My best guess is that I have to create multiple rules:
<sonar.issue.ignore.multicriteria>e1,e2</sonar.issue.ignore.multicriteria>
<!-- disable rule on naming convention -->
<sonar.issue.ignore.multicriteria.e1.ruleKey>
java:S117
</sonar.issue.ignore.multicriteria.e1.ruleKey>
<sonar.issue.ignore.multicriteria.e1.resourceKey>
firstPath
</sonar.issue.ignore.multicriteria.e1.resourceKey>
<sonar.issue.ignore.multicriteria.e2.ruleKey>
java:S117
</sonar.issue.ignore.multicriteria.e2.ruleKey>
<sonar.issue.ignore.multicriteria.e2.resourceKey>
secondPath
</sonar.issue.ignore.multicriteria.e2.resourceKey>
I was wondering if there was a more compact way to do this. I was thinking of something like this:
<sonar.issue.ignore.multicriteria>e1</sonar.issue.ignore.multicriteria>
<!-- disable rule on naming convention -->
<sonar.issue.ignore.multicriteria.e1.ruleKey>
java:S117
</sonar.issue.ignore.multicriteria.e1.ruleKey>
<sonar.issue.ignore.multicriteria.e1.resourceKey>
firstPath, secondPath
</sonar.issue.ignore.multicriteria.e1.resourceKey>
However I have no idea if this syntax will be accepted or not. I guess I should try it, but I'd rather not since in order to test it I would need to push the change to my CI server.
This seems not to be supported according to config in answer https://stackoverflow.com/a/63632140/7251133
I agree it would be really nice when lists would be allowed in rule and resource.

What NOREPLACE means at netflix's nebula plugin?

I was using netflix's nebula. Looking here, I saw this line:
fileType [org.freecompany.redline.payload.Directive] - Default for types, e.g. CONFIG, DOC, NOREPLACE, LICENSE
I didn't find any doc about the actual meaning of this enum, but I've found the original code.
Now I want an actual description of this enum. I thought NOREPLACE is releated to being not allowed to replace the file. But I want to be sure and don't rely on assumptions.
I have only seen noreplace as an additional attribute on a config file, e.g. %config(noreplace). It means that if the user has edited the file, the installer should put its new version as filename.rpmnew; by default %config files are replaced with the user one put as filename.rpmold .

It this the correct way of extending eslint rules?

In my eslint config (YAML format), I'm extending 3 different configurations:
extends:
- airbnb-base
- plugin:angular/johnpapa
- ionic
My questions are as follows:
Is this the correct format in YAML?
Some of these extensions have overlapping rules (or multiple of them extend eslint:recommended): will I get the same error multiple times if the error is related to one of these "shared" rules?
At first, Yes, it's the correct format in YAML (see for example ESLint - Configuring Plugins). As JSON, it would be
{
"extends": [
"airbnb-base",
"plugin:angular/johnpapa",
"ionic"
]
}
If you have multiple rulesets in your extend section, each following ruleset will extend or overwrite the previous ones. So you will only have one setting for each rule (see ESLint - Extending Configuration Files) Sometimes when the rules from the shareable configs are conflicting and you can't define a specific order for the extend section you have to manually define this specific rule in you rules section.
So the answer to you second question is: No, you won't get the same error multiple times.
The correct way to extend eslint rules is like so:
extends: ["standard", "plugin:jest/recommended"]

Custom Symfony2 Validator Constraint Messages

I want to use custom error messages for validation constraints on dozens of fields in my project.
I do not want to set the message on every one of these, because that would be a blatant violation of DRY. Repeating the same string in every declaration like: #NotNull(message="custom msg") would mean that if I decide to change the message in the future I'd have to hunt them all down replace them, but even worse I might use the wrong string on some of them and be inconsistent.
How do you deal with this?
Is the best option really to extend every symfony stock constraint and set my default there, and use my own custom annotation class?
Please note that using the translator is not an option for me, so I am looking for a solution that does not include the symfony translation component.
Thanks a lot for the help in advance.
Assuming you are working with English as the app's language, and you've configured translator: { fallback: en }, you can override these constraint messages universally. Start by creating this file: app/Resources/translations/validators.en.yml
And use the following translation format:
This value should not be blank.: Your custom message here
Whatever the standard message is.: Another custom message
This also works for any other language setting, assuming you've made a validators.lang.yml for it!
You can also place this file in your bundle directory under Resources/translations, and a few other places.
You can read more about this here!

MVC3 localization code generator

I am trying to add two localization resource files into my MVC3 App_GlobalResources.
I add Global.resx and then Gloabl.fr.resx. However, only the first one generates code in .designer.cs. The second one just generate an empty file.
Can anybody help me out?
Thanks a lot.
That is how its supposed to work. The .designer.cs class is a strongly typed class so that you can type.
#Global.mystring and it will return a localised (depending on the UICulture) string.
The designer file doesn't actually contain the localised strings, it just contains a bunch of properties which (in turn) return the localised string.. this is why you wouldn't need more than one class.
Perhaps you are trying to find a way of retrieving the resources for different cultures e.g. fr?
You need to set the UICulture to "fr". Either manually or by setting the following element in the web config:
<globalization culture="auto" uiCulture="auto"/>
This would do it automatically based on your browser settings

Resources