I cannot configure “New Code” as I wish, I need help.
I am using SonarQube Server: Developer Edition v2025.1.1 (104738). I don’t know how it is deployed.
What I am trying to achieve:
On a dev branch, I want to consider as “new code”, the difference with the branch master. If I push a new commit to this branch or if I amend this commit to fix something, I want the analysis to compare the code of my branch (all the commits on my branch) with master. My Quality Gate only have conditions on New Code.
How I configured Sonarqube:
The baseline chosen for new code in the settings is “Reference branch” and I chose the branch master. The first commit is always compared to master and it is ok. The next commits are always compared to the previous one and not the master.
What I have tried so far to achieve this:
I have tried to add new arguments in the dotnet sonarscanner begin command :
/d:sonar.branch.name=%MyBranchName%
/d:sonar.newCode.referenceBranch=master
/d:sonar.scm.forceReloadAll=true
I have also tried the other baselines for new code (version and number of days) but even the first analysis did not recognize anything as new code.
Configuring the “New Code Period” is all about setting a baseline, after which all changes are considered “New Code”. The Reference Branch New Code Period ensure that changes made before the first analysis (establishing the baseline) fall into the New Code Period, and does this by comparing the new branch to the reference branch.
That sounds like it’s working as expected, although I’m not quite sure what you mean by the your last sentence. Are your new commits / amended commits not falling into the New Code Period? What are you expecting to see compared to what’s being shown? Some specific examples would help.
Here’s a simple example:
In the first commit of a branch, 314 code smells are introduced. In the New Code section, I can see the exact number of code smells added.
Then, in the second commit, I fix 2 of these code smells. In New Code, I now see “0 New Issues”.
I don’t know if this is expected behavior, but I was hoping to see the combined data from both commits in the New Code tab — meaning that this branch introduced a total of 312 code smells.
In the analysis history, I can see that 2 code smells were fixed, but I can’t see how many were added in the first commit.
I was kind of hoping it would be something about collecting blame data.
I don’t think you need to worry about the commands you’re using. Analysis seems to happen correctly, it’s just that everything seems to fall out of the new code period on your next analysis.
I would like to see what these log lines look like both the first and second analysis (with /d:sonar.verbose=true). They’re towards the end.
17:30:53.060 INFO SCM revision ID '12313b6be2852ff5502412498dc24a694214124'
17:30:53.099 INFO SCM writing changed lines
17:30:53.102 INFO Merge base sha1: 7095143b6be2852ff5502a298dc24a6928f7e878
17:30:53.109 DEBUG SCM reported changed lines for 3files in the branch
17:30:53.109 INFO SCM writing changed lines (done) | time=10ms
Specifically I’ll be interested to see if the same Merge base sha1 is detected for both runs.
I used this command line dotnet sonarscanner begin /k:myProjectName/d:sonar.login=MySonarqueToken /d:sonar.host.url=MySonarUlr/d:sonar.cs.opencover.reportsPaths=./TestResults/**/*.xml /d:sonar.cs.vstest.reportsPaths=./TestResults//*.trx /d:sonar.sources=front/src /d:sonar.qualitygate.wait=true /d:sonar.verbose=true /d:sonar.scm.forceReloadAll=true /d:sonar.branch.name=MyBranchName /d:sonar.newCode.referenceBranch=master
In your server-side settings, is it possible sonar.scm.disabled is set to true? Global Administration > SCM > Disable the SCM Sensor (you can also find this at the project-level).
You can force it back on with /d:sonar.scm.disabled=false.
Without SCM data, SonarQube cannot reliably detect new code. You’re using, git, right?