Running Code Analysis (FxCop > 10) on build agent without installing Visual Studio
Asked Answered
S

5

57

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:

Sam answered 12/2, 2014 at 13:17 Comment(1)
I hate anything that suggests "install visual studio" on the build machine. boo microsoft.Wessling
S
3

Run FxCop 15.0 without installing Visual Studio 2017

Prerequisites:

  • MSBuild 15.0 --> Install Microsoft Build Tools 2017, you might also want to see here
  • Visual C++ Redistributable for Visual Studio 2017 x86 or here OR Visual Studio 2017 with C++. ---- Note: the x86 version is always required, depending on the build x64 redist may be required as well. If it is missing the error message can be cryptic, for example 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:

  • appropriate Windows SDK, for example Windows 10 SDK
  • appropriate .net SDK / targets (.NET Framework 4.6 SDK is included in Windows 10 SDK)

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)

(source control)\tools\FxCop15
│
├[Engines]
│   │
│   ├IntrospectionAnalysisEngine.dll
│   └PhoenixAnalysisEngine.dll
├[Msbuild]
│   │
│   ├fxcoptask.dll
│   ├Microsoft.CodeAnalysis.Targets
│   ├Microsoft.VisualStudio.CodeAnalysis.dll
│   └Microsoft.VisualStudio.CodeAnalysis.Sdk.dll
├[Repository]
│   │
│   ├[Compatibility]
│   │   │
│   │   ├Desktop2.0.xml
│   │   ├Desktop2.0SP1.xml
│   │   ├Desktop2.0SP2.xml
│   │   ├Desktop3.0.xml
│   │   ├Desktop3.0SP1.xml
│   │   ├Desktop3.0SP2.xml
│   │   ├Desktop3.5.xml
│   │   └Desktop3.5SP1.xml
│   └system32.bin
├[Rules]
│   │
│   ├DataflowRules.dll
│   ├DesignRules.dll
│   ├GlobalizationRules.dll
│   ├InteroperabilityRules.dll
│   ├MaintainabilityRules.dll
│   ├MobilityRules.dll
│   ├NamingRules.dll
│   ├PerformanceRules.dll
│   ├PortabilityRules.dll
│   ├ReliabilityRules.dll
│   ├SecurityRules.dll
│   ├SecurityTransparencyRules.dll
│   └UsageRules.dll
├[x64]
│   │
│   └msdia140.dll (1349 KB)
├[Xml]
│   │
│   ├CodeAnalysisReport.xsl
│   ├FxCopReport.xsl
│   └VSConsoleOutput.xsl
├Architecture-msil.dll
├CodeAnalysis.dll
├CustomDictionary.xml
├FxCopCmd.exe
├FxCopCmd.exe.config
├FxCopCommon.dll
├FxCopSdk.dll
├Microsoft.Cci.dll
├Microsoft.VisualStudio.CodeAnalysis.Common.dll
├Microsoft.VisualStudio.CodeAnalysis.DataflowModels.dll
├Microsoft.VisualStudio.CodeAnalysis.dll
├Microsoft.VisualStudio.CodeAnalysis.Interop.dll
├Microsoft.VisualStudio.CodeAnalysis.Phoenix.dll
├Microsoft.VisualStudio.CodeAnalysis.Phoenix.xml
├msdia140.dll (1057 KB)
├mssp7en.dll
├mssp7en.lex
├phx.dll
└Runtime-vccrt-win-msil.dll

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 to

    Msbuild\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:

<!-- Microsoft.CSharp.targets import is contained in csproj by default. This just goes to show the sequence -->
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>

<!-- now this must be added -->
<Import Project="$(ProjectBuildScriptDir)Custom.CodeAnalysis.targets"/>

