CSharp - Compulsory to use "dotnet build"?

Looking at the documentation for SonarScanner for .NET, it says to use dotnet to build your .sln or .csproj - My question is does Sonarqube support using dotnet restore and dotnet publish instead of dotnet build?

This question is targeted towards .NET 5+

Hi,

In fact, the docs advise /t:Rebuild, to make sure that every file is compiled. Because if it’s not built between the begin and end steps, it won’t be analyzed. If publish does that for you, then you should be good. But my understanding is that publish does more than just build. So our docs just give you the minimum required action.

 
HTH,
Ann

I believe I cannot use /t:Rebuild as it is targeted towards .exe as far as I am aware, we are using .dll. Also the reason why I asked such question is the fact that I get the following error when running both dotnet restore or dotnet publish:

Possible causes:
1. The project has not been built - the project must be built in between the begin and end steps
2023-07-31 15:48:08,870	[INFO]	14:48:08.87  Generation of the sonar-properties file failed. Unable to complete the analysis.
2. An unsupported version of MSBuild has been used to build the project. Currently MSBuild 14.0.25420.1 and higher are supported.
3. The begin, build and end steps have not all been launched from the same folder
4. None of the analyzed projects have a valid ProjectGuid and you have not used a solution (.sln)

Hi,

Please share the Scanner for .NET verbose logs

  • Add /d:"sonar.verbose=true" to the…
    • SonarScanner.MSBuild.exe or dotnet sonarscanner begin command to get more detailed logs
      • For example: SonarScanner.MSBuild.exe begin /k:"MyProject" /d:"sonar.verbose=true"
    • “SonarQubePrepare” or “SonarCloudPrepare” task’s extraProperties argument if you are using Azure DevOps
      • For example:
        - task: SonarCloudPrepare@1
            inputs:
              SonarCloud: 'sonarcloud'
              organization: 'foo'
              scannerMode: 'MSBuild'
              projectKey: 'foo_sonar-scanning-someconsoleapp'
              projectName: 'sonar-scanning-someconsoleapp'
              extraProperties: |
                sonar.verbose=true
        
  • The important logs are in the END step (i.e. SonarQubeAnalyze / SonarCloudAnalyze / “Run Code Analysis”)

Share the msbuild detailed logs

MsBuild.exe /t:Rebuild /v:d

or

dotnet build -v:d

 
Ann

the logs for the begin command is:

