Run SonarCloud Analyze Manually From Visual Studio - c#

I have paid account in Sonarcloud and Gitlab CI for automation, I use Visual Studio with integrated SonarLint for C#. Sometimes I want to run analyzing manually from VS and not using Gitlab runner every time. Is there a way to bypass pipeline? The reason is that I have limitations for runner minutes and I want to make commit and run pipeline only when I cleanup code warnings from SonarLint.

SonarScanner
You can trigger a SonarCloud analysis locally using SonarScanner:
SonarScanner for MSBuild as a stand-alone executable.
.NET Core global Tool installed with NuGet, also called as executable.
Download the SonarScanner CLI binary and run it from the command line.
The MSBuild option looks something like this:
SonarScanner.MSBuild.exe begin /k:"project-key"
MSBuild.exe <path to solution.sln> /t:Rebuild
SonarScanner.MSBuild.exe end
The .NET Core global tool looks like this:
dotnet tool install --global dotnet-sonarscanner
dotnet sonarscanner begin /k:"project-key" /d:sonar.login="myAuthenticationToken"
dotnet build <path to solution.sln>
dotnet sonarscanner end /d:sonar.login="myAuthenticationToken"
Since You need to call SonarScanner before and after a build. you can integrate the SonarScanner commands in Visual Studio using Build Events. Add the commands to PreBuildEvent.bat and PostBuildEvent.bat to run the analysis automatically every time you build. If you don't want to run SonarScanner every time you build, create a new custom build configuration (e.g. Debug, Release, Analyze).
You will need to generate a private token for your project key in SonarCloud. The final report will also be available in that project when it is ready.
SonarLint
If you don't need to update SonarCloud and just want to view the code analysis results, you can use SonarLint Visual Studio extension. You can connect SonarLint with SonarCloud to download your common configuration using a feature called Connected Mode.
Connected mode does not push issues to the server. Rather, its purpose is to configure the IDE so that it uses the same settings as the server.
A summary of instructions (see Connected Mode for details):
Open the Team Explorer Home tab and click on the SonarQube icon
Click on Connect... to display the connection dialogue
Select the server and enter your credentials
Select the Organization (SonarCloud only)
Select the Sonar project to bind to
SonarLint will then fetch the required settings from the server and create local configuration files
Your code will be analyzed live (as you type), or you can run a full code analysis as follows:
Right click solution -> Analysis -> Run Code Analysis

You can't do this from Visual Studio, but you can make this like powershell script.

Related

MSBuild ignoring /p:BuildProjectReferences=false on Azure build server

I've a solution with three projects in it: 2 C# WPF projects and 1 WIX project to create an installer. I've set up a build pipeline on Azure DevOps that references our own build server so I have access to the OS.
I want the Wix installer project to be dependant on the two C# projects for my local test builds but for the Azure Pipeline, I need to build the C# projects first and sign the executables before creating the MSI installer with the WIX project (that needs to be signed, too).
The problem is that the build server ignores "/p:BuildProjectReferences=false" (which should force msbuild to NOT build the dependencies) while my local computer does not when I run the exact same command line on both:
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\msbuild.exe" Solution.sln /p:AppxBundle=Always /p:UapAppxPackageBuildMode=StoreUpload /p:RunWixToolsOutOfProc=true /p:BuildProjectReferences=false /target:Installer /m:4 /p:Platform="x64" /p:Configuration="Release"
Extra info
I tried with and without Rebuild option (/target:Installer:Rebuild)
The build server fetches the git repository so it's exactly the same project on both sides.
Both Visual Studio installations are VS 2019 build version 16.11.14
Both Wix Toolset installations are version v3.11.2.4516
Any ideas how to get the build server to not build the dependencies?
Well this is embarrassing. Apparently I had some change on my local computer that wasn't committed yet. So code on both platforms wasn't the same...
The fix was a change in the *.sln file
Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "Installer", "Installer\Installer.wixproj", "{A9EDFE69-D273-42EE-97EA-469C3434AEDC}"
ProjectSection(ProjectDependencies) = postProject
{C784B33D-1E8D-41EF-97C2-983CA9A5B693} = {C784B33D-1E8D-41EF-97C2-983CA9A5B693}
{B6E37FA9-64CF-491B-B268-9D115AABC6B2} = {B6E37FA9-64CF-491B-B268-9D115AABC6B2}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPFProject1", "WPFProject1\WPFProject1.csproj", "{C784B33D-1E8D-41EF-97C2-983CA9A5B693}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPFProject2", "WPFProject2\WPFProject2.csproj", "{B6E37FA9-64CF-491B-B268-9D115AABC6B2}"
EndProject
I removed the 'ProjectSection' in the first project (Installer). This resulted in the code being built completely in Visual Studio but not for the Azure Pipelines because of the use of the /p:BuildProjectReferences=false param in the pipeline build command.
The two WPF projects still exist as depedencies in Installer project.
I hope this helps someone someday (or myself in a future project)