And this is what our Custom.CodeAnalysis.targets contains:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <!-- Code analysis settings. -->
    <PropertyGroup>
        <!-- this must reference the path where you copied the FxCop stuff to -->
        <FxCopDir>..\FxCop15\</FxCopDir> 
        <CodeAnalysisCulture>en-US</CodeAnalysisCulture>
        <CodeAnalysisRuleSet>$(SolutionDir)FxCop.ruleset</CodeAnalysisRuleSet>
        <!-- you can and should use another condition here. Otherwise code analysis will be run on every build in VS as well. -->
        <!-- in my build setup i do something like Condition=IsRunningOnTeamCity => true -->
        <RunCodeAnalysis>true</RunCodeAnalysis> 
        <CodeAnalysisTreatWarningsAsErrors Condition="'$(IsRunningOnTeamCity)' != 'true'">true</CodeAnalysisTreatWarningsAsErrors>
    </PropertyGroup>

    <Import Project="$(FxCopDir)Msbuild\Microsoft.CodeAnalysis.Targets" />

    <Target Name="CodeAnalysisLogHeader" BeforeTargets="RunCodeAnalysis" Condition="$(RunCodeAnalysis) == 'true' OR $(RunCodeAnalysisOnce) == 'true'">
        <Message Text="Text, Executing Code Analysis (FxCop) on $(MsBuildProjectName)" Importance="High" />
    </Target>

</Project>

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 :-)

Sam answered 5/3, 2018 at 16:45 Comment(0)
S
52

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)

(source control)\dev\tools\FxCop
│
├[amd64]
│   │
│   └msdia120.dll
├[Engines]
│   │
│   ├IntrospectionAnalysisEngine.dll
│   └PhoenixAnalysisEngine.dll
├[Msbuild]
│   │
│   ├fxcoptask.dll
│   ├Microsoft.CodeAnalysis.Targets
│   ├Microsoft.VisualStudio.CodeAnalysis.dll
│   └Microsoft.VisualStudio.CodeAnalysis.Sdk.dll
├[Repository]
│   │
│   ├[Compatibility]
│   │   │
│   │   ├Desktop2.0.xml
│   │   ├Desktop2.0SP1.xml
│   │   ├Desktop2.0SP2.xml
│   │   ├Desktop3.0.xml
│   │   ├Desktop3.0SP1.xml
│   │   ├Desktop3.0SP2.xml
│   │   ├Desktop3.5.xml
│   │   └Desktop3.5SP1.xml
│   └system32.bin
├[Rules]
│   │
│   ├DataflowRules.dll
│   ├DesignRules.dll
│   ├GlobalizationRules.dll
│   ├InteroperabilityRules.dll
│   ├MaintainabilityRules.dll
│   ├MobilityRules.dll
│   ├NamingRules.dll
│   ├PerformanceRules.dll
│   ├PortabilityRules.dll
│   ├ReliabilityRules.dll
│   ├SecurityRules.dll
│   ├SecurityTransparencyRules.dll
│   └UsageRules.dll
├[Xml]
│   │
│   ├CodeAnalysisReport.xsl
│   ├FxCopReport.xsl
│   └VSConsoleOutput.xsl
├Architecture-msil.dll
├CodeAnalysis.dll
├CustomDictionary.xml
├FxCopCmd.exe
├FxCopCmd.exe.config
├FxCopCommon.dll
├FxCopSdk.dll
├Microsoft.Cci.dll
├Microsoft.VisualStudio.CodeAnalysis.Common.dll
├Microsoft.VisualStudio.CodeAnalysis.DataflowModels.dll
├Microsoft.VisualStudio.CodeAnalysis.dll
├Microsoft.VisualStudio.CodeAnalysis.Interop.dll
├Microsoft.VisualStudio.CodeAnalysis.Phoenix.dll
├Microsoft.VisualStudio.CodeAnalysis.Phoenix.xml
├msdia120.dll
├mssp7en.dll
├mssp7en.lex
├phx.dll
└Runtime-vccrt-win-msil.dll

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 to

    Msbuild\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:

<!--Must import code analysis target before importing csharp targets, so that the correct code analysis targets gets imported. -->
<Import Project="$(ProjectBuildScriptDir)Custom.CodeAnalysis.targets"/>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>

