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) > 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.
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) > 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:
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,
DEBUGandTRACEdefined, output inbin/Debug/. - Release:
Optimize=true,DEBUGgone, our ownFAST_PATHappended, thenRELEASEadded by the SDK. Note$(DefineConstants);FAST_PATH: append to the existing value instead of replacing it, or you'd loseTRACE(andDEBUGin Debug builds). The SDK appends more symbols just before compiling: the configuration name and the framework symbolsNET,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:
<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.
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: ''
GuardedTargetprinted nothing: its condition was checked whileStampwas stillunset, so the target and its dependencies were skipped.- In
GuardedTask,SetStampran first, so the task condition sawset. - The top-level
Seenitem 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'?
- Quotes make the comparison case-sensitive
- If the property is empty, an unquoted expression becomes a syntax error
- Quotes are required for numbers
- They force evaluation at execution time
Why write <DefineConstants>$(DefineConstants);FAST_PATH</DefineConstants> instead of <DefineConstants>FAST_PATH</DefineConstants>?
- Because DefineConstants must start with a semicolon
- To keep the symbols the SDK already defined (DEBUG/TRACE and others)
- It is faster
- To make it a global property
A top-level ItemGroup condition depends on a property that a target sets. What happens?
- The item appears after the target runs
- The condition only sees evaluation-time values, so the target's change has no effect on it
- MSBuild re-evaluates the project after each target
- It's a build error