Share via


StyleCop with TFS Build without Changing Build Template

How to setup TFS builds to validate stylecop code standards? One way to implement using alteration to TFS build templates. Below few posts give good explanation on how to do this with TFS build template modification
https://msdn.microsoft.com/en-us/magazine/dn451443.aspx
http://www.towfeek.se/2014/05/customize-your-tfs-build-process-to-run-stylecop/
http://tfsbuildextensions.codeplex.com/wikipage?title=How%20to%20integrate%20the%20extensions%20into%20a%20build%20template&referringTitle=Documentation
Is there a way to do this without build template alteration? Of course yes, thanks to StyleCop.MSBuild by Adam Ralph  this is possible now (Brief introduction on Stylecop.msbuild here).
Let’s have a look at step by step how to setup stylecop with TFS builds.
Right click on the Visual Studio solution Go to “Manage NuGet Packages for Solution”.
http://lh4.ggpht.com/-wKZEb_Jzo_U/VMUpTmM0pzI/AAAAAAAADu4/dViXNJh4jHc/1_thumb%25255B1%25255D.png?imgmax=800
Search for stylecop. Stylecop.MSBuild will be in the results.
http://lh3.ggpht.com/-jp02l8ZAio4/VMUpV7RS9hI/AAAAAAAADvI/QYl-Ysxb1tQ/2_thumb%25255B1%25255D.png?imgmax=800
Click on Install and it will download StyleCop.MSBuild.
http://lh3.ggpht.com/-zdukZI6yDSs/VMUpXhUH7dI/AAAAAAAADvY/8sRKM8OEGV0/3_thumb%25255B1%25255D.png?imgmax=800
Select available projects in the solution and click OK. If new projects added to solution they can be selected by going to “Manage NuGet Packages for Solution”. Click on Manage installed package and select the new projects available.
http://lh4.ggpht.com/-RpfEFOE3zXg/VMUpZjqQwgI/AAAAAAAADvo/lcTq0swA0M8/4_1_thumb%25255B5%25255D.png?imgmax=800
SyleCop.MSBuild will be installed for selected projects.
http://lh3.ggpht.com/-8xMFQViePQ8/VMUpboCj1NI/AAAAAAAADv4/z25jF0UUFdQ/5_thumb%25255B7%25255D.png?imgmax=800
This will add packages folder to the solution.
http://lh6.ggpht.com/-8So0j9tPPEs/VMUpdZk3aHI/AAAAAAAADwI/xTnZbn09Su0/6_thumb%25255B1%25255D.png?imgmax=800
http://lh3.ggpht.com/-aRPdy41H5e4/VMUpe1PCXTI/AAAAAAAADwY/pLwTyKAg998/7_thumb%25255B1%25255D.png?imgmax=800

Right click on packages folder and undo pending changes. Undo changes will not remove the package folder from local folder and will be available to for the solution in this machine.
http://lh5.ggpht.com/-khocuiWQbrs/VMUpgjMJWFI/AAAAAAAADwo/FBnYSOY0bXc/7_1_thumb%25255B2%25255D.png?imgmax=800
 http://lh3.ggpht.com/-bLLAtr8crpk/VMUphW8w5LI/AAAAAAAADww/DXWW1glDjh4/7_2_thumb%25255B1%25255D.png?imgmax=800
http://lh5.ggpht.com/-TGAUWPvuHhM/VMUpjHOX0aI/AAAAAAAADxA/AvgqfhU8qug/7_2_1_thumb%25255B3%25255D.png?imgmax=800
This will prevent packages folder and contents being checked in to TFS. It is possible to set this package to automatically downloaded when the solution built.
 http://lh3.ggpht.com/-3lgcoFwy83o/VMUpk5K2TqI/AAAAAAAADxQ/7eV2hIM7qi0/7_3_thumb%25255B2%25255D.png?imgmax=800
A file packages.config will be added to each project in the solution and make sure to check this in.
http://lh5.ggpht.com/-C63Ezv835c8/VMUpmdier_I/AAAAAAAADxg/txqi0rgsXcY/8_thumb%25255B1%25255D.png?imgmax=800
Project file (.csproj) will be modified to add required configurations for StyleCop.
http://lh3.ggpht.com/-CmL_2Mf9UZQ/VMUpoV6U7EI/AAAAAAAADxw/pHTb00hx4gk/9_thumb%25255B3%25255D.png?imgmax=800
Right click on the solution and enable nuget package restore for the solution. This will allow other team members to automatically download NuGet packages of the solution when it is built.
http://lh5.ggpht.com/-mf9GwF8eky8/VMXiP-rkIqI/AAAAAAAAD3I/ull2fGYikX4/7_6_thumb%25255B1%25255D.png?imgmax=800
This will add few files to solution and change the .csproj files.
http://lh4.ggpht.com/-taEbGzY1jOM/VMXiRlVExUI/AAAAAAAAD3Y/gjC6ZVRipGM/7_7_thumb%25255B1%25255D.png?imgmax=800
http://lh3.ggpht.com/-gx8jzgI-lA8/VMXiUiyrJ0I/AAAAAAAAD3o/kspitx1s7ig/7_8_thumb%25255B1%25255D.png?imgmax=800

When the project is built the StyleCop violations are shown.
http://lh4.ggpht.com/-fFmXzfAwm5c/VMUpqV45zYI/AAAAAAAADyA/a0t3njF10KU/10_thumb%25255B3%25255D.png?imgmax=800
After fixing all violations in code still three violations shown based on temporary files generated when building .Net framework 4.5 projects.
http://lh6.ggpht.com/-Q6AZXRM_aXk/VMUpr9kFX3I/AAAAAAAADyQ/jmO2U35ShHE/11_thumb%25255B2%25255D.png?imgmax=800
http://lh5.ggpht.com/-WpeqHN2BK_I/VMUpteY2qlI/AAAAAAAADyg/ZIGDNXk64Ms/12_thumb%25255B1%25255D.png?imgmax=800
Below link suggests few ways to fix this issue.
http://stackoverflow.com/questions/12405619/temporarygeneratedfile-guid-in-obj-debug-breaking-build
Out of them best solution is adding option to ignore the temporary files in the .csproj file. Since .csproj file will be checked in this fix will be available for other team members and build agents.
Unload project and edit to change the .csproj file.
http://lh3.ggpht.com/-RkjnPZvOjYs/VMUpvayoJEI/AAAAAAAADys/iOg32gdFYYs/13_thumb%25255B2%25255D.png?imgmax=800
http://lh6.ggpht.com/-cf_ioxmkp24/VMUpw4dVLcI/AAAAAAAADzA/Xq9SesbENzo/14_thumb%25255B1%25255D.png?imgmax=800
Add

<ItemGroup>
    <ExcludeFromStyleCop Include="$(IntermediateOutputPath)\**\*.cs" >
        <Visible>False</Visible>
    </ExcludeFromStyleCop>
</ItemGroup>

as shown below inside the StyleCop section added by the StyleCop.MSBuild installation.

http://lh6.ggpht.com/-2eoTUioRUHc/VMUpy6HdLAI/AAAAAAAADzQ/smT2ruUBXJc/15_thumb%25255B1%25255D.png?imgmax=800

Reload project.

http://lh3.ggpht.com/-E-KwXQ1gqdc/VMUp0jOfkAI/AAAAAAAADzg/xhps5j1zMlg/16_thumb%25255B1%25255D.png?imgmax=800

Now all violations are fixed.

http://lh5.ggpht.com/-w8Y12ElsFTs/VMUp2Eaz3XI/AAAAAAAADzw/rpHDqx0OSz4/17_thumb%25255B1%25255D.png?imgmax=800

To verify StyleCop violations still validated after the above fix for temporary files, purposefully create a violation and verify.

http://lh5.ggpht.com/-Ab-3VBey0ec/VMUp3p51M3I/AAAAAAAAD0A/SL_9hSAENzA/18_thumb%25255B1%25255D.png?imgmax=800 

http://lh5.ggpht.com/-I-9mRuVRNS0/VMUp5HsHnhI/AAAAAAAAD0Q/N93EOkhwBtk/19_thumb%25255B1%25255D.png?imgmax=800

Fix the violation and check in the solution to TFS. A CI build using default build template runs successfully.

http://lh6.ggpht.com/-maEjoJDrEfs/VMUp6_iRP2I/AAAAAAAAD0c/_m8cfoF7AjQ/20_thumb%25255B4%25255D.png?imgmax=800

To verify TFS build gives warning when there is a StyleCop violation purposefully create a violation and check in.

http://lh4.ggpht.com/-T2I9kihc9yw/VMUp8yZPLmI/AAAAAAAAD0w/CmBC73LLxNM/21_thumb%25255B1%25255D.png?imgmax=800

Local build in VS show violation.

http://lh6.ggpht.com/-OPyKN2yskSE/VMUp-yiV84I/AAAAAAAAD1A/cr2z91AXhGI/22_thumb%25255B1%25255D.png?imgmax=800

CI build shows SA1515 violation as a warning.

http://lh6.ggpht.com/-7ocx7spkrd8/VMUqAgOypHI/AAAAAAAAD1Q/9OvAxG8PfL8/23_thumb%25255B2%25255D.png?imgmax=800

To make this violation to fails the build, add below to .csproj StyeCop added property group.

<StyleCopTreatErrorsAsWarnings>
**    false
</StyleCopTreatErrorsAsWarnings>**
http://lh4.ggpht.com/-3oW7KnxTkYs/VMUqCbzpx-I/AAAAAAAAD1g/CVP3NR-yiAU/24_thumb%25255B1%25255D.png?imgmax=800

This will make the violation to fail the build locally.

http://lh5.ggpht.com/-k8zexiLi_ng/VMUqEaJK7bI/AAAAAAAAD1w/u80T0lhn0kk/25_thumb%25255B1%25255D.png?imgmax=800

CI Build also fails with StyleCop violation.

http://lh5.ggpht.com/-61mHfNWie0c/VMUqGunB9cI/AAAAAAAAD2A/XxjMRK2iEng/26_thumb%25255B2%25255D.png?imgmax=800

SuppressMessage can be added to code (Method or Class level) and works fine with local builds and TFS builds.

http://lh4.ggpht.com/-qO8FAl92XlE/VMUqKHP-yWI/AAAAAAAAD2Q/4mgBdgKG_wM/29_thumb%25255B1%25255D.png?imgmax=800

 http://lh4.ggpht.com/-Y1aCoUA7Jcw/VMUqLzsTjDI/AAAAAAAAD2g/9vMfUAEagP0/30_thumb%25255B1%25255D.png?imgmax=800

http://lh6.ggpht.com/-VzR-j2aiFgg/VMUqOavPONI/AAAAAAAAD2w/V6BT2Va4k44/31_thumb%25255B1%25255D.png?imgmax=800

Using StyleCop.MSBuild it is possible to run StyleCop with TFS builds without altering build templates and without installing StyleCop in TFS Build Agents.