And this is what our Custom.CodeAnalysis.targets contains:

  <PropertyGroup>
    <!-- Code analysis settings. -->
    <CodeAnalysisCulture>en-US</CodeAnalysisCulture>

    <!-- change this so it points to your ruleset or remove it entirely -->
    <CodeAnalysisRuleSet>$(SourcesDir)Custom.ruleset</CodeAnalysisRuleSet>

    <!-- this must refer to the source control directory where you copied FxCopCommand.exe (and the rest of the FxCop files and directories...) to -->
    <CodeAnalysisPath>$(ToolsDir)FxCop\</CodeAnalysisPath>

    <!-- this must refer to the source control directory where you copied fxcoptask.dll, Microsoft.CodeAnalysis.Targets, Microsoft.VisualStudio.CodeaAnalysis.dll and Microsoft.VisualStudio.CodeaAnalysis.Sdk.dll to -->
    <CodeAnalysisTargets>$(CodeAnalysisPath)Msbuild\Microsoft.CodeAnalysis.Targets</CodeAnalysisTargets>
  </PropertyGroup>
  <!-- configure this according to your wishes -->

  <PropertyGroup Condition="'$(Configuration)' == 'Release'">
    <CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>
    <RunCodeAnalysis>true</RunCodeAnalysis>
  </PropertyGroup>

  <!-- Report code analysis results to TeamCity -->
  <Target Name="ReportCodeAnalysisResults" AfterTargets="RunCodeAnalysis" Condition="'$(RunCodeAnalysis)' == 'true' And '$(IsRunningOnTeamCity)' == 'true'">
    <Message Text="##teamcity[importData type='FxCop' path='$(CodeAnalysisLogFile)']" Importance="High" />
  </Target>

</Project>
Sam answered 12/2, 2014 at 14:49 Comment(6)
We got the following error on our build server: "Could not load file or assembly 'Microsoft.VisualStudio.CodeAnalysis.Interop.dll' or one of its dependencies." even though all specified files in the answer above was copied. The complete error message can be found here: pastebin.com/cJhjHYBZ The solution was to install "Visual C++ Redistributable Packages for Visual Studio 2013"Aspirant
@Aspirant can you please check that you've copied msdia120.dll to the correct place and the correct version (x86 vs x64)? As far as i know this is the only dll from VC++ Redist 2013 that's needed. If you run x86 don't copy it to the amd64 folder.Sam
Now I'm trying to do the same with FxCop 14 (from VS2015) with no luck. getting Could not load file or assembly 'Microsoft.VisualStudio.CodeAnalysis.Interop.dll' or one of its dependencies. all the time. Tryed installing .Sdk to GAC, even .Interop to gac too - nothing helps. Trace shows Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. but you can`t get to LoaderExceptions without debugger attached.Picturize
@Picturize I suspect this will require the DLLs of Visual C++ Redistributable for Visual Studio 2015. Can you install it and check if it works? Please also post back your results so i can update the answer. If it doesn't work it most likely means that FXCop 14 consists of more dlls (which also need to be copied).Sam
@Picturize see here for an updated answer regarding FxCop 14.0Sam
@Sam The link msdn.microsoft.com/en-us/vstudio/dn501987 is broken, Could you please provide the updated linkStorey
S
37

!! For FxCop 12.0 / VS2013 see this answer !!

Run FxCop 14.0 without installing Visual Studio 2015

Prerequisites:

  • MSBuild 14.0 --> Install Microsoft Build Tools 2015
  • Visual C++ Redistributable for Visual Studio 2015 x86 (the x86 version is always required, depending on the build x64 redist may be required as well. If it is missing the error message can be cryptic, for example 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:

  • appropriate Windows SDK, for example Windows 10 SDK
  • appropriate .net SDK / targets (.NET Framework 4.6 SDK is included in Windows 10 SDK)

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)

