TExcelTAD
Home › .NET Builds: MSBuild & NuGet › SDK-Style Project Files

SDK-Style Project Files

Open a .csproj from 2015 and you'll find hundreds of lines: every source file listed by hand, <Import>s of long paths, GUIDs, and a packages.config next to it. Open one made by dotnet new today and it's about eight lines. Both are MSBuild projects. The difference is the Sdk attribute, and understanding what it hides is the first step to reading any .NET build.

What Sdk="Microsoft.NET.Sdk" means

An MSBuild SDK is a package of .props and .targets files. Writing

xml
<Project Sdk="Microsoft.NET.Sdk">
  ...
</Project>

is shorthand for importing Sdk.props at the very top of the project and Sdk.targets at the very bottom. The two are equivalent, and you can write the long form yourself:

bash · try it yourself
mkdir longform && cd longform
cat > longform.csproj <<'XML'
<Project>
  <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
  <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>
XML
echo 'System.Console.WriteLine("explicit imports work too");' > Program.cs
dotnet run
explicit imports work too

The order matters and explains a rule you'll use constantly: Sdk.props runs before your settings, Sdk.targets after them. The props file sets defaults you can override (Configuration, OutputPath, default item globs); the targets file reads your final values and defines the build steps. The long form is useful when you need to put something after the SDK's targets in the same file; otherwise prefer the attribute.

Other SDKs layer on top of this one: Microsoft.NET.Sdk.Web (ASP.NET Core: adds the shared framework reference, appsettings*.json copying, launch settings), Microsoft.NET.Sdk.Razor, Microsoft.NET.Sdk.Worker. Each is the base SDK plus extra props and targets.

Default items: the globs you never wrote

The SDK adds three item types for you:

Item Default include Used by
Compile **/*.cs the C# compiler
EmbeddedResource **/*.resx resource generation
None **/* (everything else) shown in IDEs; copied only if you ask

All three exclude bin/**, obj/** and files starting with a dot. That's why a source file in a subfolder is compiled without being mentioned, and why a file copied into bin/ by accident is not. You can see the evaluated list with dotnet msbuild -getItem:Compile (it prints JSON with every item's metadata; here we keep just the names):

bash · try it yourself
mkdir globs && cd globs
cat > globs.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <Compile Remove="Experiments/**" />
  </ItemGroup>
</Project>
XML
mkdir -p Models Experiments bin
echo 'class A {}' > A.cs
echo 'class Order {}' > Models/Order.cs
echo 'class Broken { oops }' > Experiments/Draft.cs
echo 'class Stale {}' > bin/Stale.cs
dotnet msbuild -getItem:Compile | grep '"Identity"'
dotnet build -nologo -v:q > /dev/null && echo "build ok: Experiments/Draft.cs was never compiled"
        "Identity": "A.cs",
        "Identity": "Models/Order.cs",
build ok: Experiments/Draft.cs was never compiled
  • Models/Order.cs was found by the ** glob.
  • bin/Stale.cs was excluded by the SDK's default excludes.
  • Experiments/Draft.cs doesn't compile, but the build passed because <Compile Remove="…" /> took it out of the list. Remove is the right tool to exclude files; there's no need to switch the defaults off.

If you ever need full control (generated projects, very unusual layouts), <EnableDefaultCompileItems>false</EnableDefaultCompileItems> turns the Compile glob off, and EnableDefaultItems turns all three off. Then you list files with <Compile Include="…" /> as in the old days.

Note: A classic mistake is <Compile Include="Models/Order.cs" /> in an SDK project. The file is already included by the glob, so the build fails with NETSDK1022: Duplicate 'Compile' items were included. Use Update to change metadata of an existing item, Remove to drop it.

Properties the SDK reads

The PropertyGroup in a new project holds the handful of settings that differ between projects. A few you'll see everywhere:

Property Meaning
TargetFramework the framework to compile against (net10.0); plural TargetFrameworks for multi-targeting
OutputType Exe or Library (the default)
Nullable enable turns on nullable reference type analysis
ImplicitUsings enable adds global using directives for common namespaces
AssemblyName / RootNamespace default to the project file name
LangVersion defaults to the latest version supported by the target framework

ImplicitUsings is a good example of the SDK writing code for you. It generates a file in obj/ and adds it to Compile:

bash · try it yourself
mkdir usings && cd usings
cat > usings.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
  <ItemGroup>
    <Using Include="System.Text" />
    <Using Remove="System.Net.Http" />
  </ItemGroup>
</Project>
XML
echo 'Console.WriteLine(new StringBuilder("no using lines needed"));' > Program.cs
dotnet build -nologo -v:q > /dev/null
cat obj/Debug/net10.0/usings.GlobalUsings.g.cs
dotnet run --no-build
// <auto-generated/>
global using System;
global using System.Collections.Generic;
global using System.IO;
global using System.Linq;
global using System.Text;
global using System.Threading;
global using System.Threading.Tasks;
no using lines needed

The Using items you add or remove end up in the same generated file. Knowing that "magic" is just generated files and items makes the SDK much less mysterious.

Test yourself

What does <Project Sdk="Microsoft.NET.Sdk"> expand to?

  1. A reference to a NuGet package called Microsoft.NET.Sdk
  2. An import of Sdk.props at the top and Sdk.targets at the bottom of the project
  3. A copy of the SDK into the project folder
  4. Nothing; it's only metadata for IDEs

A draft file Experiments/Draft.cs must not be compiled. What is the idiomatic fix in an SDK-style project?

  1. <EnableDefaultItems>false</EnableDefaultItems>
  2. <Compile Remove="Experiments/**" />
  3. Rename it to Draft.txt
  4. <Compile Include="Experiments/Draft.cs" Exclude="true" />

Why is a .cs file inside bin/ not compiled?

  1. The compiler ignores folders named bin
  2. The SDK's default globs exclude bin/** and obj/**
  3. Files in bin are read-only
  4. It is compiled, but last