This project is read-only.

Running StyleCop in VS2005 or VS Express

Out of the box, StyleCop provides seamless integration into Visual Studio 2008 and Visual Studio 2010, exposing new menu items which make it quick and easy to launch StyleCop in a variety of ways.

As of version 4.4, StyleCop no longer integrates into VS 2005 automatically. StyleCop also doesn’t integrate into any of the VS Express versions, since VS Express does not provide support for loading extension packages.

If you’re running one of these versions of VS, however, all is not lost! It is still possible to get StyleCop to run automatically whenever you build your solution, with the StyleCop violations showing up in the VS Error List window next. These StyleCop violations can be clicked in the normal way to jump directly to the offending code. This creates an integrated experience for StyleCop even when running in VS 2005 or VS Express.

To set this up, first make sure that you include the MSBuild integration files option when installing StyleCop, as shown below:

runninginvs2005_image1.jpg

This will cause the StyleCop binaries and MSBuild targets files to be installed under the %ProgramFiles%\MSBuild\Microsoft\StyleCop folder.

For example, after installing StyleCop 4.4, my machine has these files under the above location. Notice that these files are located under a subdirectory called v4.4.

C:\Program Files (x86)\MSBuild\Microsoft\StyleCop\v4.4>dir 
Volume in drive C has no label. 
Volume Serial Number is 6C43-221B

Directory of C:\Program Files (x86)\MSBuild\Microsoft\StyleCop\v4.4

06/17/2010  09:53 AM    <DIR>          . 
06/17/2010  09:53 AM    <DIR>          .. 
06/17/2010  09:53 AM    <DIR>          Docs 
05/20/2010  09:16 AM               306 Microsoft.SourceAnalysis.Targets 
06/16/2010  04:39 PM           231,288 Microsoft.StyleCop.CSharp.dll 
06/16/2010  04:39 PM           186,752 Microsoft.StyleCop.CSharp.Rules.dll 
06/16/2010  04:39 PM           255,848 Microsoft.StyleCop.dll 
06/16/2010  01:13 PM             6,133 Microsoft.StyleCop.Targets 
05/20/2010  09:16 AM               618 Settings.StyleCop 
06/16/2010  04:39 PM            20,336 StyleCopSettingsEditor.exe

The next step is to link to the StyleCop.Targets file from within your C# projects. This is done by manually editing the .csproj file for each project that you would like StyleCop to be integrated into.

Within the csproj file, find the import tag which imports the Microsoft.CSharp.targets file, and insert a new import to link to the StyleCop.Targets file just below this line:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(ProgramFiles)\MSBuild\Microsoft\StyleCop\v4.4\StyleCop.targets" /> 

Now open the project in Visual Studio. You’ll notice that each time you compile the project, StyleCop will automatically run and any StyleCop violations will be logged in the Error List window:

runninginvs2005_image2.jpg

You’ve just integrated StyleCop into whatever version of VS you happen to be running!

StyleCop Flags

The default StyleCop targets file defines a couple of variables which control the way that StyleCop runs. These variables can be set on the command line or within your project file.

StyleCopEnabled: This variable determines whether StyleCop will run at all. If this is set to false, StyleCop will not run.

StyleCopTreatErrorsAsWarnings: This variable is set to true by default. If set to false, StyleCop violations will appear as build errors rather than build warnings. This is useful if you want StyleCop issues to break the build.

StyleCopForceFullAnalysis: Setting this flag to true causes StyleCop to re-analyze every file without taking into account the results of previous runs. By default, StyleCop caches the results of previous runs and will only re-analyze a file if the file has changed.

SourceAnalysisCacheResults: Setting this flag to false prevents StyleCop from caching the results of each run. By default StyleCop will place a hidden StyleCop.Cache file in each folder. If you’d rather not have these files in your enlistment, set this flag.

Team Development

The solution shown above works great when you’re the sole developer on a project, but it becomes fragile when you’re working with a team of developers, especially since you’re hard-coding the StyleCop version into every csproj file. A better solution would be to copy the StyleCop MSBuild files locally into your source code enlistment and create a custom targets file for your team. This is usually simple to set up and will make it easier to manage your solution in the future as you upgrade to newer versions of the tool. Even if you’re working alone, you may find the setup below useful.

First, identify a location within your source enlistment where you’d like to place the StyleCop files. It is typical to have a Tools folder somewhere alongside your source code. If you don’t have one already, figure out an ideal location, and then copy the StyleCop files from %ProgramFiles%\MSBuild\Microsoft\StyleCop\v4.4 into this location.

For example:

copy %ProgramFiles%\MSBuild\Microsoft\StyleCop\v4.4\* c:\MyProject\Tools\StyleCop\v4.4

Next, create an environment variable in your enlistment which points to the StyleCop tools location. The way you create this environment variable will depend upon how your team’s enlistment is set up (what source control tools you’re using, etc). The only requirement is that your projects have knowledge of this flag. In my example, I’d want every project to have the following environment variable defined, for every developer in my team:

set STYLECOPTOOLS=c:\MyProject\Tools\StyleCop

Now we can create a simple custom targets file which wraps the default StyleCop targets. Create a new file in the Tools\StyleCop folder called StyleCop.Current.Targets, and place the following within the file:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="$(STYLECOPTOOLS)\v4.4\StyleCop.Targets" />
</Project>

This custom targets file simply links to the default StyleCop targets file. Finally, we can edit our csproj files to link to this new targets file rather than to the default StyleCop targets file:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(STYLECOPTOOLS)\StyleCop.Custom.targets" />

The advantage of this setup is that in the future if you upgrade to a newer version of StyleCop, or change the path where the StyleCop binaries are located, you only need to edit your StyleCop.Current.Targets file, and all of your projects will automatically pick up this change.

(Originally from Jason Allor's blog at http://devjitsu.com/blog/2010/06/17/running-stylecop-in-vs-2005-or-vs-express/

Last edited Mar 14, 2011 at 10:36 AM by andyr, version 15