Skip to content

Instantly share code, notes, and snippets.

@benvillalobos
Last active August 25, 2022 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benvillalobos/c901534892f3249246ccb03bd75ddf91 to your computer and use it in GitHub Desktop.
Save benvillalobos/c901534892f3249246ccb03bd75ddf91 to your computer and use it in GitHub Desktop.
Debugging An MSBuild Build

Debugging An MSBuild Build

MSBuild is open-source, which means you should be able to step through the execution of a build by following these steps.

Sync Up With Your Installed MSBuild

  1. Developer Command Prompt: msbuild --version You should see something like MSBuild version 17.3.1+362d18ee6 for .NET Framework
    • Everything after the + is the hash. Copy that
  2. git clone https://github.com/dotnet/msbuild
  3. git checkout 362d18ee6
  4. VS -> MSBuild.Dev.slnf
  5. VS -> Tools -> Options -> Disable Just My Code

Now you want to run your scenario.

  1. In a dev cmd prompt, navigate to your repro project
  2. Before you build it, type set MSBUILDDEBUGONSTART=1
    • 2 is an option that shows you the process ID to attach to
  3. Now reproduce your build failure
  4. When MSBuild launches you should see a prompt asking a debugger to attach
    • If debugonstart is 1, you will see a prompt that shows a list of available debuggers.
    • If debugonstart is 2, you will need to manually attach a debugger. In VS, it's Debug->Attach to proces.
    • If neither works, try relaunching VS and make sure nothing is running as admin.
  5. Before continuing the run, add a breakpoint at the function that you want to breakpoint into.
    • In VS you can go to the breakpoints window and add a new breakpoint.
      • To be safe, the format should be Namespace.Class.FunctionName, but FunctionName or Class.FunctionName also work.
    • You can also manually find the file and place a breakpoint.

If the breakpoint always fails to bind:

  • From an admin powershell, run Deploy-MSBuild.ps1 supplying `-Destination:"Path/To/VS Install/MSBuild/Current/Bin"
    • The script will make a Backup_... file in the directory.
    • This should make absolutely certain that the source code you're looking at for MSBuild matches the source code VS will be using

If it fails even after that... ¯_(ツ)_/¯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment