π Understanding dotnet build
and When to Reach for /graphBuild
You've cloned a large repository. The first clean build works perfectly. But after a few tweaks across indirectly referenced projects, something feels... stale. Welcome to the world of incremental builds β and the subtle fixes offered by graph-based dependency resolution.
π What's the Real Issue?
By default, dotnet build
optimizes for speed:
- It builds only the project you target.
- It includes directly referenced projects, but may not reliably recompile indirect dependencies β especially if outputs exist in
bin/obj
folders.
This can lead to surprises:
- You change
ProjectC
, which is referenced byProjectB
, but you're buildingProjectA
(which referencesProjectB
). - Unless
dotnet build
detects timestamp or dependency changes perfectly,ProjectC
might not be rebuilt. - You silently pick up outdated DLLs β and scratch your head over unexpected behavior.
π§ Solution: Precompute the Dependency Graph
MSBuild (and by extension, dotnet build
) supports the /graphBuild
parameter:
dotnet build YourProject.csproj -p:GraphBuild=true
After .NET 5 SDK and MSBuild 16.6 you can even use /graphBuild
or even shorter /graph
parameter:
dotnet build /graphBuild # or dotnet build /graph
This activates static graph mode, ensuring:
- Topological build order of all referenced projects (direct and transitive).
- Predictable and safe parallel builds.
- No reliance on dynamic target evaluation, which can skip certain steps if conditions donβt trigger.
Especially useful in CI/CD, multi-repo, or cross-solution setups.
π‘ When Is It Critical?
Use /graphBuild
when:
- Projects reference others outside their solution.
- Conditional build logic or custom targets affect dependencies.
- You want to ensure clean transitive builds after codebase changes, even if timestamps arenβt reliable.
π§ͺ Example
Letβs say you have this structure:
ProjectA βββ ProjectB βββ ProjectC
You update ProjectC
and run:
dotnet build ProjectA.csproj
Without graphBuild? Might skip rebuilding ProjectC
.
With graphBuild?
dotnet build ProjectA.csproj -p:GraphBuild=true # Or Alt
Guaranteed to build all three in correct order.
π§Ό Bonus Tip for Local Builds
After switching branches or making manual edits, do this for a clean, graph-aware rebuild:
dotnet clean ProjectA.csproj dotnet build ProjectA.csproj -p:GraphBuild=true
βοΈ TL;DR
-
dotnet build
defaults to speed over precision. -
-p:GraphBuild=true
brings back accuracy in dependency chains. - Use it for indirect references, cross-solution setups, or clean builds after changes.
Top comments (0)