{"id":733,"date":"2016-07-10T09:03:08","date_gmt":"2016-07-10T09:03:08","guid":{"rendered":"http:\/\/tpodolak.com\/blog\/?p=733"},"modified":"2016-07-10T09:03:08","modified_gmt":"2016-07-10T09:03:08","slug":"debugging-cake-scripts","status":"publish","type":"post","link":"https:\/\/tpodolak.com\/blog\/2016\/07\/10\/debugging-cake-scripts\/","title":{"rendered":"Debugging cake scripts"},"content":{"rendered":"<p>We&#8217;ve been using <a href=\"http:\/\/cakebuild.net\/\">Cake<\/a> in our project for quite some time and it has been working great. However for time to time lack of debugging support was really annoying for me. Fortunately it seems that these times are gone now. During <a href=\"https:\/\/vimeo.com\/171704581\"> NDC Oslo 2016<\/a> <a href=\"https:\/\/github.com\/gep13\">Gary Ewan Park<\/a> showed that it is possible to attach debugger to the build process. Here are the steps to achieve that. Let&#8217;s assume that we have following simple cake script.<\/p>\n<pre lang=\"csharp\">\r\nvar target = Argument(\"target\", \"Default\");\r\nTask(\"Default\")\r\n    .Does(() =>\r\n{\r\n    MSBuild(Directory(\"..\/\") + File(\"DebuggingCakeScript.sln\"));\r\n});\r\n\r\nRunTarget(target);\r\n<\/pre>\n<p>In order to make the debugging possible it is necessary to add<\/p>\n<pre lang=\"csharp\">\r\n#break\r\n<\/pre>\n<p>preprocessor directive to the task we want the debugger to stop. <\/p>\n<pre lang=\"csharp\">\r\nvar target = Argument(\"target\", \"Default\");\r\nTask(\"Default\")\r\n    .Does(() =>\r\n{\r\n    #break\r\n    MSBuild(Directory(\"..\/\") + File(\"DebuggingCakeScript.sln\"));\r\n});\r\n\r\nRunTarget(target);\r\n<\/pre>\n<p>This operation doesn&#8217;t affect the default build process, so if you run in console<\/p>\n<pre lang=\"bash\">\r\n.\\build.ps1 -target Default\r\n<\/pre>\n<p>the build will run as usual. In order to be able to attach the debugger we have to call <i>Cake<\/i> with <\/p>\n<pre lang=\"bash\">\r\n --debug\r\n<\/pre>\n<p>flag. You can&#8217;t do it directly via <\/p>\n<pre lang=\"bash\">\r\n\\build.ps1\r\n<\/pre>\n<p>script so assuming that you didn&#8217;t change directory structure created by bootstrapper, go to <i>Tools<\/i>\/<i>Cake<\/i> folder and run from <i>PowerShell<\/i><\/p>\n<pre lang=\"bash\">\r\n.\\Cake.exe ..\\..\\build.cake -target=Default --debug\r\n<\/pre>\n<p>where <\/p>\n<pre lang=\"bash\">\r\n..\\..\\build.cake\r\n<\/pre>\n<p>is path to your cake script and<\/p>\n<pre lang=\"bash\">\r\nDefault\r\n<\/pre>\n<p>is task you want to start.  Once you run the command, cake will wait for attaching the debugger<br \/>\n<a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2016\/07\/debugging-cake-scripts\/WaitingForDebugger.png\" rel=\"attachment wp-att-736\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2016\/07\/debugging-cake-scripts\/WaitingForDebugger.png\" alt=\"WaitingForDebugger\" width=\"800\" class=\"aligncenter size-full wp-image-800\" \/><\/a><br \/>\nNow launch <i>Visual Studio<\/i>, go to <i>Debug-> Attach to Process\u2026<\/i>, find process id listed in console<br \/>\n<a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2016\/07\/debugging-cake-scripts\/Attach.png\" rel=\"attachment wp-att-737\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2016\/07\/debugging-cake-scripts\/Attach.png\" alt=\"Attach\" width=\"800\" class=\"aligncenter size-full wp-image-800\" \/><\/a><br \/>\nand click <i>Attach<\/i>. After couple of seconds <i>Visual Studio<\/i> will load your cake script and you will be able to debug it like normal application.<br \/>\n<a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2016\/07\/debugging-cake-scripts\/attached.png\" rel=\"attachment wp-att-739\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2016\/07\/debugging-cake-scripts\/attached.png\" alt=\"attached\" width=\"800\" class=\"aligncenter size-full wp-image-800\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;ve been using Cake in our project for quite some time and it has been working great. However for time to time lack of debugging support was really annoying for me. Fortunately it seems that these times are gone now. During NDC Oslo 2016 Gary Ewan Park showed that it is possible to attach debugger [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[301],"tags":[302],"class_list":["post-733","post","type-post","status-publish","format-standard","hentry","category-cake","tag-cake"],"_links":{"self":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/comments?post=733"}],"version-history":[{"count":17,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/733\/revisions"}],"predecessor-version":[{"id":754,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/733\/revisions\/754"}],"wp:attachment":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/media?parent=733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/categories?post=733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/tags?post=733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}