SonarQube "The main branch has not been analyzed yet" / no default branch in CI
Pull request and new-code analysis compare against a baseline on the main branch. If the main branch was never analyzed, Sonar has nothing to compare against and reports that the branch has not been analyzed yet.
What this error means
A PR scan fails or warns with "The main branch 'main' has not been analyzed yet and pull requests will appear as if they are branches" or "Could not find a default branch".
WARN: The main branch 'main' has not been analyzed yet and could not be found.
Pull requests will be analyzed as if the target was empty.Common causes
No baseline analysis exists on the default branch
PRs were analyzed but the main branch itself was never scanned, so there is no reference for new-code comparison.
The default branch name is misconfigured
The project default branch on the server does not match the repository default (for example master vs main), so Sonar cannot find it.
How to fix it
Analyze the main branch once to create a baseline
- Add a workflow trigger that runs the scan on pushes to the default branch.
- Let it complete so Sonar records a baseline analysis.
- Re-run the PR scan; it now compares against the baseline.
on:
push:
branches: [main]
pull_request:Align the default branch name on the server
In the project branch settings, set the main branch name to match the repository default so Sonar can find it.
How to prevent it
- Run analysis on pushes to the default branch, not only on PRs.
- Keep the server default branch name aligned with the repo.
- Establish the baseline before enabling gating on pull requests.