SonarQube Server Enterprise Edition v2025.4.2 and Visual studio IDE Version: 9.1.0.15828.
Make numbers for new code on main branch correct.
setting sonar.projectVersion to $(Build.SourceVersion) (git hash)
We have projects that follow the git workflow with short lived branches.
We keep the released versions in the main branch and do new developments in feature branches that are regularly merged to main with pull requests.
We do SonarQube analyses on main, branches and when doing a pull request.
Analyses on branches and PR are compared to main with âsonar.newCode.referenceBranch=mainâ
The main branch will compare with Previous version for new code detection.
When building we set sonar.projectVersion=$(Build.SourceVersion) for all builds.
The issue is that when we build main the new code detection is wrong and therefore are the numbers for Coverage and Duplications incorrect.
I can see this because the number of âNew Lines to coverâ i very low compared to the PR or a build of the feature branch.
For instance last time the main build said âOn 28 new lines to coverâ but PR build said âOn 222 New Lines to coverâ.
When looking at the code tab leaf in SonarQube I also only see very few lines marked as new code and I know other lines was modified that are not being shown.
So clearly when I build main it is not comparing to the right previous version.
When I look in SonarQube Overview tab leaf for the main branch I see the âNew Code Since âŚâ and this is listing the correct previous version that I would like to compare with.
So how can it be that SonarQube is showing the right âNew Code Since âŚâ version but it does not list the changes since that version ?
Does SonarQube store a copy of the source code for the âPrevious versionâ
Could it be a problem that I set sonar.projectVersion for all builds ?
Does Previous version always refere to a build of the same branch I am guessing so since âNew Code Since âŚâ is showing the correct previous version?
Could it be an issue that my version number is a large hash value and you can not compare with others and see if it is newer or older?
Thanks for the answer however I am still confused about this.
When I use âPrevious versionâ SonarQube will not look at the branch that I build but any build that change the sonar.projectVersion is now what will be compared to.
I had assumed that setting sonar.newCode.referenceBranch=main and building a different branch would cause it to not be included as Previous version on main.
I see this makes sense, but then I do not understand why the Overview tab would not say âNew Code Since (version number used on the the last build on a feature branch)â but instead it says the last build on the main branch.
The last time build that was not a pull request that set sonar.projectVersion would have been âf80ed3âŚâ.
I also do not understand why there are any new lines of code if SonarQube is comparing to the last build on the feature branch. The diff with that is empty.
When you set the new code definition for any branch to âprevious versionâ then whatâs ânewâ is what has changed since the sonar.projectVersion string changed. Whether itâs been one build or 200 since the string changed.
I have tried to remove the projectVersion when a branch is being build, but this still does not work.
It looks like if I do not set sonar.projectVersion it just defaults to 1.0.
I did a test where the GIT updates look like this.
The Merged commits are on the main branch and the three other commits are on the new branch. I did two analysis on the new branch that used sonar.newCode.referenceBranch=main and no projectVersion.
When I view the main branch afterwards The new code is now the code that was added in the two commits âNew memberâ and âSecond new lineâ but NOT âAdded new codeâ.
This makes no sense to me why these two would be the new ones.
I do not understand how I can make the âNew codeâ on main the changes since the last pull request to main. We are not allowing commits directly to main.
Also UI is showing. âNew Code Since 87c8940f33efc92f96d9055109191148ef0dcb18â
Yes, you must have a sonar.projectVersion value. If you donât provide one, it will default.
Since youâve set the main new code definition to âprevious versionâ then other branches have nothing to do with whatâs showing up in main. Beyond that, Iâm not able to follow what youâre doing and what youâre getting. Can you restate this for me while discussing only main and not mentioning any other branches?
In the example above there are only 2 builds (SonarQube analyses) on main.
In the first analysis the version number is 87c8940f33⌠and in the second one it is 46186be548âŚ
So it was changed. There are 3 commits between the two builds.
If with runs you mean SonarQube analysis then there are only 2 runs on the main branch.
There is one that was done november last year. In this one sonar.projectVersion was set to 87c8940f33efc92f96d9055109191148ef0dcb18. This is also the GIT hash of the âmerge commitâ that the analysis was done on.
The second one was done yesterday. In this one sonar.projectVersion was 46186be5482f06ef0550769106b2283f799d2d4b. This is again the GIT hash of the merge commit that added the three commits.
When the 3 commits are merged into main we use Merge (no fast forward). So it is nonlinear history.
Only the changes in 2 of the 3 commits are shown in SonarQube as new code.
The analysis / scanner log is whatâs output from the analysis command. Hopefully, the log you provide - redacted as necessary - will include that command as well.
I did a re-run of the last build with verbose.
So this is a build with version 46186be5482f06ef0550769106b2283f799d2d4b again.
The âAdded new codeâ commit is still not shown as ânew codeâ in SonarQube. Run Code Analysis verbose.txt (215.6 KB)
I donât see anything unusual about the SCM data (altho â493 files ignored because of scm ignore settingsâ does seem like a lot.)
The log also confirms your revision id of 46186be5482f06ef0550769106b2283f799d2d4b. So, when you look in Git, what are the changes between this revision and the previous one?
There are actually 6 lines with diff in the screenshot. There is a new line in the first block also. Not sure I understand what you mean with âThat⌠tracks for me.â ?
The initial post was on a different application and build.
After I changed to not set sonar.projectVersion when building a branch, I did another simple test that showed the same issue.
SonarQube shows only 3 of the 6 new lines and the 3 lines SonarQube did NOT detect as new code looks like this in SonarQube: