Field Notes: Repo-Wide Build Settings
These notes collect the settings that experienced .NET teams put in place on day one of a repository, and the reasons, learned the hard way. Each one is small; together they decide whether a solution with fifty projects stays easy to build.
1. One root, four files
A healthy .NET repository has these at its root, all committed:
| File | Purpose |
|---|---|
global.json |
pins the SDK version |
nuget.config |
<clear /> plus every feed the build needs, with source mapping |
Directory.Build.props |
shared defaults: language, warnings, metadata, output layout |
Directory.Packages.props |
every package version (CPM), with transitive pinning |
plus a .gitignore and, as projects grow, Directory.Build.targets. Project files then shrink to what is genuinely specific: OutputType, references, maybe TargetFramework.
2. Pin the SDK with global.json
mkdir repo && cd repo
dotnet new globaljson --sdk-version 10.0.402 --roll-forward latestFeature > /dev/null
cat global.json
dotnet --version
echo "--- a repo that asks for an SDK this machine doesn't have"
sed -i 's/10.0.402/10.0.999/; s/latestFeature/disable/' global.json
dotnet build 2>&1 | grep -E 'compatible|Requested|Install the'
echo "exit: ${PIPESTATUS[0]}"{
"sdk": {
"rollForward": "latestFeature",
"version": "10.0.402"
}
}
10.0.402
--- a repo that asks for an SDK this machine doesn't have
A compatible .NET SDK was not found.
Requested SDK version: 10.0.999
Install the [10.0.999] .NET SDK or update [/home/learner/repo/global.json] to match an installed SDK.
exit: 155
The SDK version decides the compiler, MSBuild and NuGet versions and many defaults (dotnet new sln writing .slnx, implicit SourceLink…). With an SDK the machine doesn't have, every dotnet command stops before MSBuild starts, with a message that names the file and the version. Without global.json, every machine builds with whatever SDK is newest on it. rollForward: latestFeature allows newer patch and feature bands of the same major.minor (10.0.4xx, 10.0.5xx), which keeps security fixes flowing; disable demands an exact version. The actions/setup-dotnet GitHub action and Azure Pipelines' UseDotNet task can install the version named in global.json.
3. Shared defaults, including TreatWarningsAsErrors
mkdir repo && cd repo
cat > Directory.Build.props <<'XML'
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AnalysisLevel>latest-recommended</AnalysisLevel>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
</Project>
XML
mkdir Core && cat > Core/Core.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
</Project>
XML
cat > Core/Names.cs <<'CS'
public static class Names
{
public static string First(string? full) => full.Split(' ')[0];
}
CS
dotnet build Core -nologo -v:q 2>&1 | grep -E 'error' | awk '!seen[$0]++' | sed 's|/home/learner/repo/||'
echo "--- fixed"
sed -i 's/full.Split/(full ?? "").Split/' Core/Names.cs
sed -i '1i namespace Repo;' Core/Names.cs
dotnet build Core -nologo -v:q > /dev/null && echo "build ok"Core/Names.cs(3,49): error CS8602: Dereference of a possibly null reference. [/home/learner/repo/Core/Core.csproj]
Core/Names.cs(1,21): error CA1050: Declare types in namespaces (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1050) [/home/learner/repo/Core/Core.csproj]
--- fixed
build ok
A nullable-reference warning (CS8602) became a build error, and so did an analyzer rule switched on by AnalysisLevel (CA1050, types belong in namespaces). That's the point: warnings that are allowed to accumulate get ignored, and 400 warnings hide the one that matters. Practical rules that make TreatWarningsAsErrors livable:
- Turn it on at the start of a repository. Retrofitting it onto a codebase with 2,000 warnings needs a plan: fix per project, or temporarily list codes in
<WarningsNotAsErrors>. - Suppress narrowly.
<NoWarn>$(NoWarn);CA1062</NoWarn>in one project with a comment explaining why, or#pragma warning disablearound one line, never a blanketNoWarnin the root file. - Keep NuGet audit warnings visible. New vulnerability advisories (
NU1901–NU1904) appear without any code change and would suddenly break every build. Many teams listNU1901;NU1902inWarningsNotAsErrorsand fail CI only on high and critical (NU1903;NU1904). AnalysisLevelandEnforceCodeStyleInBuildmake the .NET analyzers and.editorconfigstyle rules part of the build, so IDE hints and CI agree.
4. Never commit bin/ and obj/
mkdir repo && cd repo
git init -q
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("hi");' > App/Program.cs
dotnet build App -nologo -v:q > /dev/null
echo "without .gitignore: $(git status --porcelain -uall | wc -l) files to commit"
git status --porcelain -uall | grep -c '/obj/' | sed 's/$/ of them under obj\//'
dotnet new gitignore > /dev/null
echo "with dotnet's .gitignore: $(git status --porcelain -uall | wc -l) files to commit"
git status --porcelain -uallwithout .gitignore: 25 files to commit
18 of them under obj/
with dotnet's .gitignore: 3 files to commit
?? .gitignore
?? App/App.csproj
?? App/Program.cs
obj/ holds machine-specific absolute paths (project.assets.json, the .nuget.g.props files), so a committed obj/ breaks restores on every other machine in confusing ways, and bin/ is pure build output. dotnet new gitignore writes GitHub's maintained .NET .gitignore (bin, obj, IDE folders, test results, *.binlog…). If you use ArtifactsPath, add artifacts/ too. The files that should be committed: project files, the four root files, packages.lock.json and dotnet-tools.json.
5. Keep logic out of project files
Project files that contain targets, Exec calls and long conditions are hard to review and impossible to share. Move:
- shared targets to
Directory.Build.targets(or abuild/*.targetsfile it imports); - anything bigger than a few tasks to a small MSBuild task in C# or a .NET tool run by a target;
- per-environment decisions to properties set by CI (
-p:Version=…,ContinuousIntegrationBuild), not to conditions on machine names or paths.
6. Use CPM even for small repositories
Adding Central Package Management later means touching every project file at once. Starting with it costs one file. Turn on CentralPackageTransitivePinningEnabled as well: the day a transitive dependency gets a security advisory, the fix is one line in Directory.Packages.props instead of a hunt through the graph.
Test yourself
What does rollForward: latestFeature in global.json allow?
- Any newer SDK, including new major versions
- Newer patch and feature bands of the requested major.minor SDK
- Only the exact version
- Older SDKs
TreatWarningsAsErrors is on. A team wants an exception for one analyzer rule in one project. What's the narrowest fix?
- Turn TreatWarningsAsErrors off in Directory.Build.props
- Add the rule's code to NoWarn (or WarningsNotAsErrors) in that project, with a comment
- Delete the analyzer package
- Build with -v:q
Why does a committed obj/ folder break other developers' builds?
- It is too large for git
- It contains machine-specific absolute paths and restore results that don't match other machines
- MSBuild refuses to read committed files
- It contains passwords