All New code not detected when using Previous version

Must-share information:

  • 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.

  1. 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”

  2. Could it be a problem that I set sonar.projectVersion for all builds ?

  3. 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?

  4. 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

Hi,

You’ve told SonarQube that the code added/edited since the version string changed is what’s “new”.

And you change the version string with every analysis.

So this is pretty much expected.

To get the result I think you’re looking for, you need to re-evaluate your sonar.projectVersion schema.

 
HTH,
Ann

Hi

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.

This is what the main branch build looks like:
image

And this is the builds:

The last build on the feature build that was merged into main looks like this:

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.

git diff 34bf6f9fdb83ec3fa111b0d7037bf0698034ad38 f80ed33d03907835e58fbdc9c7f5750bcc30bc9b

This commands shows no change because nothing was changed since that last build on the feature branch.

Hi,

I’m a bit confused at this point.

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.

 
Ann

Hi

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”

/Thanks

Hi,

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?

 
Thx,
Ann

Hi

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.

  1. Added new code
  2. Second new line
  3. New member

The first commit looks like this in DevOps:

In SonarQube the three lines are not highlighted as new.

The changes in 2. and 3. commit is highlighted as new code.

/Kennet

Hi,

And what were the sonar.projectVersion values for these 3 runs?

 
Ann

Hi

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.

/Kennet

Hi Kennet,

Can you share your analysis log?

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.

This guide will help you find them.

 
Ann

Hi

I have attached the output from DevOps when the “Run Code Analysis” task was preformed in the build yesterday.

Run Code Analysis.txt (30.5 KB)

I cleared some information that might be a bit sensitive with stars.
The analysis was not executed with “sonar.verbose=true”.

/Kennet

Hi Kennet,

Can you give me a verbose log, please?

 
Thx,
Ann

Hi

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)

/Kennet

Hi Kennet,

Thanks for the log.

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?

 
Thx,
Ann

Hi

This is the output from GIT diff of the two versions.

The 493 files ignored are mostly from packages we are using, some of them are analysed separately.

Hi,

We started here:

Your diff screenshot shows 5 lines changed. That… tracks for me.

 
Ann

Hi

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:

Hi,

And if you click on your username in the left margin, what SCM data do you see there?

 
Ann

Hi

This show the GIT hash (839f…) of the commit with “Added new code”

/Thanks

Hi,

And what does the project homepage show for the New Code start?

 
Thx,
Ann