|2023-08-02 10:15:14,108|[INFO]|SonarScanner for MSBuild 5.12|
|---|---|---|
|2023-08-02 10:15:14,108|[INFO]|Using the .NET Core version of the Scanner for MSBuild|
|2023-08-02 10:15:14,174|[INFO]|Default properties file was found at /path/to/scanner/SonarQube.Analysis.xml|
|2023-08-02 10:15:14,174|[INFO]|Loading analysis properties from /path/to/scanner/SonarQube.Analysis.xml|
|2023-08-02 10:15:14,174|[INFO]|sonar.verbose=true was specified - setting the log verbosity to 'Debug'|
|2023-08-02 10:15:14,174|[INFO]|Pre-processing started.|
|2023-08-02 10:15:14,174|[INFO]|Preparing working directories...|
|2023-08-02 10:15:14,176|[INFO]|Using environment variables to determine the download directory...|
|2023-08-02 10:15:14,188|[INFO]|09:15:14.188  09:15:14.182  Loading analysis properties from /path/to/scanner/SonarQube.Analysis.xml|
|2023-08-02 10:15:14,188|[INFO]|09:15:14.188  09:15:14.188  sonar.verbose=true was specified - setting the log verbosity to 'Debug'|
|2023-08-02 10:15:14,189|[INFO]|09:15:14.189  Updating build integration targets...|
|2023-08-02 10:15:14,192|[INFO]|09:15:14.192  The file SonarQube.Integration.ImportBefore.targets is up to date at /path/to/workspace/.local/share/Microsoft/MSBuild/4.0/Microsoft.Common.targets/ImportBefore|
|2023-08-02 10:15:14,192|[INFO]|09:15:14.192  The file SonarQube.Integration.ImportBefore.targets is up to date at /path/to/workspace/local/share/Microsoft/MSBuild/10.0/Microsoft.Common.targets/ImportBefore|
|2023-08-02 10:15:14,192|[INFO]|09:15:14.192  The file SonarQube.Integration.ImportBefore.targets is up to date at /path/to/workspace/.local/share/Microsoft/MSBuild/11.0/Microsoft.Common.targets/ImportBefore|
|2023-08-02 10:15:14,192|[INFO]|09:15:14.192  The file SonarQube.Integration.ImportBefore.targets is up to date at /path/to/workspace/.local/share/Microsoft/MSBuild/12.0/Microsoft.Common.targets/ImportBefore|
|2023-08-02 10:15:14,192|[INFO]|09:15:14.192  The file SonarQube.Integration.ImportBefore.targets is up to date at /path/to/workspace/.local/share/Microsoft/MSBuild/14.0/Microsoft.Common.targets/ImportBefore|
|2023-08-02 10:15:14,192|[INFO]|09:15:14.192  The file SonarQube.Integration.ImportBefore.targets is up to date at /path/to/workspace/.local/share/Microsoft/MSBuild/15.0/Microsoft.Common.targets/ImportBefore|
|2023-08-02 10:15:14,192|[INFO]|09:15:14.192  The file SonarQube.Integration.ImportBefore.targets is up to date at /opt/perforce/workspace/MDMProxies/MDMProxies_Interim_release_docker/.local/share/Microsoft/MSBuild/Current/Microsoft.Common.targets/ImportBefore|
|2023-08-02 10:15:14,193|[INFO]|09:15:14.192  The file SonarQube.Integration.ImportBefore.targets is up to date at /path/to/workspace/Microsoft/MSBuild/15.0/Microsoft.Common.targets/ImportBefore|
|2023-08-02 10:15:14,193|[INFO]|09:15:14.192  The file SonarQube.Integration.ImportBefore.targets is up to date at /path/to/workspace/Microsoft/MSBuild/Current/Microsoft.Common.targets/ImportBefore|
|2023-08-02 10:15:14,193|[INFO]|09:15:14.193  Installed SonarQube.Integration.targets to //path/to/workspace/.sonarqube/bin/targets|
|2023-08-02 10:15:14,195|[INFO]|09:15:14.193  Creating config and output folders...|
|2023-08-02 10:15:14,195|[INFO]|09:15:14.194  Creating directory: /path/to/workspace/.sonarqube/conf|
|2023-08-02 10:15:14,195|[INFO]|09:15:14.194  Creating directory: /path/to/workspace/.sonarqube/out|
|2023-08-02 10:15:14,226|[INFO]|09:15:14.225  Downloading from https://url_to_sonarqube_server/api/server/version...|
|2023-08-02 10:15:14,357|[INFO]|09:15:14.356  Checking validity of server license|
|2023-08-02 10:15:14,357|[INFO]|09:15:14.357  Downloading from https://url_to_sonarqube_server/api/editions/is_valid_license...|
|2023-08-02 10:15:14,401|[INFO]|09:15:14.401  Fetching analysis configuration settings...|
|2023-08-02 10:15:14,403|[INFO]|09:15:14.402  Fetching properties for project 'Name_of_the_project' from https://url_to_sonarqube_server/api/settings/values?component=Name_of_the_project...|
|2023-08-02 10:15:14,404|[INFO]|09:15:14.404  Downloading from https://url_to_sonarqube_server/api/settings/values?component=Name_of_the_project...|
|2023-08-02 10:15:14,414|[INFO]|09:15:14.413  No settings for project Name_of_the_project. Getting global settings: https://url_to_sonarqube_serverapi/settings/values|
|2023-08-02 10:15:14,414|[INFO]|09:15:14.414  Downloading from https://url_to_sonarqube_server/api/settings/values...|
|2023-08-02 10:15:14,435|[INFO]|09:15:14.435  Downloading from https://url_to_sonarqube_serverapi/languages/list...|
|2023-08-02 10:15:14,442|[INFO]|09:15:14.441  Fetching quality profile for project 'Name_of_the_project' from https://url_to_sonarqube_server/api/qualityprofiles/search?project=Name_of_the_project...|
|2023-08-02 10:15:14,442|[INFO]|09:15:14.442  Downloading from https://url_to_sonarqube_server/api/qualityprofiles/search?project=Name_of_the_project...|
|2023-08-02 10:15:14,447|[INFO]|09:15:14.447  Fetching quality profile for project 'Name_of_the_project' from https://url_to_sonarqube_server/api/qualityprofiles/search?defaults=true...|
|2023-08-02 10:15:14,447|[INFO]|09:15:14.447  Downloading from https://url_to_sonarqube_server/api/qualityprofiles/search?defaults=true...|
|2023-08-02 10:15:14,478|[INFO]|09:15:14.478  Fetching rules for quality profile 'AYOdCxhhZqUr8mlslu_h' from https://url_to_sonarqube_server/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey,params,actives&ps=500&qprofile=AYOdCxhhZqUr8mlslu_h&p=1...|
|2023-08-02 10:15:14,479|[INFO]|09:15:14.478  Downloading from https://url_to_sonarqube_server/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey,params,actives&ps=500&qprofile=AYOdCxhhZqUr8mlslu_h&p=1...|
|2023-08-02 10:15:14,588|[INFO]|09:15:14.588  Fetching rules for quality profile 'AYOdCxhhZqUr8mlslu_h' from https://url_to_sonarqube_server/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey,params,actives&ps=500&qprofile=AYOdCxhhZqUr8mlslu_h&p=2...|
|2023-08-02 10:15:14,588|[INFO]|09:15:14.588  Downloading from https://url_to_sonarqube_server/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey,params,actives&ps=500&qprofile=AYOdCxhhZqUr8mlslu_h&p=2...|
|2023-08-02 10:15:14,646|[INFO]|09:15:14.645  Fetching rules for quality profile 'AYOdCxhhZqUr8mlslu_h' from https://url_to_sonarqube_server/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey,params,actives&ps=500&qprofile=AYOdCxhhZqUr8mlslu_h&p=3...|
|2023-08-02 10:15:14,646|[INFO]|09:15:14.645  Downloading from https://url_to_sonarqube_server/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey,params,actives&ps=500&qprofile=AYOdCxhhZqUr8mlslu_h&p=3...|
|2023-08-02 10:15:14,688|[INFO]|09:15:14.688  Local analyzer cache: /tmp/.sonarqube/resources|
|2023-08-02 10:15:14,692|[INFO]|09:15:14.692  Writing Roslyn generated ruleset to /path/to/workspace/.sonarqube/conf/Sonar-cs.ruleset...|
|2023-08-02 10:15:14,705|[INFO]|09:15:14.704  Writing Roslyn generated ruleset to /path/to/workspace/.sonarqube/conf/Sonar-cs-none.ruleset...|
|2023-08-02 10:15:14,706|[INFO]|09:15:14.706  Provisioning analyzer assemblies for cs...|
|2023-08-02 10:15:14,707|[INFO]|09:15:14.707  Installing required Roslyn analyzers...|
|2023-08-02 10:15:14,707|[INFO]|09:15:14.707  Processing plugin: csharp version 8.51.0.59060|
|2023-08-02 10:15:14,758|[INFO]|09:15:14.758  Cache miss: plugin files were not found in the local cache|
|2023-08-02 10:15:14,758|[INFO]|09:15:14.758  Fetching resource for plugin: csharp, version 8.51.0.59060. Resource: SonarAnalyzer-8.51.0.59060.zip|
|2023-08-02 10:15:14,760|[INFO]|09:15:14.76  Downloading SonarAnalyzer-8.51.0.59060.zip from https://url_to_sonarqube_server/static/csharp/SonarAnalyzer-8.51.0.59060.zip to /tmp/.sonarqube/resources/0|
|2023-08-02 10:15:14,761|[INFO]|09:15:14.76  Downloading file from https://url_to_sonarqube_server/static/csharp/SonarAnalyzer-8.51.0.59060.zip to /tmp/.sonarqube/resources/0/SonarAnalyzer-8.51.0.59060.zip...|
|2023-08-02 10:15:14,774|[INFO]|09:15:14.774  Extracting files to /tmp/.sonarqube/resources/0...|
|2023-08-02 10:15:14,811|[INFO]|09:15:14.81  Processing plugin: vbnet version 8.51.0.59060|
|2023-08-02 10:15:14,823|[INFO]|09:15:14.822  Cache miss: plugin files were not found in the local cache|
|2023-08-02 10:15:14,823|[INFO]|09:15:14.822  Fetching resource for plugin: vbnet, version 8.51.0.59060. Resource: SonarAnalyzer-8.51.0.59060.zip|
|2023-08-02 10:15:14,823|[INFO]|09:15:14.823  Downloading SonarAnalyzer-8.51.0.59060.zip from https://url_to_sonarqube_server/static/vbnet/SonarAnalyzer-8.51.0.59060.zip to /tmp/.sonarqube/resources/1|
|2023-08-02 10:15:14,823|[INFO]|09:15:14.823  Downloading file from https://url_to_sonarqube_server/static/vbnet/SonarAnalyzer-8.51.0.59060.zip to /tmp/.sonarqube/resources/1/SonarAnalyzer-8.51.0.59060.zip...|
|2023-08-02 10:15:14,829|[INFO]|09:15:14.828  Extracting files to /tmp/.sonarqube/resources/1...|
|2023-08-02 10:15:14,837|[INFO]|09:15:14.836  Processing plugin: securitycsharpfrontend version 9.9.0.19083|
|2023-08-02 10:15:14,837|[INFO]|09:15:14.837  Cache miss: plugin files were not found in the local cache|
|2023-08-02 10:15:14,837|[INFO]|09:15:14.837  Fetching resource for plugin: securitycsharpfrontend, version 9.9.0.19083. Resource: SonarAnalyzer.Security-9.9.0.19083.zip|
|2023-08-02 10:15:14,837|[INFO]|09:15:14.837  Downloading SonarAnalyzer.Security-9.9.0.19083.zip from https://url_to_sonarqube_server/static/securitycsharpfrontend/SonarAnalyzer.Security-9.9.0.19083.zip to /tmp/.sonarqube/resources/2|
|2023-08-02 10:15:14,837|[INFO]|09:15:14.837  Downloading file from https://url_to_sonarqube_server/static/securitycsharpfrontend/SonarAnalyzer.Security-9.9.0.19083.zip to /tmp/.sonarqube/resources/2/SonarAnalyzer.Security-9.9.0.19083.zip...|
|2023-08-02 10:15:14,842|[INFO]|09:15:14.842  Extracting files to /tmp/.sonarqube/resources/2...|
|2023-08-02 10:15:14,854|[INFO]|09:15:14.854  Writing Roslyn analyzer additional file to /path/to/workspace/.sonarqube/conf/cs/SonarLint.xml...|
|2023-08-02 10:15:14,855|[INFO]|09:15:14.855  Fetching quality profile for project 'Name_of_the_project' from https://url_to_sonarqube_server/api/qualityprofiles/search?project=Name_of_the_project...|
|2023-08-02 10:15:14,855|[INFO]|09:15:14.855  Downloading from https://url_to_sonarqube_server/api/qualityprofiles/search?project=Name_of_the_project...|
|2023-08-02 10:15:14,859|[INFO]|09:15:14.859  Fetching quality profile for project 'Name_of_the_project' from https://url_to_sonarqube_server/api/qualityprofiles/search?defaults=true...|
|2023-08-02 10:15:14,859|[INFO]|09:15:14.859  Downloading from https://url_to_sonarqube_server/api/qualityprofiles/search?defaults=true...|
|2023-08-02 10:15:14,880|[INFO]|09:15:14.88  Fetching rules for quality profile 'AYOdCxVjZqUr8mlsluJO' from https://url_to_sonarqube_server/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey,params,actives&ps=500&qprofile=AYOdCxVjZqUr8mlsluJO&p=1...|
|2023-08-02 10:15:14,881|[INFO]|09:15:14.88  Downloading from https://url_to_sonarqube_server/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey,params,actives&ps=500&qprofile=AYOdCxVjZqUr8mlsluJO&p=1...|
|2023-08-02 10:15:14,963|[INFO]|09:15:14.963  Fetching rules for quality profile 'AYOdCxVjZqUr8mlsluJO' from https://url_to_sonarqube_server/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey,params,actives&ps=500&qprofile=AYOdCxVjZqUr8mlsluJO&p=2...|
|2023-08-02 10:15:14,964|[INFO]|09:15:14.963  Downloading from https://url_to_sonarqube_server/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey,params,actives&ps=500&qprofile=AYOdCxVjZqUr8mlsluJO&p=2...|
|2023-08-02 10:15:14,988|[INFO]|09:15:14.988  Local analyzer cache: /tmp/.sonarqube/resources|
|2023-08-02 10:15:14,988|[INFO]|09:15:14.988  Writing Roslyn generated ruleset to /path/to/workspace/.sonarqube/conf/Sonar-vbnet.ruleset...|
|2023-08-02 10:15:14,989|[INFO]|09:15:14.989  Writing Roslyn generated ruleset to /path/to/workspace/.sonarqube/conf/Sonar-vbnet-none.ruleset...|
|2023-08-02 10:15:14,990|[INFO]|09:15:14.989  Provisioning analyzer assemblies for vbnet...|
|2023-08-02 10:15:14,990|[INFO]|09:15:14.99  Installing required Roslyn analyzers...|
|2023-08-02 10:15:14,990|[INFO]|09:15:14.99  Processing plugin: csharp version 8.51.0.59060|
|2023-08-02 10:15:14,990|[INFO]|09:15:14.99  Cache hit: using plugin files from /tmp/.sonarqube/resources/0|
|2023-08-02 10:15:14,990|[INFO]|09:15:14.99  Processing plugin: vbnet version 8.51.0.59060|
|2023-08-02 10:15:14,991|[INFO]|09:15:14.991  Cache hit: using plugin files from /tmp/.sonarqube/resources/1|
|2023-08-02 10:15:14,991|[INFO]|09:15:14.991  Writing Roslyn analyzer additional file to /path/to/workspace/.sonarqube/conf/vbnet/SonarLint.xml...|
|2023-08-02 10:15:14,999|[INFO]|09:15:14.995  Processing analysis cache|
|2023-08-02 10:15:15,001|[INFO]|09:15:15.001  Incremental PR analysis: Base branch parameter was not provided.|
|2023-08-02 10:15:15,001|[INFO]|09:15:15.001  Cache data is empty. A full analysis will be performed.|
|2023-08-02 10:15:15,034|[INFO]|09:15:15.034  Pre-processing succeeded.|

