.Net Build Project.csproj Still Searching Directories

Problem Statement

When running the following command to build a single project:

dotnet build project.csproj -c Release

…the .NET SDK unexpectedly searches and builds other .csproj files within the directory tree—even though no explicit project references exist in project.csproj.

Context

File structure:

./project.csproj
./scriptlibrary/Projects/project1/dir1/dir2/project2.csproj
./scriptlibrary/Projects/project1/dir1/dir3/project3.csproj
./scriptlibrary/Projects/project1/dir1/dir4/project4.csproj

Expected Behavior:

Only project.csproj should be built, as it has no project reference to any of the other .csproj files.

Observed Behavior:

All .csproj files are being discovered and built.

Root Cause

When running dotnet build inside a folder that contains a solution file (.sln), the build may behave differently based on project references and global.json settings.

If you run dotnet build inside a directory with multiple project files, the build system might attempt a restore across all discovered .csproj files (depending on SDK version and MSBuild behavior).

If there’s a solution file (*.sln) in the folder, and it references those projects, then dotnet build may implicitly build all projects listed in that solution file—even if a specific .csproj is passed.

Solutions / Workarounds

Option 1: Disable Recursive Search Using –no-dependencies

To restrict the build only to the explicitly passed .csproj file:

dotnet build project.csproj -c Release –no-dependencies

Option 2: Avoid Building Solution File (if exists)

Ensure you’re not accidentally building a .sln file instead of .csproj. Avoid:

dotnet build # Might build the entire solution

Use:

dotnet build ./project.csproj

Option 3: Use MSBuild with Explicit Project

Sometimes MSBuild gives more fine-grained control:

msbuild project.csproj /p:Configuration=Release /p:BuildProjectReferences=false

The /p:BuildProjectReferences=false option prevents MSBuild from following project references (even if defined).

Option 4: Isolate the Project Temporarily

As a fallback:

  • Temporarily copy the target .csproj file to a clean folder.
  • Run dotnet build in that isolated directory to ensure no unexpected builds are triggered.

Need Help With .Net Development?

Work with our skilled .Net developers to accelerate your project and boost its performance.

Support On Demand!