(source control)\tools\FxCop14
│
├[Engines]
│   │
│   ├IntrospectionAnalysisEngine.dll
│   └PhoenixAnalysisEngine.dll
├[Msbuild]
│   │
│   ├fxcoptask.dll
│   ├Microsoft.CodeAnalysis.Targets
│   ├Microsoft.VisualStudio.CodeAnalysis.dll
│   └Microsoft.VisualStudio.CodeAnalysis.Sdk.dll
├[Repository]
│   │
│   ├[Compatibility]
│   │   │
│   │   ├Desktop2.0.xml
│   │   ├Desktop2.0SP1.xml
│   │   ├Desktop2.0SP2.xml
│   │   ├Desktop3.0.xml
│   │   ├Desktop3.0SP1.xml
│   │   ├Desktop3.0SP2.xml
│   │   ├Desktop3.5.xml
│   │   └Desktop3.5SP1.xml
│   └system32.bin
├[Rules]
│   │
│   ├DataflowRules.dll
│   ├DesignRules.dll
│   ├GlobalizationRules.dll
│   ├InteroperabilityRules.dll
│   ├MaintainabilityRules.dll
│   ├MobilityRules.dll
│   ├NamingRules.dll
│   ├PerformanceRules.dll
│   ├PortabilityRules.dll
│   ├ReliabilityRules.dll
│   ├SecurityRules.dll
│   ├SecurityTransparencyRules.dll
│   └UsageRules.dll
├[x64]
│   │
│   └msdia140.dll (1349 KB)
├[Xml]
│   │
│   ├CodeAnalysisReport.xsl
│   ├FxCopReport.xsl
│   └VSConsoleOutput.xsl
├Architecture-msil.dll
├CodeAnalysis.dll
├CustomDictionary.xml
├FxCopCmd.exe
├FxCopCmd.exe.config
├FxCopCommon.dll
├FxCopSdk.dll
├Microsoft.Cci.dll
├Microsoft.VisualStudio.CodeAnalysis.Common.dll
├Microsoft.VisualStudio.CodeAnalysis.DataflowModels.dll
├Microsoft.VisualStudio.CodeAnalysis.dll
├Microsoft.VisualStudio.CodeAnalysis.Interop.dll
├Microsoft.VisualStudio.CodeAnalysis.Phoenix.dll
├Microsoft.VisualStudio.CodeAnalysis.Phoenix.xml
├msdia140.dll (1057 KB)
├mssp7en.dll
├mssp7en.lex
├phx.dll
└Runtime-vccrt-win-msil.dll

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 to

    Msbuild\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):

<!-- Microsoft.CSharp.targets import is contained in csproj by default. This just goes to show the sequence -->
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>

<!-- now this must be added -->
<Import Project="$(ProjectBuildScriptDir)Custom.CodeAnalysis.targets"/>

And this is what our Custom.CodeAnalysis.targets contains:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <!-- Code analysis settings. -->
    <PropertyGroup>
        <!-- this must reference the path where you copied the FxCop stuff to -->
        <FxCopDir>..\FxCop14\</FxCopDir> 
        <CodeAnalysisCulture>en-US</CodeAnalysisCulture>
        <CodeAnalysisRuleSet>$(SolutionDir)FxCop.ruleset</CodeAnalysisRuleSet>
        <!-- you can and should use another condition here. Otherwise code analysis will be run on every build in VS as well. -->
        <!-- in my build setup i do something like Condition=IsRunningOnTeamCity => true -->
        <RunCodeAnalysis>true</RunCodeAnalysis> 
        <CodeAnalysisTreatWarningsAsErrors Condition="'$(IsRunningOnTeamCity)' != 'true'">true</CodeAnalysisTreatWarningsAsErrors>
    </PropertyGroup>

    <Import Project="$(FxCopDir)Msbuild\Microsoft.CodeAnalysis.Targets" />

    <Target Name="CodeAnalysisLogHeader" BeforeTargets="RunCodeAnalysis" Condition="$(RunCodeAnalysis) == 'true'">
        <Message Text="Text, Executing Code Analysis (FxCop) on $(MsBuildProjectName)" Importance="High" />
    </Target>

    <!-- Report code analysis results to TeamCity -->
    <Target Name="ReportCodeAnalysisResults" AfterTargets="RunCodeAnalysis" Condition="$(RunCodeAnalysis) == 'true' AND '$(IsRunningOnTeamCity)' == 'true'">
        <Message Text="##teamcity[importData type='FxCop' path='$(MSBuildProjectDirectory)\$(CodeAnalysisLogFile)']" Importance="High" />
    </Target>

