Analyze c# code with sonarQube: MSBUILD version problems - c#

I need to analyze various C# files (not a project, just .cs files put under the same directory).
I tried the analysis in 2 different ways and I have errors in each of it. Could someone help solve any of those errors?
1- I tried the analysis like mentioned here
By executing C:\sonar-scanner\bin\sonar-scanner.bat in the root file, I have this error
ERROR: Error during SonarQube Scanner execution
java.lang.IllegalArgumentException: Custom and 3rd party Roslyn analyzers
are only by MSBuild 14. Either use MSBuild 14, or disable the custom/3rd
party Roslyn analyzers in your quality profile.
2- I tired creating a project and analyzing with SonarQube Scanner for MSBuild
But at the end, I have this error that appears
The SonarQube MSBuild integration failed: SonarQube was unable to collect
the required information about your projects.
Possible causes:
1. The project has not been built - the project must be built in between
the begin and end steps
2. An unsupported version of MSBuild has been used to build the project.
Currently MSBuild 14.0 upwards are supported
3. The begin, build or end steps have not all been launched from the
same folder
Generation of the sonar-properties file failed. Unable to complete
SonarQube analysis.
I have Visual Studio 2015,2012,2010,2008 installed. How can I verify that I am using the correct MSBUILD verison (14) because I checked C:\Program Files (x86)\MSBuild and I found the version 14 there.
Is there another issue to handle ?
At the end, I need to be able to do the analysis in both the first and seocnd way.
Thank you in advance!

2- I tired creating a project and analyzing with SonarQube Scanner for MSBuild
According to the document, we need execute following commands from the root folder of the project you want to analyze:
SonarQube.Scanner.MSBuild.exe begin /k:"org.sonarqube:sonarqube-scanner-msbuild" /n:"Project Name" /v:"1.0"
MSBuild.exe /t:Rebuild
SonarQube.Scanner.MSBuild.exe end
But the command MSBuild.exe is a external command, can not be recognized by Windows in an arbitrary directory, you should add the MSBuild path "C:\Program Files (x86)\MSBuild\14.0\Bin" in to your system environment to make the begin, build or end steps are all launched from the same folder.

Related

The SonarQube MSBuild integration failed - SonarQube 7 with Scanner 4.0.2.892 and VS2017

I'm using SonarQube 7.0 and SonarQube Scanner for MSBuild 4.0.2.892 and attempting to follow these instructions to scan my .NET application on Windows (ASP.NET Core). The three commands below are part of the instructions:
C:\SonarQube\Scanner\SonarQube.Scanner.MSBuild.exe begin /k:"org.sonarqube:sonarqube-scanner-msbuild" /n:"TradingPlatform" /v:"1.0"
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe" /t:Rebuild
C:\SonarQube\Scanner\SonarQube.Scanner.MSBuild.exe end
However, while pre-processing succeeds on the fist command, and the build completes on the second command, the third command always fails as shown below:
====
SonarQube Scanner for MSBuild 4.0.2
Default properties file was found at C:\SonarQube\Scanner\SonarQube.Analysis.xml
Loading analysis properties from C:\SonarQube\Scanner\SonarQube.Analysis.xml
Post-processing started.
The SonarQube MSBuild integration failed: SonarQube was unable to collect the required information about your projects.
Possible causes:
The project has not been built - the project must be built in between the begin and end steps
An unsupported version of MSBuild has been used to build the project. Currently MSBuild 14.0 upwards are supported
The begin, build or end steps have not all been launched from the same folder
Generation of the sonar-properties file failed. Unable to complete SonarQube analysis.
Exit Code 1.
====
Does anyone have any idea why it's failing? Cause 1 can't be the case as I'm building the project in step 2. Cause 2 seems very unlikely (but not impossible) as I'm using MSBuild 15. Cause 3 also can't be the case as I'm running all three commands from the same location
Other possible reasons are:
your build server is executing the builds using Local System account. Try changing the build user to a normal domain or local user.
you are building a .NET Core project that has no ProjectGuid property (the default) and that is not part of a solution. To resolve this you need to do one of these:
Add <ProjectGuid>XXXXX</ProjectGuid> element in the first PropertyGroup in your csproj, where XXXXX is a newly generated Guid.
Add your project to a Visual Studio solution and build the sln file instead of the csproj
If this does not help, add /d:sonar.verbose=true on the begin command of the scanner and save all logs into a file. Zip the logs and the .sonarqube folder from your build workspace and upload them here: https://www.dropbox.com/request/CszJAO1ZNXFVl1gwo5hA

