TExcelTAD
Home › .NET Builds: MSBuild & NuGet › Conditions & Configurations

Conditions & Configurations

Almost every element in MSBuild can carry a Condition attribute: properties, property groups, items, item groups, imports, targets and individual tasks. If the condition is false, the element is skipped. Conditions are how one project file describes Debug and Release builds, Linux and Windows builds, and builds with and without optional features.

The condition language

Conditions are small boolean expressions over strings:

Expression Meaning
'$(Configuration)' == 'Release' string comparison, case-insensitive
'$(Feature)' != '' "is set"
A and B, A or B, !A, ( … ) logic
Exists('path') a file or folder exists
$(Count) &gt; 3 numeric comparison (escape > and < in XML)
$([MSBuild]::IsOSPlatform('Linux')) any property function returning true/false
'%(Item.Kind)' == 'font' metadata, inside targets (batched)

Always quote both sides: '$(X)' == 'y'. Without quotes an empty property leaves == y, which is a syntax error.

bash · try it yourself
mkdir cond && cd cond
touch feature.flag
cat > cond.proj <<'XML'
<Project>
  <PropertyGroup>
    <Configuration Condition="'$(Configuration)' == ''">Debug</Configuration>
    <IsRelease>false</IsRelease>
    <IsRelease Condition="'$(Configuration)' == 'RELEASE'">true</IsRelease>
    <Shards>4</Shards>
  </PropertyGroup>
  <ItemGroup>
    <Feature Include="flagged" Condition="Exists('feature.flag')" />
    <Feature Include="linux-only" Condition="$([MSBuild]::IsOSPlatform('Linux'))" />
    <Feature Include="windows-only" Condition="'$(OS)' == 'Windows_NT'" />
    <Feature Include="sharded" Condition="$(Shards) &gt; 2 and '$(IsRelease)' == 'true'" />
  </ItemGroup>
  <Target Name="Show">
    <Message Importance="high" Text="$(Configuration): IsRelease=$(IsRelease) OS=$(OS) features=@(Feature)" />
  </Target>
</Project>
XML
dotnet msbuild -nologo -v:m -t:Show
dotnet msbuild -nologo -v:m -t:Show -p:Configuration=Release
  Debug: IsRelease=false OS=Unix features=flagged;linux-only
  Release: IsRelease=true OS=Unix features=flagged;linux-only;sharded

'Release' == 'RELEASE' was true: comparisons ignore case. $(OS) is Unix on Linux and macOS and Windows_NT on Windows; for finer checks use IsOSPlatform.

What Debug and Release really change

Configuration is just a property. The SDK reads it and sets other properties accordingly:

bash · try it yourself
mkdir cfg && cd cfg
cat > cfg.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)' == 'Release'">
    <DefineConstants>$(DefineConstants);FAST_PATH</DefineConstants>
  </PropertyGroup>
</Project>
XML
cat > Program.cs <<'CS'
#if DEBUG
System.Console.WriteLine("debug build: extra checks on");
#endif
#if FAST_PATH
System.Console.WriteLine("release build: fast path on");
#endif
CS
for c in Debug Release Staging; do
  dotnet msbuild -getProperty:Optimize -getProperty:DefineConstants -getProperty:OutputPath -p:Configuration=$c
done
dotnet run -c Debug
dotnet run -c Release
{
  "Properties": {
    "Optimize": "false",
    "DefineConstants": "TRACE;DEBUG",
    "OutputPath": "bin\\Debug/net10.0/"
  }
}
{
  "Properties": {
    "Optimize": "true",
    "DefineConstants": "TRACE;FAST_PATH;RELEASE",
    "OutputPath": "bin\\Release/net10.0/"
  }
}
{
  "Properties": {
    "Optimize": "",
    "DefineConstants": "TRACE;STAGING",
    "OutputPath": "bin\\Staging/net10.0/"
  }
}
debug build: extra checks on
release build: fast path on
  • Debug: no optimisation, DEBUG and TRACE defined, output in bin/Debug/.
  • Release: Optimize=true, DEBUG gone, our own FAST_PATH appended, then RELEASE added by the SDK. Note $(DefineConstants);FAST_PATH: append to the existing value instead of replacing it, or you'd lose TRACE (and DEBUG in Debug builds). The SDK appends more symbols just before compiling: the configuration name and the framework symbols NET, NET10_0, NET10_0_OR_GREATER…

