Skip to content

dotnet format - AllReferencedProjectsLoadedAsync does not track already evaluated projects #56363

Closed

Description

@andrew-hy-cloud

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/download

IDE version

N/A

Other details

No response

Activity

  1. added
    untriaged
    on Sep 21, 2026
  2. locked and limited conversation to collaborators on Sep 21, 2026
  3. unlocked this conversation on Sep 21, 2026
  4. jozsurf commented on Sep 24, 2026

    @jozsurf

    Another data point from a large enterprise codebase: we see the same thing, at a scale where it never finishes.

    • Graph: 380 projects, 7,907 ProjectReference edges (every project lists its dependencies explicitly because of DisableTransitiveProjectReferences=true).
    • Recursive visits the unmemoized walk needs from the root project: about 8.8 × 10¹⁴. A 155-project subgraph already needs about 3.7 × 10⁸.
    • Observed: dotnet format analyzers ran 14+ hours before it was killed. Workspace load takes about 4 minutes. A dotnet-stack sample then shows the only busy thread 19+ frames deep in AnalyzerRunner.<RunCodeAnalysisAsync>g__AllReferencedProjectsLoaded|1_1 → Enumerable.All → itself.
    • Version: dotnet-format from SDK 10.0.201 (Windows). The same unmemoized recursion is on main, release/10.0.2xx and release/10.0.1xx.

    Note: on current branches the function is the synchronous AllReferencedProjectsLoaded(Project), which checks MetadataReferences for mscorlib.dll. Each visit is cheap, but the number of visits grows exponentially. Memoizing on ProjectId (the HashSet<ProjectId> approach suggested above) would make it linear in the number of projects.

    Possibly related, and the reason people may not notice: an absolute --include path matches no documents, so no analyzers run and the command exits 0. Relative paths do match, and those runs hit this hang.

  5. added a commit that references this issue on Oct 13, 2026
    ccb3cc9
  6. added 2 commits that reference this issue on Oct 23, 2026
    bd5ed24
    1ea4898
  7. added a commit that references this issue on Oct 28, 2026
    932e7dd
  8. removed
    untriaged
    on Oct 28, 2026
  9. added 2 commits that reference this issue on Nov 14, 2026
    ea68e47
    cdf8b31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions