Debugging my published Roslyn source generator’s NuGet package

I recently rewrote Moxy – a Roslyn source generator that allows mix-ins in C#.

My new version had an odd problem. When my consuming product was built I would get a NullReferenceException, which was useless to me because it didn’t come with a callstack. However, when I switched out the NuGet package and referenced Moxy directly I wouldn’t get the problem.

Seeing the problem only occurred during a dotnet build I tried adding the following code to the Moxy source.

if (!System.Diagnostics.Debugger.IsAttached)
	System.Diagnostics.Debugger.Launch();

I was disappointed to see this didn’t work, but was able to fix it by creating a .reg file with the following contents and importing it using regedit. [Credit – Answered by Implex1, April 2007].

Windows Registry Editor Version 5.00
 
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Notify]
 
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Notify\SensLogn]
"DLLName"="WlNotify.dll"
"Lock"="SensLockEvent"
"Logon"="SensLogonEvent"
"Logoff"="SensLogoffEvent"
"Safe"=dword:00000001
"MaxWait"=dword:00000258
"StartScreenSaver"="SensStartScreenSaverEvent"
"StopScreenSaver"="SensStopScreenSaverEvent"
"Startup"="SensStartupEvent"
"Shutdown"="SensShutdownEvent"
"StartShell"="SensStartShellEvent"
"PostShell"="SensPostShellEvent"
"Disconnect"="SensDisconnectEvent"
"Reconnect"="SensReconnectEvent"
"Unlock"="SensUnlockEvent"
"Impersonate"=dword:00000001
"Asynchronous"=dword:00000001

My debugger window was now opening whenever I did a dotnet build but, unfortunately, I still wasn’t seeing the bug. So next I wanted to release a debug version of my package so I could consume that instead, and then debug when it failed.

Next I right-clicked my csproj file and went to the Properties menu.

  1. I selected the Release configuration at the top of Visual Studio.
  2. I opened up Build->General.
  3. Under Optimize code I unticked the Release option.
  4. Under Debug symbols I selected Embedded in DLL/EXE, portable across all platforms.

After I published the package and then consumed it, my dotnet build process triggered my debugger, and the bug was fixed in a matter of minutes!

As I said earlier, I am not entirely sure which of these steps were completely necessary, perhaps only the Debugger.Launch() code and the .reg file were necessary.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *