Execute different commands in MSBuild depending on platform

November 20, 2020 msbuild

Another quick snippet that shows how to change what command will be executed in MSBuild based on the OS the script is running on.

Read More

Filter ItemGroup by extension in MSBuild

November 04, 2020 msbuild

Just a quick snippet that shows how to filter an exiting ItemGroup by an extension.

Read More

Enabling the latest version of C#

June 21, 2018 c# msbuild

As of this writing when creating a new console project via dotnet new console the version of C# used in the project is version 7.0. This means you're missing out cool features like Default Literal Expressions. The language version can be changed via Visual Studio though I prefer to enable it via MSBuild. This can be done in the csproj file or globally via a Build.Directory.props file. Changing the C# language version is done via a property known as LangVersion.

<PropertyGroup>
    <LangVersion>latest</LangVersion>
</PropertyGroup>

This property can set to any of the values listed here. As of this writing VS Code doesn't seem to like it when you use 7.3 instead of latest. VS Code will show errors in your code although the code will compile without any problems.

Run a program directly after building in VS Code

Whenever I have a program that is just a generator of some kind I like to have that program execute directly after having built the program successfully. I'm going to show how to run the program via the dotnet cli but this trick can easily be applied to regular .NET programs or any program which is built using MSBuild.

Read More

Merging Build.Directory.props

April 04, 2018 msbuild .net

MSBuild version 15 introduced the concept of "Directory.Build.props" files. From the docs:

  • Directory.Build.props is a user-defined file that provides customizations to projects under a directory. This file is automatically imported from Microsoft.Common.props unless the property ImportDirectoryBuildTargets is set to false.

What is not stated here is that only one Directory.Build.props will be imported automatically. Imagine your project exists in the directory C:\repo\src\foo\foo.csproj and there exists a file in both C:\repo\src\Directory.Build.props and C:\repo\Directory.Build.props then only C:\repo\src\Directory.Build.props will be automatically included when building C:\repo\src\foo\foo.csproj. If you would like C:\repo\Directory.Build.props to be included as well, then C:\repo\src\Directory.Build.props will have to include C:\repo\Directory.Build.props like so:

<Project>
    <Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
</Project>

This is documented here.