FxCop Fails Only on Build Server

I recently ran into a strange issue where the build worked just fine on a developer's machine but failed when submitting to a TFS build machine as part of gated check-in. The build error looked something like this:

       RunCodeAnalysis:
         Running Code Analysis...
         C:\Program Files (x86)\Microsoft Visual Studio 11.0\Team Tools\Static Analysis Tools\FxCop\FxCopCmd.exe  /outputCulture:1033 ...
         * 1 total analysis engine exceptions.
         * 39 total rule exceptions.
     2>MSBUILD : error : CA0001 : The following error was encountered while reading module 'SomeLibrary': 
        Could not resolve member reference: [SomeReferencedLibrary, Version=1.1.0.0, Culture=neutral, 
        PublicKeyToken=null]SomeReferencedLibrary.SomeClass::SomeMethod. 
        [D:\Builds\1234\Main-Gated\src\SomeLibrary\SomeLibrary.csproj]
     2>MSBUILD : error : CA0001 : Rule=Microsoft.Reliability#CA2001, Target=SomeLibrary.WebApp1.#Application_Start() : 
        The following error was encountered while reading module 'SomeLibrary': Could not resolve member reference: 
        [SomeReferencedLibrary, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null]SomeReferencedLibrary.SomeClass::SomeMethod. 
        [D:\Builds\1234\Main-Gated\src\SomeLibrary\SomeLibrary.csproj]
     2>MSBUILD : error : CA0001 : Rule=Microsoft.Maintainability#CA1506, 
        Target=SomeLibrary.WebApiApplication : The following error was encountered while reading module 'SomeLibrary': 
        Could not resolve member reference: [SomeReferencedLibrary, Version=1.1.0.0, Culture=neutral, 
        PublicKeyToken=null]SomeReferencedLibrary.SomeClass::SomeMethod. 
        [D:\Builds\1234\Main-Gated\src\SomeLibrary\SomeLibrary.csproj]

The library FxCop is referring to, SomeReferencedLibrary version 1.1, is definitely in the correct directory, as are all of its referenced dependencies. After some investigation of those references I found that it was referencing an older version of a library than the one in the build folder. There are binding redirects in the config file that handle this for the application itself. It could be a difference in the FxCop versions between dev and build machine but for some reason the config file's binding redirects were being ignored. Rather than track down and change FxCop.exe.config on the build machine, which would be a real mess, I found the situation was easily resolved by adding the following to the csproj files:

<CodeAnalysisAdditionalOptions>/assemblyCompareMode:StrongNameIgnoringVersion</CodeAnalysisAdditionalOptions>
comments powered by Disqus