After FxCop 10 Microsoft stopped shipping a separate installer for FxCop. Officially one can currently only run code analysis (FxCop 12.0 / 14.0 / 15.0) after installing Visual Studio 2013 / 2015 / 2017. However, we are adamant about not installing Visual Studio on the build agents (the installation needs then to be kept in-sync with what we have got on the developer computers etc.).
So how would i go about getting FxCop 12.0 / 14.0 / 15.0 to work on a build agent, preferrably without installing anything else? I would accept adding a few binaries and msbuild files to source control, though. Otherwise: Is there a way to only install the FxCop stuff using Visual Studio 2013 / 2015 / 2017 installer?
Note: we are using Teamcity as build server.
Answers
As there are multiple valid answers for specific environments and FxCop versions, I've taken the liberty of linking them here for easier access:
Run FxCop 15.0 without installing Visual Studio 2017
Prerequisites:
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.CodeAnalysis.Interop.dll' or one of its dependencies. The specified module could not be found.
). Instead of installing the entire redist you could also copy the necessary DLLs separately, but at this point i don't know which are needed. It's quite tricky and time consuming to find out which ones are exactly missing.Depending on what you want to build:
Files to Add to Source Control
These are the files that i had to add to source control: (Please consider that this might violate some license agreements)
Copy them as follows:
entire FxCop installation folder contents from
%programfiles(x86)%\Microsoft Visual Studio\2017\ **INSERT EDITION HERE** \Team Tools\Static Analysis Tools\FxCop
from Visual Studio 2017 C++ redist (or your Visual Studio 2017 Installation, if you've got VC++ workload installed: %ProgramFiles(x86)%\Microsoft Visual Studio\2017\\VC\Redist\MSVC\14.12.25810) or any other place: (also see legal information) copy msdia140 x86 and x64 (mine have the version 14.12.25810.0) to:
msdia140.dll (1.12 MiB)
amd64\msdia140.dll (1.42 MiB)
All files from
%programfiles(x86)%\\Microsoft Visual Studio\2017\ **INSERT EDITION HERE** \MSBuild\Microsoft\VisualStudio\v15.0\CodeAnalysis
toMsbuild\fxcoptask.dll
Msbuild\Microsoft.CodeAnalysis.Targets
Msbuild\Microsoft.VisualStudio.CodeAnalysis.dll
Msbuild\Microsoft.VisualStudio.CodeAnalysis.Sdk.dll
Additionally i adjusted the project msbuild file (*.csproj) as follows:
And this is what our Custom.CodeAnalysis.targets contains:
Notes for Visual Studio Paths:
The path to the visual studio installation is edition dependent. It includes Professional and Enterprise. AFAIR the community edition has a different path, but also does not support FxCop. Corrections welcome :-)
There's a very simple way for TFS:
On the build machine, add an environment variable:
FXCOPDIR
, point it to:C:\Program Files (x86)\Microsoft Visual Studio 12.0\Team Tools\Static Analysis Tools\FxCop
(just copy this folder from your dev machine).In this directory there's a file called:
FxCopCmd.exe
and TFS Build will pick it up automatically, even if vs is not installed. You might need to re-boot the server once or twice, but the build will eventually work. You'll also need to ensure that two dlls are installed to the gac: Microsoft.VisualStudio.CodeAnalysis.dll and Microsoft.VisualStudio.CodeAnalysis.Sdk.dll The first one can be found in the path mentioned above, but the latter, the only way i got a hold of it is that i grabbed it from the gac on my dev machine. (you can disable the special gac folder view, by modifying the registry, just google it).For FxCop 14.0 / VS2015 see this answer
Run FxCop 12.0 without installing Visual Studio 2013
Okay, i've invested 6 hours and now it's working. I've added all necessary executables, dlls and msbuild targets to source control.
These are the files that i had to add to source control: (Please consider that this might violate some license agreements)
Copy them as follows:
entire FxCop installation folder contents from
%programfiles(x86)%\Microsoft Visual Studio 12.0\Team Tools\Static Analysis Tools\FxCop
from Visual Studio 2013 C++ redist, or any other place: (also see legal information) copy msdia120 x86 and x64 to:
msdia120.dll (874 KiB)
amd64\msdia120.dll (1.07 MiB)
from the Global Assembly Cache (
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\_NameOfTheAssembly_\
) of a computer where VS2013 is installed, copy the following DLLs to: (Make sure the DLLs are version 12.0 !)Msbuild\Microsoft.VisualStudio.CodeAnalysis.dll
Msbuild\Microsoft.VisualStudio.CodeAnalysis.Sdk.dll
All files from
%programfiles(x86)%\MSBuild\Microsoft\VisualStudio\v12.0\CodeAnalysis
toMsbuild\fxcoptask.dll
Msbuild\Microsoft.CodeAnalysis.Targets
(Furthermore you need the appropriate Windows (7.1 / 8.1) SDK for building the .net 4.0 / 4.5 application installed on the build agent)
Additionally we had to adjust the project msbuild file as follows:
And this is what our Custom.CodeAnalysis.targets contains:
Using FxCop for SonarQube analysis without installing Visual Studio
If you have any FxCop rules enabled in your SonarQube quality profile, SonarQube requires FxCop 14.0.
These steps are largely based on the outstanding answer by @BatteryBackupUnit:
Install Microsoft Build Tools 2015 on the build machine.
Install Microsoft Visual C++ 2015 Redistributable Update 3, both x86 and x64, on the build machine.
Import the following registry file (update drive letter if not C:) on the build machine:
Copy all of
%programfiles(x86)%\Microsoft Visual Studio 12.0\Team Tools\Static Analysis Tools
(not just the FxCop subdirectory) to the same place on the build machine.Copy
%programfiles(x86)%\MSBuild\Microsoft\VisualStudio\v14.0\CodeAnalysis
to the same place on the build machine.From the Global Assembly Cache (C:\Windows\Microsoft.NET\assembly\GAC_MSIL_NameOfTheAssembly_) of a computer where VS2015 is installed, copy
Microsoft.VisualStudio.CodeAnalysis.dll
andMicrosoft.VisualStudio.CodeAnalysis.Sdk.dll
(Make sure the DLLs are version 14.0!). Either copy toC:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\CodeAnalysis
on the build machine or install into the build machine's GAC.If you see
Phx.FatalError
after all this - there's one last ritual... Copyvs_profiler_x64_enu.exe
fromProgram Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Performance Tools\Setups
and install it on the build machine.Deviations from @BatteryBackupUnit's instructions:
Static Analysis Tools
directory is needed, not just the FxCop subdirectory.msdia120.dll
andamd64\msdia140.dll
files into the FxCop directory seems unnecessary so long as the Visual C++ 2015 Redistributable is installed.Custom.CodeAnalysis.targets
file.!! For FxCop 12.0 / VS2013 see this answer !!
Run FxCop 14.0 without installing Visual Studio 2015
Prerequisites:
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.CodeAnalysis.Interop.dll' or one of its dependencies. The specified module could not be found.
). Instead of installing the entire redist you could also copy the necessary DLLs separately, but at this point i don't know which are needed. It's quite tricky and time consuming to find out which ones are exactly missing.Depending on what you want to build:
Files to Add to Source Control
These are the files that i had to add to source control: (Please consider that this might violate some license agreements)
Copy them as follows:
entire FxCop installation folder contents from
%programfiles(x86)%\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\FxCop
from Visual Studio 2015 C++ redist, or any other place: (also see legal information) copy msdia140 x86 and x64 to:
msdia140.dll (1057 KiB)
amd64\msdia140.dll (1349 KiB)
from the Global Assembly Cache (
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\_NameOfTheAssembly_\
) of a computer where VS2015 is installed, copy the following DLLs to: (Make sure the DLLs are version 14.0 !)Msbuild\Microsoft.VisualStudio.CodeAnalysis.dll
Msbuild\Microsoft.VisualStudio.CodeAnalysis.Sdk.dll
All files from
%programfiles(x86)%\MSBuild\Microsoft\VisualStudio\v14.0\CodeAnalysis
toMsbuild\fxcoptask.dll
Msbuild\Microsoft.CodeAnalysis.Targets
Additionally i adjusted the project msbuild file (*.csproj) as follows (Hint: i'm slightly deviating from the way i previously did it with VS2013. It's not because FxCop 14 works differently but rather because this way i can include fxcop by a nuget package and use standard nuget functionality for .targets file import to .csproj):
And this is what our Custom.CodeAnalysis.targets contains: