TExcelTAD
Home › .NET Builds: MSBuild & NuGet › Directory.Build.props & .targets

Directory.Build.props & .targets

Copying <Nullable>enable</Nullable> and <TreatWarningsAsErrors>true</TreatWarningsAsErrors> into forty project files works until someone forgets the forty-first. MSBuild has a built-in answer: before evaluating a project, the SDK walks up the folder tree from the project and imports the first Directory.Build.props it finds; near the end it does the same for Directory.Build.targets. Put one at the repository root and every project below it shares its settings.

One file, every project

bash · try it yourself
mkdir repo && cd repo
cat > Directory.Build.props <<'XML'
<Project>
  <PropertyGroup>
    <Nullable>enable</Nullable>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <Company>Ada Industries</Company>
    <ArtifactsPath>$(MSBuildThisFileDirectory)artifacts</ArtifactsPath>
  </PropertyGroup>
</Project>
XML
mkdir -p src/Core src/Cli
cat > src/Core/Core.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
</Project>
XML
cat > src/Cli/Cli.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <Nullable>disable</Nullable>
  </PropertyGroup>
</Project>
XML
echo 'public class Core { }' > src/Core/Core.cs
echo 'System.Console.WriteLine("cli");' > src/Cli/Program.cs
for p in src/Core src/Cli; do
  echo "$p: $(dotnet msbuild $p -getProperty:Nullable -getProperty:Company -getProperty:DirectoryBuildPropsPath | tr -d '\n' | tr -s ' ')"
done
dotnet build src/Cli -nologo -v:q > /dev/null
find artifacts -maxdepth 3 -type d | sort
src/Core: { "Properties": { "Nullable": "enable", "Company": "Ada Industries", "DirectoryBuildPropsPath": "/home/learner/repo/Directory.Build.props" }}
src/Cli: { "Properties": { "Nullable": "disable", "Company": "Ada Industries", "DirectoryBuildPropsPath": "/home/learner/repo/Directory.Build.props" }}
artifacts
artifacts/bin
artifacts/bin/Cli
artifacts/bin/Cli/debug
artifacts/obj
artifacts/obj/Cli
artifacts/obj/Cli/debug
  • Both projects got Company and TreatWarningsAsErrors without mentioning them, and DirectoryBuildPropsPath shows which file was imported.
  • Cli overrode Nullable. Directory.Build.props is imported inside Sdk.props, before the project body, so anything the project sets later wins. That makes the props file the right place for defaults.
  • $(MSBuildThisFileDirectory) is the folder of the file being evaluated (the repo root here), not of the project. Use it for every path in a shared file.
  • ArtifactsPath switches on the SDK's artifacts output layout: instead of bin/ and obj/ inside every project, all output goes to artifacts/bin/<project>/<pivot>/ and artifacts/obj/<project>/<pivot>/ (the pivot is the lower-cased configuration, plus the framework and runtime when a project has several), under one folder that is easy to cache, clean and ignore.

props or targets?

Directory.Build.targets is imported from Sdk.targets, after the project body. The difference matters whenever a shared setting depends on what the project itself says:

bash · try it yourself
mkdir repo && cd repo
cat > Directory.Build.props <<'XML'
<Project>
  <PropertyGroup>
    <SeenInProps>OutputType='$(OutputType)'</SeenInProps>
  </PropertyGroup>
</Project>
XML
cat > Directory.Build.targets <<'XML'
<Project>
  <PropertyGroup>
    <SeenInTargets>OutputType='$(OutputType)'</SeenInTargets>
    <!-- Only executables get a version banner -->
    <InformationalVersion Condition="'$(OutputType)' == 'Exe'">$(Version)+cli</InformationalVersion>
  </PropertyGroup>
  <Target Name="Describe" AfterTargets="Build">
    <Message Importance="high" Text="$(MSBuildProjectName): props saw $(SeenInProps), targets saw $(SeenInTargets)" />
  </Target>
</Project>
XML
mkdir app && cat > app/app.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
</Project>
XML
echo 'System.Console.WriteLine("app");' > app/Program.cs
dotnet build app -nologo | grep -E 'props saw'
dotnet msbuild app -getProperty:InformationalVersion
  app: props saw OutputType='', targets saw OutputType='Exe'
1.0.0+cli

The props file saw an empty OutputType (the project hadn't set it yet; the SDK's default is applied later), while the targets file saw Exe. Rule of thumb:

File Imported Use it for
Directory.Build.props early, inside Sdk.props defaults projects may override: Nullable, LangVersion, TreatWarningsAsErrors, package metadata, ArtifactsPath
Directory.Build.targets late, inside Sdk.targets custom targets, and settings that depend on the project's own values (OutputType, TargetFramework, IsPackable)
Directory.Packages.props from the SDK's NuGet props package versions for Central Package Management

dotnet new buildprops and dotnet new buildtargets create empty files of each kind.

Nested files: the search stops at the first match

MSBuild imports only the closest Directory.Build.props. A tests/Directory.Build.props replaces the root one for the projects under tests/, unless it imports its parent explicitly:

bash · try it yourself
mkdir repo && cd repo
cat > Directory.Build.props <<'XML'
<Project>
  <PropertyGroup>
    <Company>Ada Industries</Company>
  </PropertyGroup>
</Project>
XML
mkdir -p tests/Unit
cat > tests/Directory.Build.props <<'XML'
<Project>
  <Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
  <PropertyGroup>
    <IsPackable>false</IsPackable>
  </PropertyGroup>
</Project>
XML
cat > tests/Unit/Unit.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
</Project>
XML
dotnet msbuild tests/Unit -getProperty:Company -getProperty:IsPackable
sed -i '/<Import/d' tests/Directory.Build.props
echo "--- without the Import line"
dotnet msbuild tests/Unit -getProperty:Company -getProperty:IsPackable
{
  "Properties": {
    "Company": "Ada Industries",
    "IsPackable": "false"
  }
}
--- without the Import line
{
  "Properties": {
    "Company": "Unit",
    "IsPackable": "false"
  }
}

With the Import line, the test project got both the root's Company and its own folder's IsPackable=false. Without it, the root file was never read and Company fell back to the SDK default (the assembly name). GetPathOfFileAbove searches upward from the given folder; starting from ../ keeps it from finding the file itself.

Tip: Run dotnet msbuild -getProperty:DirectoryBuildPropsPath whenever a project "ignores" a shared setting. Nine times out of ten a nested file is shadowing the root one.

Test yourself

A project sets <Nullable>disable</Nullable> and the root Directory.Build.props sets enable. What does the project get?

  1. enable, the shared file wins
  2. disable: Directory.Build.props is imported before the project body, so the project's value comes later and wins
  3. An error about conflicting values
  4. Whichever file is newer

You want a shared setting that applies only to projects with OutputType Exe. Where does it belong?

  1. Directory.Build.props
  2. Directory.Build.targets, which is imported after the project body
  3. nuget.config
  4. The solution file

tests/Directory.Build.props exists, and so does /repo/Directory.Build.props. Which ones does tests/Unit/Unit.csproj import automatically?

  1. Both
  2. Only the closest one, tests/Directory.Build.props
  3. Only the root one
  4. Neither