{"id":17,"date":"2013-05-06T17:25:34","date_gmt":"2013-05-06T16:25:34","guid":{"rendered":"http:\/\/thisblessedplot.com\/wp\/?p=17"},"modified":"2013-05-06T17:25:34","modified_gmt":"2013-05-06T16:25:34","slug":"stylecop-and-the-msbuild-extension-pack","status":"publish","type":"post","link":"https:\/\/thedifficultbit.com\/?p=17","title":{"rendered":"StyleCop and the MSBuild Extension Pack"},"content":{"rendered":"<p>I use <a href=\"http:\/\/stylecop.codeplex.com\/\" title=\"StyleCop\">StyleCop<\/a> to check my C# code, running it both manually on particular projects or files from within Visual Studio as well as automatically for all code files as part of the build. The <a href=\"http:\/\/msbuildextensionpack.codeplex.com\/\" title=\"MSBuild Extension Pack\">MSBuild Extension Pack<\/a> provides a <a href=\"http:\/\/www.msbuildextensionpack.com\/help\/4.0.7.0\/html\/e2afb52c-e056-ed76-7503-c57fc3125f66.htm\" title=\"Task\">Task<\/a> to run StyleCop from MSBuild. The build file imports the MSBuild Extension Pack tasks and then uses the StyleCop task:<\/p>\n<pre>\n&lt;Project DefaultTargets=\"RunStyleCop\" xmlns=\"http:\/\/schemas.microsoft.com\/developer\/msbuild\/2003\" ToolsVersion=\"4.0\"&gt;\n   &lt;Import Project=\"$(ProgramFiles)MSBuildExtensionPack4.0MSBuild.ExtensionPack.tasks\"\/&gt;\n\n      &lt;!-- Run StyleCop --&gt;\n      &lt;Target Name=\"RunStyleCop\"&gt;\n         &lt;MSBuild.ExtensionPack.CodeQuality.StyleCop\n            remainder not shown ...\n<\/pre>\n<p>However as initially set up this does not work, returning the error: <code>MSB4062: The \"MSBuild.ExtensionPack.CodeQuality.StyleCop\" task could not be loaded from the assembly C:Program Files (x86)StyleCop 4.7MSBuild.ExtensionPack.StyleCop.dll. Could not load file or assembly 'file:\/\/\/C:Program Files (x86)StyleCop 4.7MSBuild.ExtensionPack.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.<\/code><\/p>\n<p>The imported tasks file <code>MSBuild.ExtensionPack.tasks<\/code> contains an entry for StyleCop <code>&lt;UsingTask AssemblyFile=\"$(MSBuildProgramFiles32)StyleCop 4.7MSBuild.ExtensionPack.StyleCop.dll\" TaskName=\"MSBuild.ExtensionPack.CodeQuality.StyleCop\"\/&gt;<\/code> but the <code>MSBuild.ExtensionPack.StyleCop.dll<\/code> file is not located in the <code>StyleCop 4.7<\/code> folder but is in fact in the same folder as the tasks file, <code>MSBuildExtensionPack4.0<\/code>.<\/p>\n<p>The obvious move is to change the StyleCop entry in <code>MSBuild.ExtensionPack.tasks<\/code> to point to the right location i.e. <code>&lt;UsingTask AssemblyFile=\"$(ExtensionTasksPath)MSBuild.ExtensionPack.StyleCop.dll\" TaskName=\"MSBuild.ExtensionPack.CodeQuality.StyleCop\"\/&gt;<\/code>. But this will not work and will return the error &#8220;<code>Failed to add file<\/code>&#8221; even though the file named is in fact present.<\/p>\n<p>After a little <a href=\"http:\/\/social.msdn.microsoft.com\/Forums\/en-US\/msbuild\/thread\/016e4856-ec53-4406-8897-29908d32e905\">searching<\/a> and experimentation it seems that the <code>StyleCop.*<\/code> DLLs need to be in the same directory as the <code>MSBuild.ExtensionPack.StyleCop.dll<\/code> for the StyleCop task to work.<\/p>\n<p>The easiest way to get the task working is not to modify the <code>MSBuild.ExtensionPack.tasks<\/code> StyleCop entry but instead copy the <code>MSBuild.ExtensionPack.StyleCop.dll<\/code> from the MSBuild Extension Pack folder to the <code>StyleCop 4.7<\/code> folder which contains the required DLLs.<\/p>\n<p>Using: MS Build Extension Pack 4.0.7.0; StyleCop 4.7.44.0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I use StyleCop to check my C# code, running it both manually on particular projects or files from within Visual Studio as well as automatically for all code files as part of the build. The MSBuild Extension Pack provides a Task to run StyleCop from MSBuild. The build file imports the MSBuild Extension Pack tasks [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3,4,5],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-code-analysis","tag-msbuild","tag-stylecop"],"_links":{"self":[{"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17"}],"version-history":[{"count":0,"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=\/wp\/v2\/posts\/17\/revisions"}],"wp:attachment":[{"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}