As for the end command, I get an error which is:

|2023-08-02 09:45:43,597|[INFO]|SonarScanner for MSBuild 5.12|
|---|---|---|
|2023-08-02 09:45:43,597|[INFO]|Using the .NET Core version of the Scanner for MSBuild|
|2023-08-02 09:45:43,656|[INFO]|Default properties file was found at /path/to/msbuild/scanner/SonarQube.Analysis.xml|
|2023-08-02 09:45:43,656|[INFO]|Loading analysis properties from /path/to/msbuild/scanner/SonarQube.Analysis.xml|
|2023-08-02 09:45:43,656|[INFO]|sonar.verbose=true was specified - setting the log verbosity to 'Debug'|
|2023-08-02 09:45:43,656|[INFO]|Post-processing started.|
|2023-08-02 09:45:43,658|[INFO]|08:45:43.657  Using environment variables to determine the download directory...|
|2023-08-02 09:45:43,688|[INFO]|08:45:43.687  08:45:43.687  This setting is not valid in the end phase in this version of the C# plugin: sonar.verbose|
|2023-08-02 09:45:43,688|[INFO]|08:45:43.687  08:45:43.686  Uninstalling target: /path/to/workspace/.sonarqube/bin/targets/SonarQube.Integration.targets|
|2023-08-02 09:45:43,688|[INFO]|08:45:43.688  Post-processing failed. Exit code: 1|