A custom configuration gets neither set of defaults. Staging above has an empty Optimize (so the compiler doesn't optimise) and no DEBUG; the SDK added only the upper-cased configuration name (STAGING, like RELEASE for Release, appended by Sdk.targets after your project body). If Staging should behave like Release, say so explicitly: <Optimize Condition="'$(Configuration)' == 'Staging'">true</Optimize>. (The odd bin\Debug/ in OutputPath, printed JSON-escaped as bin\\Debug/, is the SDK's raw string; MSBuild normalises separators when it uses the path.)

Choose / When / Otherwise

For multi-way decisions there's a switch-like construct. It can contain property groups and item groups:

xml
<Choose>
  <When Condition="'$(TargetDatabase)' == 'postgres'">
    <ItemGroup>
      <Compile Include="Data/Postgres/**/*.cs" />
    </ItemGroup>
  </When>
  <When Condition="'$(TargetDatabase)' == 'sqlite'">
    <ItemGroup>
      <Compile Include="Data/Sqlite/**/*.cs" />
    </ItemGroup>
  </When>
  <Otherwise>
    <PropertyGroup>
      <TargetDatabase>inmemory</TargetDatabase>
    </PropertyGroup>
  </Otherwise>
</Choose>

Choose is evaluated in the property and item passes like everything else; Choose blocks can't appear inside targets.

Conditions on targets and tasks

Conditions inside targets are evaluated during execution, so they can see properties that earlier targets set. But there's a trap: a condition on a <Target> is checked before its DependsOnTargets run. A condition on a task is checked when the task is reached.

bash · try it yourself
mkdir late && cd late
cat > late.proj <<'XML'
<Project>
  <PropertyGroup>
    <Stamp>unset</Stamp>
  </PropertyGroup>
  <ItemGroup>
    <Seen Include="yes" Condition="'$(Stamp)' == 'set'" />
  </ItemGroup>
  <Target Name="SetStamp">
    <PropertyGroup>
      <Stamp>set</Stamp>
    </PropertyGroup>
  </Target>
  <Target Name="GuardedTarget" DependsOnTargets="SetStamp" Condition="'$(Stamp)' == 'set'">
    <Message Importance="high" Text="GuardedTarget ran" />
  </Target>
  <Target Name="GuardedTask" DependsOnTargets="SetStamp">
    <Message Importance="high" Text="task condition saw Stamp=$(Stamp)" Condition="'$(Stamp)' == 'set'" />
    <Message Importance="high" Text="evaluation-time item Seen: '@(Seen)'" />
  </Target>
</Project>
XML
echo "--- -t:GuardedTarget"
dotnet msbuild -nologo -v:m -t:GuardedTarget
echo "--- -t:GuardedTask"
dotnet msbuild -nologo -v:m -t:GuardedTask
--- -t:GuardedTarget
--- -t:GuardedTask
  task condition saw Stamp=set
  evaluation-time item Seen: ''
  • GuardedTarget printed nothing: its condition was checked while Stamp was still unset, so the target and its dependencies were skipped.
  • In GuardedTask, SetStamp ran first, so the task condition saw set.
  • The top-level Seen item stayed empty: evaluation was over long before any target ran.

When a condition "doesn't work", first ask when it is evaluated: during evaluation, before a target's dependencies, or when a task is reached.

Test yourself

Why should conditions quote both sides, as in '$(Flavor)' == 'fast'?

  1. Quotes make the comparison case-sensitive
  2. If the property is empty, an unquoted expression becomes a syntax error
  3. Quotes are required for numbers
  4. They force evaluation at execution time

Why write <DefineConstants>$(DefineConstants);FAST_PATH</DefineConstants> instead of <DefineConstants>FAST_PATH</DefineConstants>?

  1. Because DefineConstants must start with a semicolon
  2. To keep the symbols the SDK already defined (DEBUG/TRACE and others)
  3. It is faster
  4. To make it a global property

A top-level ItemGroup condition depends on a property that a target sets. What happens?

  1. The item appears after the target runs
  2. The condition only sees evaluation-time values, so the target's change has no effect on it
  3. MSBuild re-evaluates the project after each target
  4. It's a build error