Skip to content

MSBuild Tips & Tricks

Kirill Osenkov edited this page Mar 3, 2017 · 21 revisions

#MSBuild.exe /pp MSBuild preprocessor. Pass /pp to the command line to create a single huge XML project file with all project imports inlined in the correct order. This is useful to investigate the ordering of evaluation and execution. Example:

msbuild MyProject.csproj /pp:inlined.proj

#MSBuild.exe /m Parallel build. Many people still don't know that they can significantly speed up their builds by passing /m to MSBuild.exe.

#MSBuild.exe /nr:false Disable node reuse (/nodeReuse:false). Don't leave MSBuild.exe processes hanging around locking files after the build completes. See more details in MSBuild command line help (/?). See also MSBUILDDISABLENODEREUSE=1 below.

#EnvironmentVariables

  • MSBUILDTARGETOUTPUTLOGGING=1 - set this to enable printing all target outputs to the log.
  • MSBUILDLOGTASKINPUTS=1 - log task inputs (not needed if there are any diagnostic loggers already).
  • MSBUILDEMITSOLUTION=1 - save the generated .proj file for the .sln that is used to build the solution.
  • MSBUILDENABLEALLPROPERTYFUNCTIONS=1 - enable additional property functions.
  • MSBUILDLOGVERBOSERARSEARCHRESULTS=1 - in ResolveAssemblyReference task, log verbose search results.
  • MSBUILDLOGCODETASKFACTORYOUTPUT=1 - dump generated code for task to a .txt file in the TEMP directory
  • MSBUILDDISABLENODEREUSE=1 - set this to not leave MSBuild processes behind (see /nr:false above, but the environment variable is useful to also set this for Visual Studio for example).
  • MSBUILDLOGASYNC=1 - enable asynchronous logging.

#TreatAsLocalProperty If MSBuild.exe is passed properties on the command line, such as /p:Platform=AnyCPU then this value overrides whatever assignments you have to that property inside property groups. For instance, <Platform>x86</Platform> will be ignored. To make sure your local assignment to properties overrides whatever they pass on the command line, add the following at the top of your MSBuild project file:

<Project TreatAsLocalProperty="Platform" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

This will make sure that your local assignments to the Platform property are respected. You can specify multiple properties in TreatAsLocalProperty separated by semicolon.

#Visual Studio Background Builds Set the TRACEDESIGNTIME=true environment variable to output design-time build logs to TEMP: read more here: https://blogs.msdn.microsoft.com/jeremykuhne/2016/06/06/vs-background-builds

#Extend all builds (at system-wide level) See https://www.simple-talk.com/dotnet/.net-tools/extending-msbuild, "Extending all builds" section. Also read about MSBuildUserExtensionsPath, CustomBeforeMicrosoftCommonProps, CustomBeforeMicrosoftCommonTargets, and CustomAfterMicrosoftCommonProps/CustomAfterMicrosoftCommonTargets.

Example: Create this file (Custom.props) in C:\Users\username\AppData\Local\Microsoft\MSBuild\14.0\Microsoft.Common.targets\ImportAfter:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <MyCustomProperty>Value!</MyCustomProperty>
  </PropertyGroup>
</Project>

then build any project. It will have MyCustomProperty set to Value!