1

I created an inline MSBuild task (as described here), and I want to know if there's a way to debug it in Visual Studio?

I tried adding a call to Debugger.Launch(), which did indeed launch the debugger, but Visual Studio couldn't find a source file that corresponds to the c# code from the inline task.

The only thing I can think of now is to put the code into a real assembly, rather than having it be inline.

RobSiklos
  • 8,348
  • 5
  • 47
  • 77

1 Answers1

2

There is a way, but it's not really pretty nor suitable for using regularly. But it does work:

  • create an environment variable MSBUILDLOGCODETASKFACTORYOUTPUT and set it to 1 (actual value doesn't matter). This will dump the generated code which was also used for compilation into a text file with random guid as name and in the directory found by Path.GetTempPath()
  • run msbuild and because of Debugger.Launch() you get the dialog for selecting a VS instance; select the one you want
  • VS now shows a file dialog asking for the source file. Unfortunately it doesn't accept just any file but it insists on the one compiled
  • so browse to the temp folder, rename the most recent txt file (should be the source) to the name the dialog asks for
  • select the file and clik 'Ok' in the file dialog in VS, and it will show the debugger halted at the correct line in that file

Slightly shorter would be to put your source code in a .cs file already and use <Code Type="Class" Language="cs" Source="path\to\inline.cs"> instead of a fragment: you can omit setting MSBUILDLOGCODETASKFACTORYOUTPUT since you have the file used already, but you still need to rename it.

So if you need this often using an assmbly wil be shorter. Convenient way for achieving this in my answer here.

Btw I found most of this information this by looking at the CodeTaskFactory source. It can be seen it uses CompileAssemblyFromSource. So if you'd make a small modification to the source to instead always dump the source code to a file, then use CompileAssemblyFromFile instead, it would work out of the box..

Community
  • 1
  • 1
stijn
  • 34,664
  • 13
  • 111
  • 163