- Developer Edition v2025.1 (102418), MQR Mode
- SonarQube deployed: Docker
- I am trying to create incremental scaning
Dear SonarQube Support,
I am encountering an issue during pull request analysis using dotnet-sonarscanner
in a TeamCity CI environment.
Problem Summary
We receive the following warning in the Sonar logs:
WARN: Could not find ref 'refs/heads/develop' in refs/heads, refs/remotes, refs/remotes/upstream or refs/remotes/origin
As a result:
Cache is working
Downloading cache. Project key: Test2.BE, branch: refs/heads/develop.
Incremental PR analysis: 12041 files out of 12104 are unchanged.
Pre-processing succeeded.
Changed files are not detected
Incremental PR analysis is working
Context
My script is here:
#!/bin/bash
set -o xtrace -o pipefail -o errexit -o nounset
SONAR_PROJECT_KEY=Test2.BE
SONAR_TOKEN=*******
SONAR_URL=http://some_url.net:9000/
SONAR_QGATE_WAIT=true
TEAMCITY_BUILD_BRANCH=merge-requests/5231
TEAMCITY_TARGET_BRANCH_SHORT=develop
TEAMCITY_SOURCE_BRANCH_SHORT=Sonar-test
TARGET_BRANCH_REF="refs/heads/$TEAMCITY_TARGET_BRANCH_SHORT"
SOURCE_BRANCH_REF="refs/heads/$TEAMCITY_SOURCE_BRANCH_SHORT"
echo "Using branch name: $TEAMCITY_BUILD_BRANCH"
echo "Target branch ref: $TARGET_BRANCH_REF"
echo "Source branch ref: $SOURCE_BRANCH_REF"
apt update
apt-get install --yes openjdk-17-jre
dotnet tool install --global dotnet-sonarscanner
dotnet tool install --global dotnet-coverage
# === Git config & fix for shared reference clones ===
git config --global --add safe.directory '*'
git remote set-url origin "https://oauth2:${GITLAB_TOKEN}@gitlab.test.com/as/oo/test2.be.git"
echo "=== Cleaning up possible Git reference issues ==="
if [ -f .git/objects/info/alternates ]; then
echo "⚠️ Detected alternates file in .git — removing to avoid shared object issues..."
cat .git/objects/info/alternates
rm .git/objects/info/alternates
else
echo "✅ No alternates file detected — OK"
fi
# === Fetch full refs from origin ===
git fetch --unshallow || echo "Not a shallow clone"
git fetch origin "+refs/heads/*:refs/remotes/origin/*"
# Ensure local target branch exists and is checked out
echo "Ensuring local branch for $TEAMCITY_TARGET_BRANCH_SHORT exists and checked out"
git fetch origin "$TEAMCITY_TARGET_BRANCH_SHORT"
MERGE_BASE=$(git merge-base HEAD origin/$TEAMCITY_TARGET_BRANCH_SHORT)
echo "MERGE_BASE: $MERGE_BASE"
# === Sonar analysis ===
if [[ $TEAMCITY_BUILD_BRANCH == merge-requests/* ]]; then
PULL_REQUEST_ID=$(echo $TEAMCITY_BUILD_BRANCH | cut -d'/' -f2)
echo "Running Sonar analysis for merge request"
echo "MR IID: $PULL_REQUEST_ID"
echo "MR SOURCE BRANCH: $SOURCE_BRANCH_REF"
echo "MR TARGET BRANCH: $TARGET_BRANCH_REF"
/root/.dotnet/tools/dotnet-sonarscanner begin \
/k:$SONAR_PROJECT_KEY \
/d:sonar.host.url=$SONAR_URL \
/d:sonar.token=$SONAR_TOKEN \
/d:sonar.qualitygate.wait=$SONAR_QGATE_WAIT \
/d:sonar.pullrequest.key="$PULL_REQUEST_ID" \
/d:sonar.pullrequest.branch="$TEAMCITY_SOURCE_BRANCH_SHORT" \
/d:sonar.pullrequest.base="$TARGET_BRANCH_REF" \
/d:sonar.pullrequest.provider="gitlab" \
/d:sonar.scm.revision="$MERGE_BASE" \
/d:sonar.scm.provider=git \
/d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml \
/d:sonar.scanner.scanAll=false
else
echo "Running Sonar analysis for branch"
/root/.dotnet/tools/dotnet-sonarscanner begin \
/k:$SONAR_PROJECT_KEY \
/d:sonar.host.url=$SONAR_URL \
/d:sonar.token=$SONAR_TOKEN \
/d:sonar.qualitygate.wait=$SONAR_QGATE_WAIT \
/d:sonar.branch.name="$TEAMCITY_BUILD_BRANCH" \
/d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml \
/d:sonar.scanner.scanAll=false
fi
dotnet build ./Test2.BE.sln
/root/.dotnet/tools/dotnet-coverage collect "dotnet test Test2.BE.sln --filter Category!=IntegrationTests" -f xml -o "coverage.xml"
/root/.dotnet/tools/dotnet-sonarscanner end /d:sonar.token=$SONAR_TOKEN
When I am using the values TARGET_BRANCH_REF and SOURCE_BRANCH_REF w/o “refs/heads/…” I have the next result:
Cache doesn’t work and error about refs/heads/develop not appeared anymore
Changed files are detected
Incremental PR analysis fails, it started to check all files
Our Question
What is the wrong here, or recommended approach to ensure that the base branch (e.g., refs/heads/develop) is available locally so that SonarQube can correctly analyze the diff?
- Can SonarQube work with
refs/remotes/origin/develop
directly? - Is there a supported way to avoid this checkout issue, or bypass the local
refs/heads/*
requirement?
We would appreciate any guidance you can provide.