Old code is now considered new by SonarCloud

Recently we started having issues with SonarCloud incorrectly marking entire source code files as new code even though only a few lines were changed by a PR. This in turn makes our quality gate fail on code duplication measure. Any help would be appreciated. Example: https://sonarcloud.io/component_measures?id=AssureCare.MedCompass.Web&metric=new_lines&pullRequest=2385&selected=AssureCare.MedCompass.Web%3Asrc%2FAssureCare.MedCompass.Web%2FServices%2FAdmin%2FCMService.cs&view=list

The PR only added 20 lines and removed 2 lines from this file: CMService.cs +20 -2

We are going to take a look at this, the behaviour is indeed strange. Do you observe this behaviour on every PR? Was it working correctly at some point of time for your project?

Yes, this started happening on every PR recently. It was working correctly since we adopted SonarCloud back in December. I think it started happening some time in March. Thank you so much for looking into it

Hi,

Could you provide us the logs of the scanner, in one of the runs that give unexpected results? If you could run it with debug enable, even better.

Just to confirm this is happening for us too

We are looking at all the cases reported here in the forum. We do need the logs of the scanner in debug to look into possible causes. The main thing to check is if SCM is being used to detect new lines and if that’s the case, if the configuration and local references are correct.

I am just running a build with verbose on now. Can you explain a bit more about SCM? How do you go about turning it on and off, or is that something out of our control, what does using it do?

Thanks,
Pete

When possible, the scanner checks which lines were changed since the fork point between the base and target branches. It does it by loading information from supported SCMs such as git or svn.

This is what gets logged around SCM change detection:

00:12:38.164 INFO: Pull request 2552 for merge into master from feature/234453-fix-duplicate-external-provider-services
00:12:38.180 INFO: SCM collecting changed files in the branch
00:12:38.211 DEBUG: readpipe [git, --version],C:\agents\2.149.2\externals\git\cmd
00:12:38.242 DEBUG: readpipe may return 'git version 2.18.0.windows.1'
00:12:38.242 DEBUG: remaining output:
00:12:38.258 DEBUG: readpipe [git, config, --system, --edit],C:\agents\2.149.2\externals\git\cmd
00:12:38.305 DEBUG: readpipe may return 'C:/agents/2.149.2/externals/git/mingw64/etc/gitconfig'
00:12:38.305 DEBUG: remaining output:
00:12:38.414 DEBUG: Merge base sha1: b596a0bd9647c20d10a3d417e9f2a57f4eed916a
00:12:38.445 INFO: SCM collecting changed files in the branch (done) | time=265ms
00:12:38.461 DEBUG: SCM reported 1 file changed in the branch

Please let me know if you’d like me to attach the entire log

Thanks Kiryl.
Is the commit with sha1 b596a0bd9647c20d10a3d417e9f2a57f4eed916a the correct fork point (last common commit) between master and feature/234453-fix-duplicate-external-provider-services?

It’s hard to tell as the feature branch has been already deleted. But comparing these two commits produces the correct diff