</Project>
Sam answered 19/8, 2015 at 11:15 Comment(6)
Is anyone interested in the source code for creating a nuget package with FxCop 14.0 that can then be installed into the projects? Due to licensing restrictions i think i cannot publish the package but i could publish all code necessary to create the actual package (which you then would have to "host" yourself...)Sam
I've ended up installing vc++ redist on build machine. I've made .proj file that copies all the files needed and installs vc++ redist simply like this: <ItemGroup> <FxCopLibs Include="$(MSBuildThisFileDirectory)lib\**\*.*" /> </ItemGroup> <Exec Command="vc_redist.x86.exe /q /norestart" WorkingDirectory="$(FxCopFolder)" />Picturize
Can't get it working after following the steps above. I'm not using the FxCop msbuild target, but directly calling the FxCopCmd.exe. 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. Any ideas?Vani
Found it, installing vc++ but it had to be the x86 version, because the Microsoft.VisualStudio.CodeAnalysis.Interop.dll is x86 only.Vani
@Vani Thanks. I improved the wording to make it more explicit that the x86 is always needed.Sam
Note: the provided link that says "Visual C++ Redistributable for Visual Studio 2015 x86" automatically downloads the x64 version. You need to cancel the automatically download and click on "If your download does not start after 30 seconds, Click here", and select the x86 version.Cleanlimbed
A
8

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:

  1. Install Microsoft Build Tools 2015 on the build machine.

  2. Install Microsoft Visual C++ 2015 Redistributable Update 3, both x86 and x64, on the build machine.

  3. Import the following registry file (update drive letter if not C:) on the build machine:


Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\WDExpress\14.0\Setup\EDev]
"StanDir"="C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Team Tools\\Static Analysis Tools\\"
"FxCopDir"="C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Team Tools\\Static Analysis Tools\\FxCop\\"
  1. 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.

  2. Copy %programfiles(x86)%\MSBuild\Microsoft\VisualStudio\v14.0\CodeAnalysis to the same place on the build machine.

  3. 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 and Microsoft.VisualStudio.CodeAnalysis.Sdk.dll (Make sure the DLLs are version 14.0!). Either copy to C:\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... Copy vs_profiler_x64_enu.exe from Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Performance Tools\Setups and install it on the build machine.

Deviations from @BatteryBackupUnit's instructions:

  1. The entire Static Analysis Tools directory is needed, not just the FxCop subdirectory.
  2. Instead of nesting a Msbuild subfolder under FxCop, you need to place it in the original location.
  3. Copying msdia120.dll and amd64\msdia140.dll files into the FxCop directory seems unnecessary so long as the Visual C++ 2015 Redistributable is installed.
  4. No FxCop-related modifications to the project file (*.csproj).
  5. No Custom.CodeAnalysis.targets file.
Agler answered 9/6, 2016 at 8:12 Comment(2)
This approach worked for me, with one change. Initially, code analysis was reporting errors: "MSBUILD CA0001 : Exception of type 'Phx.FatalError' was thrown." According to this answer, this was due to msdia140.dll not being registered - apparently it has not been installed by VC++ 2015 Redist. Copying the DLL from VS2015 installation (I used "C:\Program Files (x86)\Microsoft Visual Studio 14.0\DIA SDK\bin") and registering it with regsvr32 has fixed the problem.Disarmament
This worked well for me, but I had to copy "%programfiles(x86)%\Microsoft Visual Studio 14.0\Team Tools\Static Analysis" (the 14.0 version not 12.0). I also had the issue with 'Phx.FatalError' caused by by missing msdia140.dllSinuosity
S
3

Run FxCop 15.0 without installing Visual Studio 2017

Prerequisites:

  • MSBuild 15.0 --> Install Microsoft Build Tools 2017, you might also want to see here
  • Visual C++ Redistributable for Visual Studio 2017 x86 or here OR Visual Studio 2017 with C++. ---- Note: the x86 version is always required, depending on the build x64 redist may be required as well. If it is missing the error message can be cryptic, for example 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:

  • appropriate Windows SDK, for example Windows 10 SDK
  • appropriate .net SDK / targets (.NET Framework 4.6 SDK is included in Windows 10 SDK)

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)

