Hello,
At my workplace we are experiencing problems with analysis of .NET projects, in which code duplication detection seems to be unaffected by the sonar.cpd.cs.minimumtokens and sonar.cpd.cs.minimumLines setting. These setting are correctly appearing for each scan in “Project Settings → Show Background Tasks → Scanner Context”, but it does not seem to affect the analysis: if I set high values (1000 tokens and 100 lines), it keeps detecting the duplicated blocks, and the other way around: with small values of 5 tokens and 2 lines, two identical functions and only 4 lines are not detected.
How can I troubleshoot this problem? Are there other settings affecting duplicate code detection?
I’ve prepared sample repository here. There are main and feature branch, both have sonar.cpd.cs.minimumTokens and sonar.cpd.cs.minimumLines settings set to a high number (1000) yet when analysis performed (either on Community edition or Developer edition with Pull Request Analysis) it reports the duplicate code as if the settings are ignored.
Hi @mb_centaurea, welcome to the community and thanks for the reproduction!
I believe there may be a typo in the command being used.
sonar.cpd.cs.minimumtokens vs sonar.cpd.cs.minimumTokens
Please note the camel case.
Setting the value using the .csproj only works for some parameters and this is not one of them.
I’ve tried analysing your solution using the following with v9.0.0 of the scanner and uploading to SonarQube Server and SonarQube Cloud.
dotnet .\SonarScanner.MSBuild.dll begin /o:"alexmeseldzija" /k:"alexmeseldzija_cpd-test" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.token="<SONAR_TOKEN>" /d:sonar.cpd.cs.minimumTokens=1000 /d:sonar.cpd.cs.minimumLines=1000
You can see the result with no duplications on sonarcloud here.
If you go to the overview for that project you can also see how the duplications change when I switched the sonar.cpd.cs.minimumTokens && sonar.cpd.cs.minimumLines to 2 each.
Can you re-run your analysis with sonar.cpd.cs.minimumTokens and confirm it works?
If not could you please share the logs from the scan?
Add /d:"sonar.verbose=true" to the SonarScanner.MSBuild.exe or dotnet sonarscannerbegin command to get more detailed logs
For example: SonarScanner.MSBuild.exe begin /k:"MyProject" /d:"sonar.verbose=true"
I tried executing the scan with the parameters provided in the command line and seems like that worked, thanks! Unfortunately I can’t seem to find in the documentation what parameters can’t be specified in the .csproj file. In case if project contains multiple languages, should all parameters be specified in the command line?
It seems there is a typo in the documentation where I copied it from (at Analysis parameters & SonarQube), do you happen to know where I can report that to be fixed?