Search code examples
msbuildvisual-studio-2015stylecop

How do I embed StyleCop in the solution?


I try to integrate StyleCop in a Visual Studio solution. Installing StyleCop on each machine of each developer is something I would prefer to avoid. The suggestion I've seen several times (example) is to include the binaries of StyleCop within the project, storing them in version control.

I did that. It works on my machine, but fails on a different machine where StyleCop is not installed. After uninstalling StyleCop on my machine, it doesn't work there either.

The error message is the following:

Severity Code Description Project File Line Error The "StyleCopTask" task could not be loaded from the assembly C:\Program Files (x86)\MSBuild..\StyleCop 4.7\StyleCop.dll. Could not load file or assembly 'file:///C:\Program Files (x86)\StyleCop 4.7\StyleCop.dll' or one of its dependencies. The system cannot find the file specified. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. Demo

This is what I included in every .csproj file:

<Import Project="$(SolutionDir)\externs\Microsoft.StyleCop\StyleCop.targets" />

The directory C:\demo\externs\Microsoft.StyleCop contains:

  • The copy of all the files from C:\Program Files (x86)\StyleCop 4.7,

  • The copy of C:\Program Files (x86)\MSBuild\StyleCop\v4.7\StyleCop.Targets.

What's wrong?


Solution

  • It appears that StyleCop.Targets contains an absolute path:

    <UsingTask
        AssemblyFile="$(MSBuildExtensionsPath)\..\StyleCop 4.7\StyleCop.dll"
        TaskName="StyleCopTask"/>
    

    In order to be able to use StyleCop on machines where the application is not installed, change this path to something similar to:

    <UsingTask
        AssemblyFile="$(SolutionDir)\externs\Microsoft.StyleCop\StyleCop.dll"
        TaskName="StyleCopTask"/>