about · lately · live · notes · talks · writing · contact · thanks · support ·


Debugging a Fody Weaver (Plugin) and/or debugging MSBuild

Last night at an ungodly hour I did a massive refactor on a prototype codebase with no unit tests and now the once working project now no longer compiles because something I did is causing the most excellent Fody to blow up. Normally you would just go and do a bisect using git but in this case I didn’t incrementally commit on each change as I was on my own branch and the work was all related to a single higher unit of work. Old habits die hard, sometimes they come back to bite you hard…

So how exactly to you get yourself out of this situation? Your application won’t launch/debug because it won’t compile, hmm pickle…..

It turns out back when .NET 4.0 landed Microsoft added an unofficial /debug flag:

# cd C:\Windows\Microsoft.NET\Framework\v4.0.30319>
# msbuild /debug

Microsoft (R) Build Engine version 4.6.1038.0
[Microsoft .NET Framework, version 4.0.30319.42000]
Copyright (C) Microsoft Corporation. All rights reserved.

MSBUILD : error MSB1001: Unknown switch.
Switch: /debug

But it won’t work until you enable it from an elevated (admin) command prompt:

# reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSBuild\14.0" /v DebuggerEnabled /d true
# reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSBuild\12.0" /v DebuggerEnabled /d true
# reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSBuild\4.0" /v DebuggerEnabled /d true
# reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSBuild\3.5" /v DebuggerEnabled /d true
# reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\14.0" /v DebuggerEnabled /d true
# reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\12.0" /v DebuggerEnabled /d true
# reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\4.0" /v DebuggerEnabled /d true
# reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\3.5" /v DebuggerEnabled /d true

The next step is to debug the actual code that is being weaved, in my case the project was a portable class library whereby all dependancies came from NuGet so this process was super simple:

The next steps from here is to invoke msbuild manually and wait for your build to fail:

# cd C:\Windows\Microsoft.NET\Framework\v4.0.30319>
# msbuild.exe /debug C:\...\ghuntley-reactiveui.fody\reactiveuifody.sln

Attach debugger to MSBuild

From this point, the workflow is pretty much same/same:

Now we are cooking with gas!

Rewind the stack and inspect the previous variables to discover the offending attribute/viewmodel.

Offending ViewModel/Attribute found!

Remember kids, commits are cheap and git rebase exists - use it.