(source control)\tools\FxCop15
│
├[Engines]
│   │
│   ├IntrospectionAnalysisEngine.dll
│   └PhoenixAnalysisEngine.dll
├[Msbuild]
│   │
│   ├fxcoptask.dll
│   ├Microsoft.CodeAnalysis.Targets
│   ├Microsoft.VisualStudio.CodeAnalysis.dll
│   └Microsoft.VisualStudio.CodeAnalysis.Sdk.dll
├[Repository]
│   │
│   ├[Compatibility]
│   │   │
│   │   ├Desktop2.0.xml
│   │   ├Desktop2.0SP1.xml
│   │   ├Desktop2.0SP2.xml
│   │   ├Desktop3.0.xml
│   │   ├Desktop3.0SP1.xml
│   │   ├Desktop3.0SP2.xml
│   │   ├Desktop3.5.xml
│   │   └Desktop3.5SP1.xml
│   └system32.bin
├[Rules]
│   │
│   ├DataflowRules.dll
│   ├DesignRules.dll
│   ├GlobalizationRules.dll
│   ├InteroperabilityRules.dll
│   ├MaintainabilityRules.dll
│   ├MobilityRules.dll
│   ├NamingRules.dll
│   ├PerformanceRules.dll
│   ├PortabilityRules.dll
│   ├ReliabilityRules.dll
│   ├SecurityRules.dll
│   ├SecurityTransparencyRules.dll
│   └UsageRules.dll
├[x64]
│   │
│   └msdia140.dll (1349 KB)
├[Xml]
│   │
│   ├CodeAnalysisReport.xsl
│   ├FxCopReport.xsl
│   └VSConsoleOutput.xsl
├Architecture-msil.dll
├CodeAnalysis.dll
├CustomDictionary.xml
├FxCopCmd.exe
├FxCopCmd.exe.config
├FxCopCommon.dll
├FxCopSdk.dll
├Microsoft.Cci.dll
├Microsoft.VisualStudio.CodeAnalysis.Common.dll
├Microsoft.VisualStudio.CodeAnalysis.DataflowModels.dll
├Microsoft.VisualStudio.CodeAnalysis.dll
├Microsoft.VisualStudio.CodeAnalysis.Interop.dll
├Microsoft.VisualStudio.CodeAnalysis.Phoenix.dll
├Microsoft.VisualStudio.CodeAnalysis.Phoenix.xml
├msdia140.dll (1057 KB)
├mssp7en.dll
├mssp7en.lex
├phx.dll
└Runtime-vccrt-win-msil.dll

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 to

    Msbuild\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:

<!-- Microsoft.CSharp.targets import is contained in csproj by default. This just goes to show the sequence -->
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>

<!-- now this must be added -->
<Import Project="$(ProjectBuildScriptDir)Custom.CodeAnalysis.targets"/>

And this is what our Custom.CodeAnalysis.targets contains:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <!-- Code analysis settings. -->
    <PropertyGroup>
        <!-- this must reference the path where you copied the FxCop stuff to -->
        <FxCopDir>..\FxCop15\</FxCopDir> 
        <CodeAnalysisCulture>en-US</CodeAnalysisCulture>
        <CodeAnalysisRuleSet>$(SolutionDir)FxCop.ruleset</CodeAnalysisRuleSet>
        <!-- you can and should use another condition here. Otherwise code analysis will be run on every build in VS as well. -->
        <!-- in my build setup i do something like Condition=IsRunningOnTeamCity => true -->
        <RunCodeAnalysis>true</RunCodeAnalysis> 
        <CodeAnalysisTreatWarningsAsErrors Condition="'$(IsRunningOnTeamCity)' != 'true'">true</CodeAnalysisTreatWarningsAsErrors>
    </PropertyGroup>

    <Import Project="$(FxCopDir)Msbuild\Microsoft.CodeAnalysis.Targets" />

    <Target Name="CodeAnalysisLogHeader" BeforeTargets="RunCodeAnalysis" Condition="$(RunCodeAnalysis) == 'true' OR $(RunCodeAnalysisOnce) == 'true'">
        <Message Text="Text, Executing Code Analysis (FxCop) on $(MsBuildProjectName)" Importance="High" />
    </Target>

</Project>

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 :-)

Sam answered 5/3, 2018 at 16:45 Comment(0)
K
1

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).

Kenway answered 29/7, 2014 at 16:50 Comment(3)
just yesterday i've had a look at the Microsoft.CodeAnalysis.targets file and saw that it was using the $(FxCopDir) variable. I think if you're not running TFS you'll at least need to import this targets file in the projects. Execution of Code-Analysis is then controlled by the property RunCodeAnalysis and done as a "part" (not really, after target) of the Build target.Sam
come to think of it, doesn't TFS support Static Code Analysis (aka FxCop) out of the box?Sam
I'm not sure. I just wanted anyone reading this to know, that contrary to what I've been reading in a couple of places that FxCop is no longer available as a separate tool, and one must install vs 2013, is just not true.Kenway

© 2022 - 2024 — McMap. All rights reserved.