TExcelTAD
Home › .NET Builds: MSBuild & NuGet › .NET Builds: What dotnet build Really Does

.NET Builds: What dotnet build Really Does

dotnet new, dotnet build, dotnet run: for a small app those three commands are all you need. They stop being enough when a solution grows to forty projects that must share settings, a CI build picks up a different package version than your laptop, a release needs a NuGet package with a README and debug symbols, or a build takes four minutes and nobody knows which step is slow. This course is about what happens underneath.

Who this course is for

You can write C#: classes, namespaces, generics, async code. The C# course covers that, and its Get Started lesson introduces dotnet new console and the .csproj file. The ASP.NET Core course helps too: its Project Structure lesson shows a real project file with package references. This course starts where those stop. The general ideas behind every build tool (dependency graphs, incremental builds, semantic versioning, lockfiles) are taught language-neutrally in Build Fundamentals.

Two tools behind one command

The dotnet command is a front end. Almost everything it does to a project is delegated to two engines:

Engine What it does Files it reads
MSBuild evaluates the project file, then runs targets (compile, copy, pack, publish) in dependency order, skipping the ones that are up to date .csproj, .props, .targets, Directory.Build.*, solution files
NuGet restores packages: resolves version ranges to exact versions, downloads them into a global cache and writes the result for MSBuild to use PackageReference items, nuget.config, Directory.Packages.props, packages.lock.json

dotnet build is really "restore, then run MSBuild's Build target". dotnet pack, dotnet publish and dotnet test are the same engine running other targets. Once you can read the MSBuild language, none of these commands are magic.

A first session

Every bash run session in this course is a real terminal session recorded with the .NET SDK 10.0.402 (MSBuild 18.9, runtime 10.0.13) on Linux. Here we write a project file by hand, build it twice and run it:

bash · try it yourself
mkdir hello && cd hello
cat > hello.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
</Project>
XML
echo 'System.Console.WriteLine("Hello from MSBuild");' > Program.cs
dotnet build
dotnet build -nologo | grep -E 'restore|->'
dotnet bin/Debug/net10.0/hello.dll
ls obj
  Determining projects to restore...
  Restored /home/learner/hello/hello.csproj (in 67 ms).
  hello -> /home/learner/hello/bin/Debug/net10.0/hello.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:02.33
  Determining projects to restore...
  All projects are up-to-date for restore.
  hello -> /home/learner/hello/bin/Debug/net10.0/hello.dll
Hello from MSBuild
Debug
hello.csproj.nuget.dgspec.json
hello.csproj.nuget.g.props
hello.csproj.nuget.g.targets
project.assets.json
project.nuget.cache

What to notice:

  • The project file names no source files. Program.cs was compiled because the SDK (Microsoft.NET.Sdk) includes **/*.cs by default. The next lesson opens up everything an SDK-style project gets for free.
  • The build began with Determining projects to restore...: NuGet ran first, even with no packages, and wrote obj/project.assets.json. The second build found it up to date.
  • Build output goes to bin/<Configuration>/<TargetFramework>/. Debug is the default configuration. Intermediate files (generated code, the assets file, incremental-build markers) live in obj/.
  • The program is hello.dll, run by the shared runtime through dotnet hello.dll. The SDK also writes a small native launcher, bin/Debug/net10.0/hello, that does the same.

How the sessions in this course work

The machine that recorded the sessions has no network. Instead of nuget.org it has a folder feed at /opt/nuget-feed, registered as the source local. It holds 57 real packages: what the console, classlib, xunit, nunit, mstest and webapi templates restore (xunit 2.9.3, NUnit 4.3.2, MSTest 4.0.2, Microsoft.NET.Test.Sdk 17.14.1 and 18.0.1, …), plus Newtonsoft.Json 13.0.4 and 13.0.3, Humanizer.Core 2.14.1 and Microsoft.SourceLink.GitHub 10.0.402. Asking for anything else fails exactly as a missing package fails on nuget.org, and you can push your own packages to the feed. The global package cache ~/.nuget/packages starts empty in every session, so you see real Installed … from /opt/nuget-feed lines.

Two consequences you'll meet later, and which the course states honestly: only the net10.0 targeting pack is installed (other frameworks need packs from nuget.org), and self-contained or trimmed publishing needs runtime packs that aren't in the feed. Those parts show the real error and explain what happens with network access.

Timings such as Time Elapsed 00:00:01.42 or (in 87 ms) will differ on your machine.

The course map

  1. The MSBuild language: properties, items, targets, conditions, incremental targets and Directory.Build.props.
  2. Seeing inside a build: querying properties and items, verbosity levels and binary logs.
  3. Frameworks and outputs: multi-targeting, publish modes, deterministic builds.
  4. NuGet, consuming: PackageReference, version ranges, transitive dependencies, Central Package Management, lock files, nuget.config and source mapping.
  5. NuGet, producing: dotnet pack with metadata, symbols and SourceLink, pushing to a feed, local tools.
  6. Toolkit, field notes, exercises and a mini project: a solution with a library, an app and tests, sharing settings and package versions, whose library is packed, pushed and consumed by another app.

Tip: Microsoft's reference pages on learn.microsoft.com ("MSBuild reference", "NuGet documentation", "dotnet CLI") are the authority for every property and command named here. When a lesson shows a property, search for it there.

Test yourself

What does dotnet build do first, before compiling anything?

  1. Downloads the .NET runtime
  2. Runs a NuGet restore, then MSBuild's Build target
  3. Runs dotnet publish
  4. Deletes the obj folder

Why was Program.cs compiled although the project file never mentions it?

  1. dotnet build compiles every file in the folder
  2. The Microsoft.NET.Sdk includes **/*.cs as Compile items by default
  3. Program.cs is a reserved name
  4. The C# compiler searches for it

Where does a Debug build of a net10.0 project put its output assembly?

  1. obj/
  2. bin/Debug/net10.0/
  3. out/
  4. ~/.nuget/packages