DEV Community

Enis Necipoglu
Enis Necipoglu

Posted on

What is 'graphBuild' in .NET CLI & MsBuild

πŸš€ 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 by ProjectB, but you're building ProjectA (which references ProjectB).
  • 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 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

You update ProjectC and run:

dotnet build ProjectA.csproj 
Enter fullscreen mode Exit fullscreen mode

Without graphBuild? Might skip rebuilding ProjectC.

With graphBuild?

dotnet build ProjectA.csproj -p:GraphBuild=true # Or Alt 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

✍️ 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)