Visual Studio 2015 builds my project without problems, MSBuild.exe cannot

I am developing simple Asp.net MVC 5 app which is working ok and visual studio 2015 can build it without any issues, but when i use MSBuild.exe command line tool it shows alot of errors which relates to typescript files compilation (primarily from node_modules folder).
I am trying to use Jenkins CI tool to automate my deployment routine, in order to do so i have to build my app using MSBuild.exe.
My project has tsconfing.json file in the root folder which contains following lines to exclude node_modules folder (it is inside #AngularComponents folder):
"exclude": [ "Scripts/**", "WrittenScripts/#AngularComponents/**" ]
But somehow MSBuild.exe command line tool just ignores it during build.
Does anyone have any ideas how to configure it to take into account my tsconfig? or tell MsBuild to not to build my node_modules folder?
Thank you in advance!
I finally figured it out.
First of all i have to use this MsBuild:
1) C:\Program Files (x86)\MSBuild\14.0\Bin\msbuild.exe;
Secondly, for some reason msbuild messed up my tools version 4.0 so I need to pass additional parameter to msbuild:
2) /tv:14.0
Thirdly, my typescript files have some /// references to node_modules\#types folder and for some reason it shows me any errors but not related to the reference one.
3) so i have to run npm install to recover #types packages.
After these steps msBuild builds successfully without any errors.
Thanks for all!

Microsoft.CSharp.Core.targets missing

I am building a webservice project on a TFS2017 Update 3 with build tools 2017. I get the following error
C:\Program Files (x86)\Microsoft Visual
Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.CSharp.CurrentVersion.targets(322,5):
Error MSB4019: The imported project "C:\Program Files (x86)\Microsoft
Visual
Studio\2017\Enterprise\MSBuild\15.0\Bin\Roslyn\Microsoft.CSharp.Core.targets"
was not found. Confirm that the path in the declaration is
correct, and that the file exists on disk.
What steps do I need to take to fix this error?
In my case, opening the existing Solution file (with .sln extension), it installed all the missing dependencies.
Otherwise check for missing target packages at nuget.org manually and install them (e.g. MSBuild.Microsoft.VisualStudio.Web.targets).
As per this GitHub issue, you'll need to set $(RoslynTargetsPath).
If MSBuild is installed on the machine and your tests run in a Visual Studio Developer Command Prompt, then you shouldn't need to do anything. However, MSBuild 15.0 no longer places itself in the registry which makes it impossible for MSBuild to find itself. This is because of a mandate by Visual Studio where you can have multiple side-by-side installations. MSBuild in this case would be installed once per Visual Studio instance. MSBuild now finds itself by looking at environment variables set by the VS command prompt.
If you want to fully redistribute MSBuild via a combination of our packages and all of the other packages needed to do full project evaluation, you will have to set RoslynTargetsPath before you load projects. You could argue this is an issue with NuGet since all packages' <contentFiles /> go to the the root of your output directory and you can't specify a subdirectory for certain packages.
If you're coming to this page from google/duckduckgo after trying to figure out why you're getting a similar message in Ubuntu 20.04, maybe this can help: https://askubuntu.com/a/1231973/1073658 (tl;dr apt install mono-roslyn from mono-project's repo)
First suggest you directly use msbuild command on the build agent with TFS build service account. This will narrow down if the issue is related to your agent environment or your TFS build definition.
According to your error info, the path C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Roslyn\Microsoft.CSharp.Core.targets is looking the path of Visual Studio 2017.
However, for Build Tools the path is different should be C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\Roslyn
Seems you haven't installed VS2017 on your build agent. This maybe the root cause of the issue.
The simplest solution is installing VS2017 on your build agent. Note, you need to reconfigure the build agent or restart build agent service to capture those new capabilities. Otherwise you need to change the corresponding import section in your .csproj project files.
I updated to TFS2018 and the problem got solved.

Does Sonarqube supports Xamarin for code quality?

I am working on a Xamarin project. I would like to check code quality with sonarqube. I see there is a support for .net/c# project. I have followed the instruction here for Xamarin.I was able to do the first two steps Begin and Rebuild. When i execute the third step "end"
SonarQube.Scanner.MSBuild.exe end
I am getting below error
mono /Users/apple/Downloads/sonar-scanner-msbuild-2.3.2.573/SonarQube.Scanner.MSBuild.exe end
SonarQube Scanner for MSBuild 2.3.2
Default properties file was found at /Users/apple/Downloads/sonar-scanner-msbuild-2.3.2.573/SonarQube.Analysis.xml
Loading analysis properties from /Users/apple/Downloads/sonar-scanner-msbuild-2.3.2.573/SonarQube.Analysis.xml
Post-processing started.
SONAR_SCANNER_OPTS is not configured. Setting it to the default value of -Xmx1024m
Calling the SonarQube Scanner...
Execution failed. The specified executable does not exist: /Users/apple/Downloads/sonar-scanner-msbuild-2.3.2.573/sonar-scanner-3.0.3.778\bin\sonar-scanner.bat
The SonarQube Scanner did not complete successfully
13:17:17.361 Creating a summary markdown file...
13:17:17.366 Post-processing failed. Exit code: 1
This is currently not supported to use the scanner on non Windows platforms.
There is an open issue for that: https://jira.sonarsource.com/browse/SONARMSBRU-319
Now SonarQube.Scanner.MSBuild.exe has the support for mac, it works.
The sonarqube for MSBuild version 3.0.2 was not supporting Non window OS, now the SonarQube.Scanner.MSBuild.exe 4.0 has the support for mac
Also the sonarC# Plugin has to be > 6.6 to run the analysis on mac os version.
Please check the below link,
https://forums.xamarin.com/discussion/111935/source-code-analysis-for-xamarin-project-with-sonarqube-on-mac/p1?new=1

Running SonarQube without TFS Build?

I was asked to integrate another project (C#) into SonarQube. We use TFS 2013, SonarQube 5.1.2 with Sonar.MSBuild.Runner 1.0.
The Build process template is old (does not support pre and post Powershell scripts) and it is customized. Visual Studio 2013 is unable to edit the old build process template.
I tried to do just the static code analysis and run the MSBuild.SonarQube.Runner with the start parameter and a second time with the end parameter without doing a build in between (The code coverage does not seem important to them). But then I get the error "No ProjectInfo.xml files were found. Check that the analysis targets are referenced by the MSBuild projects being build"?
What options do I have to integrate this project into SonarQube?
To use the MSBuild SonarQube Runner from the command line, you have to:
Prepare the environment: MSBuild.SonarQube.Runner.exe begin /k:SQ_project_key /n:SQ_project_name /v:SQ_project_version (pick arbitrary SonarQube project key, name and version)
Run your build: msbuild
Upload analysis results: MSBuild.SonarQube.Runner.exe end
The MSBuild SonarQube Runner integrates with MSBuild and collects informations as it builds your project: You must run the build during the begin and end invocations, or this mandatory information won't be available and the MSBuild SonarQube Runner will fail with the message you reported. The MSBuild integration is compatible with MSBuild versions 4.0, 12 and 14.
Regarding the integration with TFS XAML builds, if you're using a customized one, you'll have to customize it a bit more to integrate the calls to the beginand end commands of the MSBuild SonarQube Runner.
Code Coverage is supported out of the box on TFS 2013 with the default build definition template, you just need to run tests with coverage enabled. If that doesn't work for you, see this link on how to manually pass the code coverage results path property: http://docs.sonarqube.org/x/CoBh

Categories

Resources