ERROR: Could not find a default branch for project with key 'xxx'. Make sure project exists

Hello sonar community,
I am working on my project to integrate sonarqube with gitlab with self host on my machine.
First step in the pipeline is to copy my repo to a static folder on my machine to avoid losing files like .sonarqube between the stages.
I create a stage where i download sonarscanner.msbuild.exe using nugget then begin my scanner by specifying the key,name,host and token.
Second stage is to build my project.
Third stage is to end the sonarscanner, but i encountered an error:
ERROR: Could not find a default branch for project with key ‘NewBranch’. Make sure project exists.
Can anyone help to solve this error?

Hey there.

What version of SonarQube are you using? This information is requested in the template post.

I think it would be great if you provided your Gitlab CI YAML file instead of describing it. :slight_smile:

1 Like

Hello Colin
I am using SonarCloud at the moment.
There’s the script:
Set-Alias sonarscanner -Value “dotnet-framework-sonarscanner.6.2.0/tools/sonarscanner.MSBuild.exe”

sonarscanner begin /k:“proj1” /n:“proj1” /d:sonar.host.url=“https://sonarcloud.io” /v:“1.0” /d:sonar.token=“sonarqube_token” /o:“mofakih” /d:sonar.verbose=true /d:sonar.branch.name=“master”

msbuild “C:/Users/techno/Downloads/MTRIAL (1)/src/mtrial.sln”

sonarscanner end /d:sonar.token=“sonarqube_token”

And does this project already exist on SonarCloud, with this key?

1 Like

No.
As I read in the documentation, the project should be created automatically on the first analysis, should not??
Because I tried to run these commands locally on my powershell command line, so the project is created normally. However, when GitLab pipeline tried to run it, it gave me this error.

That will depend on if the user running analysis has permissions to Create Projects in the organization or not.

1 Like

Can you please provide me a small explanation how to check that, because the same sonarqube token is used on my local machine and on GitLab pipeline.

Hello!
I’m experiencing the same error when trying to run the analysis in GitLab CI. And when the project does not exist in SonarCloud (first run).

  1. First I tried to run the analysis on local machine using :latest Docker image in the next ways:
    Spoiler: both option work as expected. They create a new project in SonarCloud and perform the analysis.

1.1. From outside of container:

docker run --rm -e SONAR_TOKEN="..." -e SONAR_HOST_URL='https://sonarcloud.io/' -v ".:/usr/src" sonarsource/sonar-scanner-cli -Dsonar.projectKey=px-ms-shortener

1.2. Inside the container:

docker run --rm -e SONAR_TOKEN="..." -e SONAR_HOST_URL='https://sonarcloud.io/' -v ".:/usr/src" -it --entrypoint sh sonarsource/sonar-scanner-cli

And inside I run