regarding the msbuild detailed logs you mentioned, I cannot do it as I am not running dotnet build but instead dotnet publish. However I did try to add the flag --verbosity diagnostic to it but for some reason it failed and threw the error:
MSBUILD : error MSB1001: Unknown switch.

Hi,

Could you remove the setting and try again?

 
Ann

This is the error I get when removing it as you suggested:

Possible causes:
1. The project has not been built - the project must be built in between the begin and end steps
2023-07-31 15:48:08,870	[INFO]	14:48:08.87  Generation of the sonar-properties file failed. Unable to complete the analysis.
2. An unsupported version of MSBuild has been used to build the project. Currently MSBuild 14.0.25420.1 and higher are supported.
3. The begin, build and end steps have not all been launched from the same folder
4. None of the analyzed projects have a valid ProjectGuid and you have not used a solution (.sln)

-t:Rebuild works with dotnet build.

Try the following:

dotnet build -t:rebuild
dotnet publish --no-build

FYI if you want to turn on diagnostic level logging in dotnet build or dotnet publish add the argument -v:diag.

dotnet publish /path/to/csproj --packages /path/to/my/packages -o /path/to/output --no-restore --no-build

does the above command look right to you? Without --no-build it works but as soon as I add --no-build it throws the following error:
error : Manifest file at 'obj/Debug/net6.0/staticwebassets.build.json' not found. [/path/to/csproj/file]

According to the MS documentation --no-build implies --no-restore, so you don’t need both. Other than that it looks ok to me.

Are you calling dotnet build -t:rebuild for that .csproj first?

Try increasing the logging level to see what is being built during publishing, if anything.

Seems like the issue had something to do with the build server. I moved the existing code to another build server and that server had no issues building the project and then scanning it.

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