{"id":1873,"date":"2010-07-08T16:52:55","date_gmt":"2010-07-08T16:52:55","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudio\/2010\/07\/08\/debugging-msbuild-script-with-visual-studio-2\/"},"modified":"2022-05-18T04:37:10","modified_gmt":"2022-05-18T11:37:10","slug":"debugging-msbuild-script-with-visual-studio-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/visualstudio\/debugging-msbuild-script-with-visual-studio-2\/","title":{"rendered":"Debugging MSBuild script with Visual Studio (2)"},"content":{"rendered":"<p>In my <a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/debugging-msbuild-script-with-visual-studio\/\" target=\"_blank\" rel=\"noopener\">previous post<\/a>, I showed how to enable the hidden Visual Studio debugger for MSBuild script, and demonstrated it by stepping through the evaluation of a C# project. In this post, I&#8217;ll keep debugging into the actual build of that project.<\/p>\n<p>Note that this blog is rather narrow, so some of the screenshots may be hard to see \u2013 you can click on them to see the full size version.<\/p>\n<p>Starting from where we left off last post, I&#8217;ll set a breakpoint in Microsoft.CSharp.targets on the &lt;Csc&gt; task tag. That&#8217;s where the compiler will run. Then hit F5 to run to it.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2010\/07\/3386.image_thumb_26CFAD92.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2010\/07\/3386.image_thumb_26CFAD92.png\" width=\"681\" height=\"371\" \/><\/a>&#160; <\/p>\n<p>Ideally, I&#8217;d be able to set a breakpoint on the enclosing &lt;Target&gt; tag, but unfortunately there&#8217;s a bug: you can&#8217;t. As a workaround you could inspect the values of a target&#8217;s attributes when you get to the first tag in the body of that target. If the target&#8217;s condition is false, or if its inputs and outputs are up to date so that it skips, it&#8217;s not so simple. You&#8217;d have to work around this by stepping up to the target before. <\/p>\n<p>I&#8217;d like to be able to evaluate the condition on the task there, but because it&#8217;s <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms171473.aspx\" target=\"_blank\" rel=\"noopener\">batchable<\/a> it could have multiple results: the EvaluateCondition delegate I used before won&#8217;t accept it. If this was a parameter on the task, I&#8217;d probably step into the task&#8217;s code itself to see the value. Since it&#8217;s a condition, I&#8217;d probably look through the metadata on the item list directly, or query the object model in some way.<\/p>\n<p>Something like the value of Sources is easy to evaluate here, though:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2010\/07\/2605.image_thumb_00D9FD3C.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2010\/07\/2605.image_thumb_00D9FD3C.png\" width=\"691\" height=\"147\" \/><\/a> <\/p>\n<p>Now I want to step into the task implementation.<\/p>\n<p><p>You might think that at this point, you can simply Step In (F11). However, you can&#8217;t \u2013 it happens that MSBuild will run this task on a different thread. To know to jump threads properly here, the debugger has to support what they call &quot;causality&quot; for this kind of debugging, and since it doesn&#8217;t know anything about MSBuild, it doesn&#8217;t. <\/p>\n<p>It&#8217;s easy to get the job done though \u2013 set a breakpoint in the task and run until it&#8217;s hit.<\/p>\n<p>I have the source code for the Csc task, so I set a breakpoint here on the setter of the WarningLevel property, and did Continue (F5). I can see the task is getting &quot;4&quot; as the value of that property here. I can debug this code just like any other C# code, stepping through methods and so forth. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2010\/07\/6320.image_thumb_7688D8DB.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2010\/07\/6320.image_thumb_7688D8DB.png\" width=\"701\" height=\"425\" \/><\/a> <\/p>\n<\/p>\n<p>To get out to the XML, I&#8217;ll set a breakpoint in it and run \u2013 the same trick I used to get into the C#, but in reverse. Here I&#8217;m at the next tag after the task:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2010\/07\/4278.image_thumb_7EECAE32.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2010\/07\/4278.image_thumb_7EECAE32.png\" width=\"700\" height=\"486\" \/><\/a> <\/p>\n<p>I used Csc as an example here, but you&#8217;ll generally be debugging a custom task (or possibly, logger). Just make sure the assembly is not optimized: since you have Just-My-Code on, it won&#8217;t be debuggable otherwise. If you only have access to an optimized one, you can switch off Just-My-Code temporarily.<\/p>\n<p>There&#8217;s a CallTarget tag here: you can step into those, and like imports they&#8217;ll look like a new frame on the callstack \u2013 although unlike imports, that&#8217;s correct for their semantics.<\/p>\n<p>Probably the biggest limitation (bug) with the debugger right now is that you can&#8217;t see item and property changes made inside a target. For example, at this point @(_CoreCompileResourceInputs) should be empty because of the line above, but the immediate window tells me it isn&#8217;t:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2010\/07\/7002.image_thumb_71F2CE21.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2010\/07\/7002.image_thumb_71F2CE21.png\" width=\"721\" height=\"246\" \/><\/a> <\/p>\n<p>When you get past that target, you can see the changes.<\/p>\n<h3>Scenario 2: Debugging a build with multiple projects<\/h3>\n<p>Typically there&#8217;s more than one project in a build. I&#8217;ve added a project reference from this project to another. I&#8217;ve put a breakpoint at the top of that project, and run to it:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2010\/07\/0574.image_thumb_72CB340B.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2010\/07\/0574.image_thumb_72CB340B.png\" width=\"802\" height=\"386\" \/><\/a> <\/p>\n<p>The bottom of the callstack is the point in Microsoft.Common.targets where, early in the build of WindowsFormsApplication1, it invoked WindowsFormsApplication2.<\/p>\n<h5>In my next posts I&#8217;m going to cover<\/h5>\n<\/p>\n<ul>\n<li>Debugging a multiprocessor build<\/li>\n<li>Debugging the build of projects loaded into Visual Studio<\/li>\n<\/ul>\n<p>See you then! <\/p>\n<p>Dan <\/p>\n<p>Visual Studio Project &amp; Build Dev Lead<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my previous post, I showed how to enable the hidden Visual Studio debugger for MSBuild script, and demonstrated it by stepping through the evaluation of a C# project. In this post, I&#8217;ll keep debugging into the actual build of that project. Note that this blog is rather narrow, so some of the screenshots may [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":255385,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[155],"tags":[5,9,37],"class_list":["post-1873","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-visual-studio","tag-csharp","tag-debug","tag-msbuild"],"acf":[],"blog_post_summary":"<p>In my previous post, I showed how to enable the hidden Visual Studio debugger for MSBuild script, and demonstrated it by stepping through the evaluation of a C# project. In this post, I&#8217;ll keep debugging into the actual build of that project. Note that this blog is rather narrow, so some of the screenshots may [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/1873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/comments?post=1873"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/1873\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media\/255385"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media?parent=1873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/categories?post=1873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/tags?post=1873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}