sonar-scanner -X -Dsonar.projectKey=px-ms-shortener -Dsonar.token=... -Dsonar.organization=... -Dsonar.qualitygate.wait=true -Dsonar.qualitygate.timeout=300 -Dsonar.javascript.lcov.reportPaths=**/lcov.info -Dsonar.tests.inclusions=**/*.spec.ts,**/*.e2e-spec.ts -Dsonar.exclusions=**/__tests__/**,**/__mocks__/**,**/coverage/**,**/node_modules/**,**/dist/**,**/*.spec.ts,**/*.e2e-spec.ts,deploy/**,docs/**,**/*.jpg,**/*.jpeg,**/*.png -Dsonar.coverage.exclusions=**/__tests__/**,**/__mocks__/**,**/coverage/**,**/node_modules/**,**/dist/**,deploy/**,docs/**,**/*.html,**/*.css,**/*.scss,**/*.jpg,**/*.jpeg,**/*.png
  1. I launched the same Docker image in GitLab CI and tried the next steps always getting the same error (error at the bottom) and the project is not created in SonarCloud:

2.1. Run the same command as in 1.2:

sonar-scanner -X -Dsonar.projectKey=px-ms-shortener -Dsonar.token=... -Dsonar.organization=... -Dsonar.qualitygate.wait=true -Dsonar.qualitygate.timeout=300 -Dsonar.javascript.lcov.reportPaths=**/lcov.info -Dsonar.tests.inclusions=**/*.spec.ts,**/*.e2e-spec.ts -Dsonar.exclusions=**/__tests__/**,**/__mocks__/**,**/coverage/**,**/node_modules/**,**/dist/**,**/*.spec.ts,**/*.e2e-spec.ts,deploy/**,docs/**,**/*.jpg,**/*.jpeg,**/*.png -Dsonar.coverage.exclusions=**/__tests__/**,**/__mocks__/**,**/coverage/**,**/node_modules/**,**/dist/**,deploy/**,docs/**,**/*.html,**/*.css,**/*.scss,**/*.jpg,**/*.jpeg,**/*.png

2.2. Run the same command through the docker entrypoint:

/usr/bin/entrypoint.sh sonar-scanner -X -Dsonar.projectKey=px-ms-shortener -Dsonar.token=... -Dsonar.organization=... -Dsonar.qualitygate.wait=true -Dsonar.qualitygate.timeout=300 -Dsonar.javascript.lcov.reportPaths=**/lcov.info -Dsonar.tests.inclusions=**/*.spec.ts,**/*.e2e-spec.ts -Dsonar.exclusions=**/__tests__/**,**/__mocks__/**,**/coverage/**,**/node_modules/**,**/dist/**,**/*.spec.ts,**/*.e2e-spec.ts,deploy/**,docs/**,**/*.jpg,**/*.jpeg,**/*.png -Dsonar.coverage.exclusions=**/__tests__/**,**/__mocks__/**,**/coverage/**,**/node_modules/**,**/dist/**,deploy/**,docs/**,**/*.html,**/*.css,**/*.scss,**/*.jpg,**/*.jpeg,**/*.png

And the error I am getting:

13:58:54.246 INFO: Scanner configuration file: /opt/sonar-scanner/conf/sonar-scanner.properties
13:58:54.262 INFO: Project root configuration file: NONE
13:58:54.291 INFO: SonarScanner 5.0.1.3006
13:58:54.292 INFO: Java 17.0.11 Alpine (64-bit)
13:58:54.292 INFO: Linux 5.10.217-205.860.amzn2.x86_64 amd64
13:58:54.446 DEBUG: keyStore is : 
13:58:54.446 DEBUG: keyStore type is : pkcs12
13:58:54.447 DEBUG: keyStore provider is : 
13:58:54.448 DEBUG: init keystore
13:58:54.448 DEBUG: init keymanager of type SunX509
13:58:54.564 DEBUG: Create: /opt/sonar-scanner/.sonar/cache
13:58:54.566 INFO: User cache: /opt/sonar-scanner/.sonar/cache
13:58:54.566 DEBUG: Create: /opt/sonar-scanner/.sonar/cache/_tmp
13:58:54.570 DEBUG: Extract sonar-scanner-api-batch in temp...
13:58:54.573 DEBUG: Get bootstrap index...
13:58:54.574 DEBUG: Download: https://sonarcloud.io/batch/index
13:58:55.284 DEBUG: Get bootstrap completed
13:58:55.288 DEBUG: Create isolated classloader...
13:58:55.297 DEBUG: Start temp cleaning...
13:58:55.301 DEBUG: Temp cleaning done
13:58:55.301 DEBUG: Execution getVersion
13:58:55.331 INFO: Analyzing on SonarQube server 8.0.0.55018
13:58:55.332 INFO: Default locale: "en_US", source code encoding: "UTF-8" (analysis is platform dependent)
13:58:55.332 DEBUG: Work directory: /tmp/.scannerwork
13:58:55.334 DEBUG: Execution execute
13:58:55.558 DEBUG: JVM max available memory: 193 MB
13:58:55.589 DEBUG: SonarCloud 10.6.0.2114
13:58:55.726 DEBUG: Sonar User Home: /opt/sonar-scanner/.sonar
13:58:55.726 DEBUG: Initialize DefaultScannerWsClient
13:58:56.068 DEBUG: Loaded [441] system trusted certificates
13:58:56.178 INFO: Load global settings
13:58:56.193 DEBUG: --> GET https://sonarcloud.io/api/settings/values.protobuf
13:58:56.795 DEBUG: <-- 200 https://sonarcloud.io/api/settings/values.protobuf (601ms, unknown-length body)
13:58:56.805 INFO: Load global settings (done) | time=627ms
13:58:56.810 INFO: Server id: 1BD809FA-AWHW8ct9-T_TB3XqouNu
13:58:56.813 DEBUG: Initialize DownloadPluginsScannerWsClient
13:58:57.037 DEBUG: Loaded [441] system trusted certificates
13:58:57.043 DEBUG: User cache: /opt/sonar-scanner/.sonar/cache
13:58:57.055 INFO: Loading required plugins
13:58:57.055 INFO: Load plugins index
13:58:57.057 DEBUG: --> GET https://sonarcloud.io/api/plugins/installed
13:58:57.531 DEBUG: <-- 200 https://sonarcloud.io/api/plugins/installed (474ms, unknown-length body)
13:58:57.579 INFO: Load plugins index (done) | time=524ms
13:58:57.580 INFO: Load/download plugins
13:58:57.633 INFO: Load/download plugins (done) | time=53ms
13:58:57.636 DEBUG: Plugins not loaded because they are optional: [abap, sonarapex, csharp, cpp, cobol, dbd, dbdjavafrontend, dbdpythonfrontend, flex, go, web, java, javascript, kotlin, php, pli, plsql, rpg, ruby, sonarscala, swift, tsql, vbnet, vb, security, securitycsharpfrontend, securityjsfrontend, securityjavafrontend, securityphpfrontend, securitypythonfrontend]
13:58:57.654 DEBUG: Plugins loaded:
13:58:57.654 DEBUG:   * License for SonarLint 8.0.0.55018 (license)
13:58:57.654 DEBUG:   * Python Code Quality and Security 4.18.0.15334 (python)
13:58:57.654 DEBUG:   * XML Code Quality and Security 2.10.0.4108 (xml)
13:58:57.654 DEBUG:   * JaCoCo 1.3.0.1538 (jacoco)
13:58:57.654 DEBUG:   * IaC Code Quality and Security 1.31.0.10579 (iac)
13:58:57.654 DEBUG:   * Text Code Quality and Security 2.10.0.2188 (text)
13:58:57.655 DEBUG: --> GET https://sonarcloud.io/api/server/version
13:58:58.126 DEBUG: <-- 200 https://sonarcloud.io/api/server/version (470ms, 11-byte body)
13:58:58.128 DEBUG: Updated analysis started with a difference of -84 milliseconds
13:58:58.138 DEBUG: Started at Mon Jun 10 13:58:56 GMT 2024
13:58:58.315 DEBUG: register org.eclipse.jgit.util.FS$FileStoreAttributes$$Lambda$281/0x00007f33e92f23f8@24361cfc with shutdown hook
13:58:58.347 INFO: Found an active CI vendor: 'Gitlab CI'
13:58:58.358 INFO: Load project settings for component key: 'px-ms-shortener'
13:58:58.359 DEBUG: --> GET https://sonarcloud.io/api/settings/values.protobuf?component=px-ms-shortener
13:58:58.527 DEBUG: <-- 404 https://sonarcloud.io/api/settings/values.protobuf?component=px-ms-shortener (167ms, unknown-length body)
13:58:58.532 INFO: Process project properties
13:58:58.533 DEBUG: Process project properties (done) | time=1ms
13:58:58.539 INFO: Project key: px-ms-shortener
13:58:58.539 INFO: Base dir: /builds/project
13:58:58.539 INFO: Working dir: /tmp/.scannerwork
13:58:58.540 DEBUG: Project global encoding: UTF-8, default locale: en_US
13:58:58.542 DEBUG: Creating module hierarchy
13:58:58.542 DEBUG:   Init module 'px-ms-shortener'
13:58:58.542 DEBUG:     Base dir: /builds/project
13:58:58.543 DEBUG:     Working dir: /tmp/.scannerwork
13:58:58.543 DEBUG:     Module global encoding: UTF-8, default locale: en_US
13:58:58.549 INFO: Load project branches
13:58:58.550 DEBUG: --> GET https://sonarcloud.io/api/project_branches/list?project=px-ms-shortener
13:58:59.021 DEBUG: <-- 404 https://sonarcloud.io/api/project_branches/list?project=px-ms-shortener (470ms, unknown-length body)
13:58:59.023 DEBUG: Could not process project branches - continuing without it
13:58:59.024 INFO: Load project branches (done) | time=475ms
13:58:59.027 INFO: Check ALM binding of project 'px-ms-shortener'
13:58:59.028 DEBUG: --> GET https://sonarcloud.io/api/alm_integration/is_project_bound?project=px-ms-shortener
13:58:59.195 DEBUG: <-- 404 https://sonarcloud.io/api/alm_integration/is_project_bound?project=px-ms-shortener (167ms, unknown-length body)
13:58:59.196 INFO: Detected project binding: NONEXISTENT
13:58:59.197 INFO: Check ALM binding of project 'px-ms-shortener' (done) | time=171ms
13:58:59.198 INFO: Load project pull requests
13:58:59.200 DEBUG: --> GET https://sonarcloud.io/api/project_pull_requests/list?project=px-ms-shortener
13:58:59.670 DEBUG: <-- 404 https://sonarcloud.io/api/project_pull_requests/list?project=px-ms-shortener (469ms, unknown-length body)
13:58:59.670 DEBUG: Could not process project pull requests - continuing without it
13:58:59.672 INFO: Load project pull requests (done) | time=474ms
13:58:59.675 INFO: Load branch configuration
13:58:59.677 INFO: Auto-configuring branch staging
13:58:59.703 DEBUG: Cleanup org.eclipse.jgit.util.FS$FileStoreAttributes$$Lambda$281/0x00007f33e92f23f8@24361cfc during JVM shutdown
13:58:59.704 INFO: ------------------------------------------------------------------------
13:58:59.704 INFO: EXECUTION FAILURE
13:58:59.704 INFO: ------------------------------------------------------------------------
13:58:59.705 INFO: Total time: 5.470s
13:58:59.733 INFO: Final Memory: 9M/31M
13:58:59.733 INFO: ------------------------------------------------------------------------
13:58:59.734 ERROR: Error during SonarScanner execution
java.lang.IllegalStateException: Unable to load component class org.sonar.scanner.scan.filesystem.InputComponentStore
        at org.sonar.core.platform.ComponentContainer$ExtendedDefaultPicoContainer.getComponent(ComponentContainer.java:52)
        at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:632)
        at org.picocontainer.parameters.BasicComponentParameter$1.resolveInstance(BasicComponentParameter.java:118)
        at org.picocontainer.parameters.ComponentParameter$1.resolveInstance(ComponentParameter.java:136)
        at org.picocontainer.injectors.SingleMemberInjector.getParameter(SingleMemberInjector.java:78)
        at org.picocontainer.injectors.ConstructorInjector$CtorAndAdapters.getParameterArguments(ConstructorInjector.java:309)
        at org.picocontainer.injectors.ConstructorInjector$1.run(ConstructorInjector.java:335)
        at org.picocontainer.injectors.AbstractInjector$ThreadLocalCyclicDependencyGuard.observe(AbstractInjector.java:270)
        at org.picocontainer.injectors.ConstructorInjector.getComponentInstance(ConstructorInjector.java:364)
        at org.picocontainer.injectors.AbstractInjectionFactory$LifecycleAdapter.getComponentInstance(AbstractInjectionFactory.java:56)
        at org.picocontainer.behaviors.AbstractBehavior.getComponentInstance(AbstractBehavior.java:64)
        at org.picocontainer.behaviors.Stored.getComponentInstance(Stored.java:91)
        at org.picocontainer.DefaultPicoContainer.instantiateComponentAsIsStartable(DefaultPicoContainer.java:1034)
        at org.picocontainer.DefaultPicoContainer.addAdapterIfStartable(DefaultPicoContainer.java:1026)
        at org.picocontainer.DefaultPicoContainer.startAdapters(DefaultPicoContainer.java:1003)
        at org.picocontainer.DefaultPicoContainer.start(DefaultPicoContainer.java:767)
        at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:122)
        at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:109)
        at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(GlobalContainer.java:131)
        at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:123)
        at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:109)
        at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:60)
        at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:54)
        at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
        at jdk.proxy1/jdk.proxy1.$Proxy0.execute(Unknown Source)
        at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:189)
        at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:138)
        at org.sonarsource.scanner.cli.Main.execute(Main.java:126)
        at org.sonarsource.scanner.cli.Main.execute(Main.java:81)
        at org.sonarsource.scanner.cli.Main.main(Main.java:62)
Caused by: java.lang.IllegalStateException: Unable to load component interface org.sonar.scanner.scan.branch.BranchConfiguration
        at org.sonar.core.platform.ComponentContainer$ExtendedDefaultPicoContainer.getComponent(ComponentContainer.java:52)
        at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:632)
        at org.picocontainer.parameters.BasicComponentParameter$1.resolveInstance(BasicComponentParameter.java:118)
        at org.picocontainer.parameters.ComponentParameter$1.resolveInstance(ComponentParameter.java:136)
        at org.picocontainer.injectors.SingleMemberInjector.getParameter(SingleMemberInjector.java:78)
        at org.picocontainer.injectors.ConstructorInjector$CtorAndAdapters.getParameterArguments(ConstructorInjector.java:309)
        at org.picocontainer.injectors.ConstructorInjector$1.run(ConstructorInjector.java:335)
        at org.picocontainer.injectors.AbstractInjector$ThreadLocalCyclicDependencyGuard.observe(AbstractInjector.java:270)
        at org.picocontainer.injectors.ConstructorInjector.getComponentInstance(ConstructorInjector.java:364)
        at org.picocontainer.injectors.AbstractInjectionFactory$LifecycleAdapter.getComponentInstance(AbstractInjectionFactory.java:56)
        at org.picocontainer.behaviors.AbstractBehavior.getComponentInstance(AbstractBehavior.java:64)
        at org.picocontainer.behaviors.Stored.getComponentInstance(Stored.java:91)
        at org.picocontainer.DefaultPicoContainer.getInstance(DefaultPicoContainer.java:699)
        at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:647)
        at org.sonar.core.platform.ComponentContainer$ExtendedDefaultPicoContainer.getComponent(ComponentContainer.java:50)
        ... 34 more
Caused by: Could not find a default branch for project with key 'px-ms-shortener'. Make sure project exists.
13:58:59.744 DEBUG: Cleanup org.eclipse.jgit.util.FS$FileStoreAttributes$$Lambda$281/0x00007f33e92f23f8@24361cfc during JVM shutdown
  1. Notes:
    3.1. Token is the same with permission to create new projects.
    3.2. By default CI puts codebase and performa analysis from another folder. I was trying to do that manually from /usr/src/ - same effect.
    3.3. I was trying to add bracnc config -Dsonar.branch.name=staging or -Dsonar.branch.name=master - same error.
    3.4. I was trying to clone git repo manually and perform analysis on it - no luck.

So it works locally and doesn’t work on CI. Please help me to find out the way to run it on CI, so it will automatically crate project in SonrCloud and perform the analysis.

Hey all.

I’m pretty sure this is happening when sonar.branch.name is set (explicitly or via auto-configuration) and the project does not exist yet.

This would make sense why it works locally (and sonar.branch.name is not set), but not in a CI environment (where it’s auto-detected and set via environment variables).

We have an internal ticket to solve for this (provision project when branch configuration is provided), a feature already present in SonarQube. Unfortunately, I’m not aware of any workaround here. Until that’s implemented, you’ll need to provision your projects before the first analysis.

1 Like

Hey Colin,
Thanks for you support, I appreciate that.
I would ask about the SonarQube API, I found that I can create a new project using this API but not in Community Edition. So using SonarCloud, which edition is considered? Because I tried to use this API method ,but I got that the current edition doesn’t support it.

Hey @Fakih_MhamadBy

I’m a little confused about where Community Edition comes into this conversation. What specific API did you use?

POST api/projects/create exists in SonarCloud (and POST api/project_branches/rename, for that matter, in case you need it)

1 Like

Hey Colin
I am sorry for the confusion, I guess that the API in python has two edition Community and Pro. I tried to create a python script that automate the creation process of the project before analysis.

So first i installed python-sonarqube-api:
pip install --upgrade python-sonarqube-api

Then my python script:
from sonarqube import SonarQubeClient
sonar = SonarQubeClient(sonarqube_url=“http://SonarCloud.io”, token=‘***************’)
sonar.projects.create_project(project=“my_project”, name=“my project”, visibility=“private”)

So I got :
sonar.projects.create_project(project=“my_project”, name=“my project”, visibility=“private”)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: ‘SonarQubeProjects’ object has no attribute ‘create_project’. Did you mean: ‘get_project’?

Ah, indeed, this is all something completely different (Welcome to SonarQube Client with Python’s documentation! — SonarQube Client with Python 2.0.0 documentation)

This is just a way that I tried to figure out a solution for my error.
I already read this documentation, and find the API should be of Pro Edition to allow me to use the create_project method.
In addition, I can’t find any documentation about how to install the Pro Edition API other than this command:
pip install python_sonarqube_pro_api-x.y.z-py3-none-any.whl

This is a third-party python library not supported in this Community. I really recommend reaching out to the maintainer on their Github repo. GitHub - shijl0925/python-sonarqube-api: Python Client library for interacting with Community, Developer, and Enterprise Editions SonarQube's REST APIs and SonarCloud's REST APIs.

1 Like

Hello Colin
Thanks for you support.
I built a solution using the SonarQube API within Powershell to create a project before analysis and it run successfully.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.