dotnet / dotnet/sdk

Order of code fixes applied by `dotnet format` is not deterministic

Open
#51,637 0 comments 1 reaction 0 assignees View on GitHub
Area-Format untriaged
Dominant language
C#
Stars
3.2k
Forks
1.3k
PR merge metrics
PR metrics pending

Description

### Describe the bug

`dotnet format` applies the code fixes from references analyzers in a non-deterministic order.
Repeatedly invoking this command and undoing the change will sometimes result in different code.

### To Reproduce

Only needs 3 files: a .cs, .csproj, and .editorconfig
[code.zip](https://github.com/user-attachments/files/23431832/code.zip)
When built, this code will trigger two warnings (NUnit2003 and NUnit2055).
If `dotnet format` fixes NUnit2003 first, everything is fine. But if it fixes NUnit2055 first, it triggers a bug in the analyzer (https://github.com/nunit/nunit.analyzers/issues/953) that generated non-compiling code.
The order seems to be random each time I repeat this test.

To be clear: The non-compiling code is a separate bug.
I'm raising this issue because I believe that `dotnet format` should produce the same results each time it is invoked.
I only used this issue in NUnit because is makes it obvious that the order of the fixes is different.
The analyzer doesn't seem to have a random components to it, so maybe this is a race condition.

Also: Using the "Fix all warning and errors set in EditorConfig" cleanup in VS18 (Community Insiders [11201.2]) seems to always fix the warning in the same order.

### Further technical details
details of dotnet --info

.NET SDK:
Version: 10.0.100-rc.2.25502.107
Commit: 89c8f6a112
Workload version: 10.0.100-manifests.4d32cd9e
MSBuild version: 18.0.0-preview-25502-107+89c8f6a11

Runtime Environment:
OS Name: Windows
OS Version: 10.0.26200
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\10.0.100-rc.2.25502.107\

.NET workloads installed:
There are no installed workloads to display.
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: 10.0.0-rc.2.25502.107
Architecture: x64
Commit: 89c8f6a112

.NET SDKs installed:
8.0.415 [C:\Program Files\dotnet\sdk]
9.0.306 [C:\Program Files\dotnet\sdk]
10.0.100-rc.2.25502.107 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 10.0.0-rc.2.25502.107 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.0-rc.2.25502.107 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.21 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 10.0.0-rc.2.25502.107 [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:
Not found

Learn more:
https://aka.ms/dotnet/info

Download .NET:
https://aka.ms/dotnet/download

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.