Cannot find .NET 5.0 Console App project template in Visual Studio 2019

I'm currently trying to create a C# .NET 5.0 Console Application in Visual Studio 2019, and the option does not appear under project templates as I expect it to.
I downloaded Visual Studio Professional 2019 Version 16.9.4 (from https://visualstudio.microsoft.com/downloads/) and selected all workloads to be installed, then I installed SDK 5.0.202, ASP.NET Core Runtime 5.0.5, .NET Desktop Runtime 5.0.5, and .NET Runtime 5.0.5 (from https://dotnet.microsoft.com/download/dotnet/5.0).
When I go to File -> New -> Project and search for "Console Application", there is no default "Console Application" project template for C#. The closest I can find is "Console Application (.NET Framework)" for C#, VB, and F# and "Console Application" for C++. If I select "Console Application (.NET Framework)" for C#, the latest Framework that I can choose is ".NET Framework 4.8".
Actual Outcome: Project Templates
Actual Outcome: Frameworks
I compared notes with a developer who did not run into this problem, and they see the same framework restriction when they select "Console Application (.NET Framework)" for C#, but is also able to see the project template "Console Application" for C# (without ".NET Framework") and select ".NET 5.0 (Current)" as the target framework as expected. Nothing about what they installed and updated appeared to be noticeably different, our results for the command prompt command "dotnet --info" matched, and they did not have any "Installed products" listed under "About" or "Individual Components" listed under the installer that I was missing.
Expected Outcome: Project Templates
Expected Outcome: Frameworks
I have tried uninstalling and reinstalling everything, exhaustively installing every workload as well as additional individual components that looked potentially relevant, and making sure that the "Tools -> Options -> Environment -> Preview Features -> Show All .NET Core templates in the New project dialog (requires restart)" checkbox is checked.
Does anyone know what step I'm missing that is preventing me from creating a Console Application project template in C# using .NET 5.0?
Background -- This change/broke, issue that Microsoft opted deliberately you can read more here and the issue reported here but you can fix this with the 2 options listed below, i.e. A) enable it OR B) add it to VS via the repair tool
suggest clear VStudio Cache's -- kill dev process & clear cache before re/starting VS
run this cmd as admin in powershell
Get-Process devenv | Foreach-Object { $_.CloseMainWindow() | Out-Null }
Then DELETE ItemTemplatesCache, ProjectTemplatesCache folder ent. prof. etc.. C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE.
Now please Proceed with these Steps/Options
Option 1: you may have to enable this option...
Tools > Options > Preview Features > “Show all .NET Core templates in the New project dialog (requires restart)”.
Option 2: Install via Visual Studio Installer like so..
You can install missing workloads using the repair VS (this will also update the installer) and make sure you check the optional packages you want from the VS Installer tool please see this
after the installer starts check if the .NET Core workload is selected for desktop and console.. like below.. on the right pane you will need to check the correct boxes
Clear Visual Studio Component Cache (& optionally all Your templates)
Now, if you have already followed the above steps, then verifying you selected the checkbox of ".net-Desktop development" on top right is selected or not on Visual Studio installer: delete the cache and restore
i.e. you could restore the template in VS2017/2019 then try it again.
Kill dev process first run this Get-Process devenv | Foreach-Object { $_.CloseMainWindow() | Out-Null }
Please refer to the following steps:
DELETE the ItemTemplatesCache, ProjectTemplatesCache folder Ent. or Prof. version etc.. C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE. & C:\Users\<USERNAME>\AppData\Local\Microsoft\VisualStudio\16.0_93de0ddd
In the command prompt, navigate to the location of devenv.exe. This file is located in \Common7\IDE.
Type/Run this command devenv /installvstemplates and press Enter.
FYI - your templates cache is here, containing folders named after the zip files from your archives in the template folder.
you will need admin privilege's for edits/delete.
//templates cache Microsoft
%AppData%\Microsoft\VisualStudio\{some_version}\ItemTemplatesCache\
//templates path for verification, repair/reinstall or delete
%USERPROFILE%\Documents\Visual Studio 2019\Templates\ProjectTemplates
%USERPROFILE%\Documents\Visual Studio 2019\Templates\ItemTemplates
%ProgramFiles(x86)%\Microsoft Visual Studio\2019\<edition>\Common7\IDE\ItemTemplates\
%ProgramFiles(x86)%\Microsoft Visual Studio\2019\<edition>\Common7\IDE\ProjectTemplates\
Option 3: debugging your env. Template Config folder
Checking/Diagnosing the version:
run this dotnet --version
install the correct core version https://dotnet.microsoft.com/download/dotnet/5.0 more here
this dotnet new -u will tell you which templates are installed, you can uninstall, clear the packages cache, restart and reinstall with the repair tool.
Download and run collect.exe - https://aka.ms/vscollect - and share the log file created in %TEMP%\vslogs.zip with MS on github
FYI - This is what your template folder structure in visual studio should look like.
└───mytemplate
│ console.cs
│ readme.txt
│
└───.template.config
template.json
Eventually clear the MEF component cache, manually or with this extension.
But most likely something is wrong in the user cache at C:\Users\USERNAME\AppData\Local\Microsoft\VisualStudio\16.0_93de0ddd, which includes in particular InstalledTemplates.json and privateregistry.bin.
Try and rename (or delete) the folder, (or one the the two mentionned files to narrow the problem down), while VS is off, and restart VS. Only make sure that you have the proper workloads installed and the .net 5 from VS.
While it can be hard to tell what's exactly the issue with VS or local environment you have, it should be possible to temporary create projects with dotnet cli:
dotnet new console --output <project-dir>
If you would want to add it to an existing solution run
dotnet sln <solution-name>.sln add <project-dir>
The console app project file is very simple, so you can even create it manually, or create a project from any existing template and replace the contents with following asa workaround:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>
And maybe one more thing you could try is to run the VS installer as admin or ensure that it can install and access all the required components.
I checked and I am able to create dotnet5.0 console application with visual studio version 16.9.2 and SDK version 5.0.201. I think there is some installation conflict on your end. You can try uninstalling the SDK and reinstalling visual studio or updating it to latest version and downloading latest SDK. Also check if SDK is getting installed on your system under
C:\Program Files\dotnet\sdk[your SDK version]\
After a year of just dealing with it, I FINALLY FIGURED IT OUT.
Go to C:\Program Files\dotnet\templates then open every folder there. Apparently it was a permission issue because I was getting:
[Window Title]
6.0.6
[Main Instruction]
You don't currently have permission to access this folder.
[Content]
Click Continue to permanently get access to this folder.
[Continue] [Cancel]
After selecting Continue for each folder, everything started working.
The error that resulted from running dotnet new --list is what gave me the heads up.
My solution was deleting C:\Users\USERNAME.templateengine\
I have VS 2019 version 16.9.5 when I seen your post. I am using console app for my sample projects. I have tried it and able to achieve what you are looking for. I would recommend to update your VS to 16.9.5 and give it a try.
My VS 2019 version:
Creating Console app in VS 2019 16.9.5:
Finally it has created a console app project with target framework as .net 5.0

Under what cases does a VSTS build produce an empty drop

I have a .Net (VS 2015) application that I manually build using VSTS. The build was successfully completed but no artifacts were generated. The agents/tasks used in this manual build are the NuGet tool installer, NuGet restore, Visual Studio build, publish build artifact.
Visual Studio Build parameters:
Publish Build Artifact parameters:
Variables added:
End Result:
A similar structure should be produced as per my expectation:
Before the "Publish artifacts" task, you need to copy to the folder a in the agent the files you want they will be in the artifacts.
For example:
Now in your artifacts folder (a on the agent) will be your release output and in the Publish task it will be published as artifacts.

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

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