Repository navigation
Description
Describe the bug
I'm currently working on a legacy project that has over 120 csproj projects that have convoluted dependency chains between them. dotnet format would take fifteen minutes on this project, despite other operations not being so bad.
After some debugging, it looks like the AllReferencedProjectsLoadedAsync in AnalyzerRunner.cs was taking up most of the time. It recursively checks every project to see that it compiles, but doesn't track if a project has already been checked.
So if we had projects, A, B, C, D, E, with the following dependencies.
A -> B, C
B -> D, E
C -> D, E
Then the AllReferencedProjectsLoadedAsync code will check every possible iteration of the dependency chain,
A -> B -> D
-> E
C -> D
-> E
and will check that D, E compile twice, and check all of their dependencies twice.
In our codebase, where there are lots of projects with unoptimized project-references, projects were being checked many times over, which led to minutes of unnecessary work.
A possible fix is to have a HashSet<ProjectId> that tracks if a project has been evaluated as 'true' to avoid unnecessary double-checking.
static async Task<bool> OptimizedAllReferencedProjectsLoadedAsync(Project project, CancellationToken cancellationToken , HashSet<ProjectId> projects)
{
if (projects.Contains(project.Id))
{
return true;
}
var compilation = await project.GetCompilationAsync(cancellationToken);
if (compilation is null)
return false;
// If we don't have a System.Object this project is clearly missing references and we shouldn't try to process it further
if (compilation.ObjectType.TypeKind == TypeKind.Error)
return false;
foreach (var projectReference in project.ProjectReferences)
{
var referencedProject = project.Solution.GetProject(projectReference.ProjectId);
Debug.Assert(referencedProject is not null);
if (!await OptimizedAllReferencedProjectsLoadedAsync(referencedProject, cancellationToken, projects))
return false;
}
projects.Add(project.Id);
return true;
}Steps to reproduce
Simple artificial example.
Powershell script to create 30 library projects where each has a dependency on all previous ones.
$existingProjects=@()
foreach($i in 0..30){
$projectName = "proj_$i"
dotnet new classlib --name $projectName --force
$projectPath = "$($projectName)/$($projectName).csproj"
if($existingProjects){
dotnet reference add --project $projectPath $existingProjects
}
$existingProjects += "$($projectName)/$($projectName).csproj";
}Expected behavior
Running dotnet format style \proj_30\proj_30.csproj -v diag takes a reasonable amount of time. There are 30 projects, but each one only contains one default file ('Class1.cs') each, so formatting shouldn't take too long.
Actual behavior
Running dotnet build .\proj_30\proj_30.csproj , takes 34 seconds.
Running dotnet format whitespace .\proj_30\proj_30.csproj , takes 16 seconds.
Running dotnet format style .\proj_30\proj_30.csproj takes 11.5 minutes.
Running dotnet format style on projects in order, it starts to slow down after project 23.
foreach($i in 0..30){
Write-Host "Formatting proj_$i.csproj"
Measure-Command {
dotnet format style "./proj_$i/proj_$i.csproj" --no-restore
}
}Is this a regression?
Not sure
Are there any workarounds?
The code works, it's just very slow.
dotnet --info output
.NET SDK:
Version: 10.0.112
Commit: 95017c711e
Workload version: 10.0.100-manifests.b0700452
MSBuild version: 18.0.11+95017c711
Runtime Environment:
OS Name: Windows
OS Version: 10.0.26100
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\10.0.112\
.NET workloads installed:
[wasm-tools-net8]
Installation Source: VS 18.9.12120.119
Manifest Version: 10.0.112/10.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\10.0.100\microsoft.net.workload.mono.toolchain.net8\10.0.112\WorkloadManifest.json
Install Type: Msi
[wasm-tools]
Installation Source: VS 18.9.12120.119
Manifest Version: 10.0.112/10.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\10.0.100\microsoft.net.workload.mono.toolchain.current\10.0.112\WorkloadManifest.json
Install Type: Msi
Configured to use workload sets when installing new manifests.
No workload sets are installed. Run "dotnet workload restore" to install a workload set.
Host:
Version: 11.0.0-rc.1.26425.128
Architecture: x64
Commit: 3551975be0
.NET SDKs installed:
8.0.425 [C:\Program Files\dotnet\sdk]
9.0.311 [C:\Program Files\dotnet\sdk]
10.0.112 [C:\Program Files\dotnet\sdk]
10.0.400 [C:\Program Files\dotnet\sdk]
10.0.401 [C:\Program Files\dotnet\sdk]
11.0.100-rc.1.26425.128 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.31 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 10.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 10.0.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 11.0.0-rc.1.26425.128 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 8.0.24 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.31 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.19 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 11.0.0-rc.1.26425.128 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 8.0.24 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.30 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.31 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.19 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 10.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 10.0.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 11.0.0-rc.1.26425.128 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
C:\code\XXXXXXXXXXXXX\global.json
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/downloadIDE version
N/A
Other details
No response
Activity
jozsurf commentedon Sep 24, 2026
Another data point from a large enterprise codebase: we see the same thing, at a scale where it never finishes.
ProjectReferenceedges (every project lists its dependencies explicitly because ofDisableTransitiveProjectReferences=true).dotnet format analyzersran 14+ hours before it was killed. Workspace load takes about 4 minutes. Adotnet-stacksample then shows the only busy thread 19+ frames deep inAnalyzerRunner.<RunCodeAnalysisAsync>g__AllReferencedProjectsLoaded|1_1→Enumerable.All→ itself.main,release/10.0.2xxandrelease/10.0.1xx.Note: on current branches the function is the synchronous
AllReferencedProjectsLoaded(Project), which checksMetadataReferencesformscorlib.dll. Each visit is cheap, but the number of visits grows exponentially. Memoizing onProjectId(theHashSet<ProjectId>approach suggested above) would make it linear in the number of projects.Possibly related, and the reason people may not notice: an absolute
--includepath matches no documents, so no analyzers run and the command exits 0. Relative paths do match, and those runs hit this hang.Check each referenced project once in dotnet format
Check each referenced project once in dotnet format
Check each referenced project once in dotnet format
Check each referenced project once in dotnet format (#56621)
Check each referenced project once in dotnet format
Check each referenced project once in dotnet format