If the dotnet build not work, how can i analyze my c# code?

scanner
msbuild

(Sun) #1

When i use dotnet sonarscanner, sometimes i couldn’t build some project which is comiled well in Visual Studio.

In this case, “dotnet sonarscanner begin ~~~” works well/

Howerver, when i execute dotnet build.

It fails sometimes.

For example, the project uses VSIX or IIS Webserver package.

In this case, how can i analyze my c# code?


(Amaury Levé) #2

Hi @sun_lim,

I am a bit confused by your question. You flagged your question as SonarLint and yet you are talking about the scanner (dotnet sonarscanner). Do you need help with analyzing within Visual Studio using SonarLint or running an analysis you push to a SonarQube/SonarCloud instance using the scanner?

Cheers,
Amaury


(Sun) #3

Hello,

I just curious about dotnet sonarscanner which uses (dotnet begin ~~, dotnet build, dotnet end)

link [https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+MSBuild]

Now i’m using .Net Core global tool.

However sometime, i get a message like below

C:\Builds\1\MBS.Payments.Forms.Mvc\MBS Payment Forms MVC\Sources\MBS.Payments.Forms.Mvc\MBS.Forms.Payments.Web\MBS.Forms.Payments.Web.csproj (979): The imported project “C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets” was not found.

C# analyzer only analyze code when the solution complete.

However, sometimes dotnet sonarscanner failed from above error.

In this case, how can i build my solution and get the result.

Please give me some advice.

Thank you.


(Amaury Levé) #4

I will need a few more information to help you.

Could you let me know:

  • version of the scanner for msbuild?
  • version of SonarC#?
  • version of SonarQube?

Also could you share the log of some failure so that we can have a deeper look?


(Sun) #5

I just use

dotnet tool install --global dotnet-sonarscanner
dotnet sonarscanner begin /k: "project-key"
dotnet build
dotnet sonarscanner end

commands.
MSBUILD.exe is recently version. (15.8.169+g1ccb72aefa)

However, i got a message like :

C:\Builds\1\MBS.Payments.Forms.Mvc\MBS Payment Forms MVC\Sources\MBS.Payments.Forms.Mvc\MBS.Forms.Payments.Web\MBS.Forms.Payments.Web.csproj (979): The imported project “C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets” was not found.

How can i handle this problem?


(Sun) #6

I curious how to handle various C# project build environment in SonarQube.

It is difficult to handle every environment with diffrent libraries, different visual extension…


(Amaury Levé) #7

I am sorry but you are not giving me any information about the version, no logs nor any clear problem to work onto. I really don’t see how I can help for now.


(Sun) #8

Sorry. I just have a question.

When the project build fails, how can i analyze my C# code using sonar lint?

Can i analyze my code without build?


(Nicolas Bontoux) #9

The general answer to that is no. Independently of the language, independently of the tool, doing static code analysis on code that does not compile simply does not make sense.

The very first step of your pipeline should be to build the code, and if that fails then fixing the build should be the absolute priority. Other Continuous Integration phases (be it testing, code analysis, other sort of the inspection) strongly depend on the fact that the code is syntactically correct, and compiles.


(Sun) #10

So If the c# solution works well in local Visual Studio,

Sonarqube can handle every project without ‘build fail’?

In my situation,
the project is build well in Visual Studio, but not in dotnet command.

like
dotnet tool install --global dotnet-sonarscanner
dotnet sonarscanner begin /k: "project-key"
dotnet build
dotnet sonarscanner end
https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+MSBuild

How did you configure every build environment in sonarqube to build every project with different configuration?

Do I need more option in dotnet?


(Duncan Pocklington) #11

You don’t need to specify any other options you make the analysis work; you just need to have the correct pre-requisites installed on the build agent to run the Scanner for MSBuild, and they will be present if you have MSBuild 15 installed.

Does the project build correctly using dotnet build if you don’t run any of the sonarscanner steps?

The build error you reported above (“The imported project “C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets” was not found”) has nothing to do with SonarQube. Does that targets file exist on your machine?