Skip to content

dotnet-format is very slow on larger repos #1378

Open

Description

on our repository (https://github.com/xSke/PluralKit), dotnet-format will take between 4 and 8 seconds to run, even when no changes need to be applied.

I saw #757 but I'm not sure if it's related, thus the new issue.

here's a -v diag logging of a particularly bad run:

$ dotnet-format -v diag
  The dotnet format version is '5.1.225507+756d5a1c121be3e57e924788af64aa5607dc24e1'.
  The dotnet runtime version is '5.0.9'.
  The dotnet CLI version is '5.0.400'.
  Using MSBuild.exe located in '/home/spiral/.dotnet/sdk/5.0.400/'.
  Formatting code files in workspace '/home/spiral/sources/pluralkit/PluralKit.sln'.
  Loading workspace.
  Project PluralKit.Bot is using configuration from '/home/spiral/sources/pluralkit/.editorconfig'.
  Project PluralKit.Bot is using configuration from '/home/spiral/sources/pluralkit/PluralKit.Bot/obj/Debug/net5.0/PluralKit.Bot.GeneratedMSBuildEditorConfig.editorconfig'.
  Project PluralKit.Bot is using configuration from '/home/spiral/.dotnet/sdk/5.0.400/Sdks/Microsoft.NET.Sdk/analyzers/build/config/AnalysisLevel_5_Default.editorconfig'.
  Project PluralKit.Core is using configuration from '/home/spiral/sources/pluralkit/.editorconfig'.
  Project PluralKit.Core is using configuration from '/home/spiral/sources/pluralkit/PluralKit.Core/obj/Debug/net5.0/PluralKit.Core.GeneratedMSBuildEditorConfig.editorconfig'.
  Project PluralKit.Core is using configuration from '/home/spiral/.dotnet/sdk/5.0.400/Sdks/Microsoft.NET.Sdk/analyzers/build/config/AnalysisLevel_5_Default.editorconfig'.
  Project PluralKit.API is using configuration from '/home/spiral/sources/pluralkit/.editorconfig'.
  Project PluralKit.API is using configuration from '/home/spiral/sources/pluralkit/PluralKit.API/obj/Debug/net5.0/PluralKit.API.GeneratedMSBuildEditorConfig.editorconfig'.
  Project PluralKit.API is using configuration from '/home/spiral/.dotnet/sdk/5.0.400/Sdks/Microsoft.NET.Sdk/analyzers/build/config/AnalysisLevel_5_Default.editorconfig'.
  Project PluralKit.Tests is using configuration from '/home/spiral/sources/pluralkit/.editorconfig'.
  Project PluralKit.Tests is using configuration from '/home/spiral/sources/pluralkit/PluralKit.Tests/obj/Debug/net5.0/PluralKit.Tests.GeneratedMSBuildEditorConfig.editorconfig'.
  Project PluralKit.Tests is using configuration from '/home/spiral/.dotnet/sdk/5.0.400/Sdks/Microsoft.NET.Sdk/analyzers/build/config/AnalysisLevel_5_Default.editorconfig'.
  Project Myriad is using configuration from '/home/spiral/sources/pluralkit/.editorconfig'.
  Project Myriad is using configuration from '/home/spiral/sources/pluralkit/Myriad/obj/Debug/net5.0/Myriad.GeneratedMSBuildEditorConfig.editorconfig'.
  Project Myriad is using configuration from '/home/spiral/.dotnet/sdk/5.0.400/Sdks/Microsoft.NET.Sdk/analyzers/build/config/AnalysisLevel_5_Default.editorconfig'.
  Complete in 4845ms.
  Determining formattable files.
  Complete in 1144ms.
  Running formatters.
  Complete in 1146ms.
  Formatted 0 of 309 files.
  Format complete in 7138ms.

Activity

  1. erdalsivri commented on Jan 4, 2022

    @erdalsivri
    Contributor

    I have been investigating dotnet format slowness in our large solution. Here is what I've found out so far:

    • dotnet format whitespace --folder is much faster assuming you are using .Net 6 (with the new version of dotnet format) and are only interested in whitespace formatting. If you need style and analyzers, things get much slower due to compilation.
    • Running dotnet format under a specific project instead of the whole solution helps. For example, dotnet format Solution.sln --include SomeProject/SomeFile.cs is much slower than dotnet format SomeProject/SomeProject.csproj --include SomeProject/SomeFile.cs for some reason.
      Of course this workaround is only useful if you are doing a git diff to format only affected files in a branch. I wrote a simple bash script to group modified files by project and run dotnet format concurrently for each affected project with --include <path> option. It helped reduce the total runtime from more than 2 mins to under 1 min in our solution assuming a couple of files are modified under a few different projects. We do need style and analyzers so can't use the fast whitespace command with the --folder option.

    I suspect redundant compilation (due to referenced projects getting built/analyzed multiple times) is the root cause for the slowness but I am not sure yet. Needs more debugging

  2. erdalsivri commented on Jan 5, 2022

    @erdalsivri
    Contributor

    CSharpSimplifyTypeNamesDiagnosticAnalyzer is responsible for about 20 seconds of runtime for one of our projects. This is what I got after running analyzers separately with extra logging:

      [01/05/2022 02:37:26] Running 7 analyzers on Kaggle.XXX
      [01/05/2022 02:37:26] Running analyzer MisplacedUsingDirectivesDiagnosticAnalyzer on Kaggle.XXX
      [01/05/2022 02:37:27] Finished running analyzer MisplacedUsingDirectivesDiagnosticAnalyzer on Kaggle.XXX in 914ms
      [01/05/2022 02:37:27] Running analyzer CSharpUnboundIdentifiersDiagnosticAnalyzer on Kaggle.XXX
      [01/05/2022 02:37:28] Finished running analyzer CSharpUnboundIdentifiersDiagnosticAnalyzer on Kaggle.XXX in 653ms
      [01/05/2022 02:37:28] Running analyzer CSharpUseExplicitTypeDiagnosticAnalyzer on Kaggle.XXX
      [01/05/2022 02:37:29] Finished running analyzer CSharpUseExplicitTypeDiagnosticAnalyzer on Kaggle.XXX in 990ms
      [01/05/2022 02:37:29] Running analyzer CSharpNamingStyleDiagnosticAnalyzer on Kaggle.XXX
      [01/05/2022 02:37:30] Finished running analyzer CSharpNamingStyleDiagnosticAnalyzer on Kaggle.XXX in 868ms
      [01/05/2022 02:37:30] Running analyzer CSharpAddBracesDiagnosticAnalyzer on Kaggle.XXX
      [01/05/2022 02:37:31] Finished running analyzer CSharpAddBracesDiagnosticAnalyzer on Kaggle.XXX in 764ms
      [01/05/2022 02:37:31] Running analyzer CSharpSimplifyTypeNamesDiagnosticAnalyzer on Kaggle.XXX
      [01/05/2022 02:37:50] Finished running analyzer CSharpSimplifyTypeNamesDiagnosticAnalyzer on Kaggle.XXX in 19688ms
      [01/05/2022 02:37:50] Running analyzer CSharpPreferFrameworkTypeDiagnosticAnalyzer on Kaggle.XXX
      [01/05/2022 02:37:51] Finished running analyzer CSharpPreferFrameworkTypeDiagnosticAnalyzer on Kaggle.XXX in 477ms
      [01/05/2022 02:37:51] Finished running 7 analyzers on Kaggle.XXX in 24407ms
    

    Turns out there are known performance issues with this analyzer: dotnet/roslyn#18950

    The runtime seems to be proportional to number of files (or number of lines of code) in the project. We have several projects with over 300 files and this analyzer takes at least 5 seconds to run for each of these projects.

  3. erdalsivri commented on Jan 5, 2022

    @erdalsivri
    Contributor

    Another thing I noticed is that NamingStyleDiagnosticAnalyzer always runs for all projects even if they are not included with --include. For example, dotnet format --include SomeProject/SomeFile.cs will run the NamingStyleDiagnosticAnalyzer in SomeOtherProject and just discard the result as no files are specified under SomeOtherProject.

    Filtering out the analyzer based on formattablePaths before the following line fixes the issue:

    if (analyzer.GetType().FullName?.EndsWith("NamingStyleDiagnosticAnalyzer") == true)

    GetSeverityAsync does have a formattablePaths check but that's after the line above so NamingStyleDiagnosticAnalyzer is always included.

    This fixed's reduced the runtime of dotnet format --include SomeProject/SomeFile.cs by 50% in our solution (from more than 2 minutes to 1 minute). I will prepare a PR

  4. erdalsivri commented on Jan 5, 2022

    @erdalsivri
    Contributor

    Here is the PR to fix the issue with redundant runs of NamingStyleDiagnosticAnalyzer: #1485

  5. sander1095 commented on May 18, 2022

    @sander1095
    • I wrote a simple bash script to group modified files by project and run dotnet format concurrently for each affected project with --include <path> option.

    Hey @erdalsivri , could you perhaps share this script? Thanks!

  6. sander1095 commented on May 19, 2022

    @sander1095

    I think it's a good idea to link this to #1479 because both need to be fix to make this tool actually usable in pre commit hooks

  7. JoeRobich commented on May 19, 2022

    @JoeRobich
    Member

    Just wanted to share some comments from #1479

    Two things go into why Style and Analyzer operations are slower.

    First, they require that we have correct semantic information about your code. To do this we rely on MSBuild performing a design-time build where we can know the exact set of code files as well as what references to include with each project. This is equivalent to what Visual Studio or OmniSharp does when opening a project/solution. It is less costly than a full compile, but it can take anywhere from seconds to minutes depending on the size of your solution.

    Second, we have to actually run the analyzers and apply code fixes. Running analyzers requires additional in-memory compilations. The results of one analysis become invalid as we apply code fixes from earlier analysis.

    We have discussed alternate ways to speed up populating our workspace. Ultimately MSBuild will be involved at some point because .NET builds are so highly customizable the only way not to be wrong is to have MSBuild do the work.

    1. We could use a cache of MSBuild .binlog (or other format) containing the code files and dependency information we need. At which point this becomes a staleness problem. If dependencies aren't being added/removed, then this cache may remain a viable source of information for a long period.
    2. We could develop a Project Server of sorts. This service would have an initial slow startup that you experience today but, while it remains running, future invocations wouldn't have to pay the MSBuild cost.

    We are not committing to either approach today, but know that this a problem that we are thinking on.

  8. shtratos commented on Apr 26, 2023

    @shtratos

    @JoeRobich could you please share what is the current state of this effort?

    I just ran dotnet format on a unit tests project with 10 files which is a part of a large repository.

    Loading workspace took ridiculous amount of time:

      Complete in 1045066ms.
      Determining formattable files.
    

    What's puzzling is that it seems to figure out via MSBuild the whole dependency chain of projects to figure out their editorconfig settings and then simply discards them as they are not in scope.

    • What is the actual information that formatter needs from the MSBuild?
    • Can we have a lightweight mode where it just walks up the directories searching for .editorconfig files or just using user-specified .editorconfig without loading the whole project in memory?
    • If .NET team is serious about developers adopting dotnet-format as a default out-of-the-box formatter - this needs to be prioritized, funded and fixed.

    Happy to share my binlog if it helps.

  9. shtratos commented on Apr 26, 2023

    @shtratos

    Not to mention that dotnet format process consumes 25GB of RAM while running.

  10. niemyjski commented on Feb 2, 2024

    @niemyjski

    I don't think any teams from Microsoft are using this, because if they were they would find it unacceptable.

  11. johnthagen commented on Mar 27, 2024

    @johnthagen

    I can confirm for my relatively small project dotnet format whitespace runs 2x as fast as dotnet format. Sadly, it still runs in 4 seconds even with only whitespace, which is still a noticeable slow down in development flow compared to something like Ruff format from the Python ecosystem which runs in imperceivable time.

    I had the following set up a File Watcher in Rider:

    dotnet format whitespace $SolutionPath$ --include $FileRelativePath$
    

    I was hoping that having it only format the single file I was working on would speed things up, but as mentioned above it seems like a full project build/analysis of some kind is involved.

  12. 9 remaining items

  13. johnthagen commented on Jun 1, 2024

    @johnthagen

    i'm not even sure why you guy still stick with this tool,

    I can't speak for everyone, but there is value in a tool that is installed by default with the dotnet CLI, and a first party tool is generally more universally used than a third party. That said, I will probably try out CSharpier soon since in my experience opinionated formatters save so much time (dotnet format is not very strict compared to things like Ruff format or Prettier).

  14. rmannibucau commented on Sep 17, 2024

    @rmannibucau

    Same there, it is way too slow and even if I find csharpier insanely great it doesn't support analyzers but there should be something in between (js did it quite well, I don't see why c# can't, maybe using a post build phase instead of plugging into the build directly and just reusing loaded model instead of building twice).

  15. abdulkareemnalband commented on Jul 24, 2026

    @abdulkareemnalband

    What if dotnet format supported caching similar to https://eslint.org/docs/latest/use/command-line-interface#caching
    based hash of files content. then format can skip recomputing issues if file is not changed

  16. rmannibucau commented on Jul 24, 2026

    @rmannibucau

    caching would be great but on CI it would be disabled most of the time I think and still csharpier is fast without cache so maybe use cache as a final optimization rather than a first class citizen in the impl (for >= 2 executions, indeed cache in a single one is fine).

  17. abdulkareemnalband commented on Jul 24, 2026

    @abdulkareemnalband

    You can cache inside CI also
    Lot of CIs provide caching artifacts, which will be populated on next run for next run,

  18. rmannibucau commented on Jul 24, 2026

    @rmannibucau

    @abdulkareemnalband not saying you can't, more than 1. it will not be done that often, 2. it is often desired to start on the CI without cache (at least nightly builds for ex to ensure repeatability - even for formatting, cache can hide some issues), so will be not enabled/disabled
    since we know it can be fast (csharpier) there is no reason to start by optimization based on cache IMHO

  19. abdulkareemnalband commented on Jul 24, 2026

    @abdulkareemnalband

    @abdulkareemnalband not saying you can't, more than 1. it will not be done that often, 2. it is often desired to start on the CI without cache (at least nightly builds for ex to ensure repeatability - even for formatting, cache can hide some issues), so will be not enabled/disabled since we know it can be fast (csharpier) there is no reason to start by optimization based on cache IMHO

    @rmannibucau I understand you are worried about absolute perf gain.
    But I think caching can be additional benefit for DX.

  20. rmannibucau commented on Jul 24, 2026

    @rmannibucau

    @abdulkareemnalband not again caching, just stating that it must be fast without and using chsarpier as a ref it doesn't need caching so I would put caching as a workaround and failure of the rework rather than a core feature for next iteration

  21. Frulfump commented on Sep 11, 2026

    @Frulfump

    It's also slow on small repos dotnet/sdk#49910 (Shouldn't all issues be moved to the SDK repo now given this repos README.md?

    Going forward, the .NET SDK team is using the dotnet/sdk repository for further development and respond to issues formerly in this repository.

    )

  22. christianomicron commented on Sep 23, 2026

    @christianomicron

    @rmannibucau I think you might be mixing two things - "cache" is a very broad subject, and in some cases, you just can't get around it (but often it might come up as a different name).

    You have a valid point that caching dependencies in CI might not always be desirable, but caching the formatting results is something entirely different. Conceptually, the formatting tool would write out a (cache) file of:

    formatterVersion: v1.2.3
    fileHashes:
        "some/path/in/the/project": 1234567890abcdef
        "another/path/in/the/project": abcdef1234567890
    • If the current formatter version is different, then the cache is unusable (but this is unlikely, how often are you updating your formatter?).
    • If the file to be formatted matches that cached hash, then there isn't any reason to recheck it - just skip.

    Admittedly, I don't know the full details behind the dotnet format process - so perhaps a file cache might not help much if the slowness is coming from compilation (which might not be easily evaded). Otherwise, I think such a result cache system should be simple to implement and provide tangible benefits - even as a standalone app/script could provide a benefit by skipping dotnet format when nothing has changed (yes, also in CI).

  23. rmannibucau commented on Sep 23, 2026

    @rmannibucau

    @christianomicron not sure if I got it right but my point is you want cache on the CI for PR iterations etc and you want to drop it for master (at least nightly or friends) cause you don't want any state can impact the execution or skip any processing, so a cache is not a real solution there for me, in particular when alternative formatter are insanely fast already so it reall sounds like something to fix and not workaround with a cache to me

  24. christianomicron commented on Sep 23, 2026

    @christianomicron

    @rmannibucau as I said, it depends on what we mean by cache:

    • If you mean caching dependencies (e.g. NuGet packages), then yes, I agree with you.
    • If you mean caching formatting results, then that's a different thing. As I said above, this works pretty universally.

    Pretty much any formatter can cheaply do this: hash the file, compare it with the hash from the cache, and skip it if it hasn't changed. Since formatting is deterministic, if the same input was already formatted with the same formatter version, there's no reason to do the work again. Hash collisions are also negligible with a suitable algorithm (and they're extremely fast).

  25. rmannibucau commented on Sep 23, 2026

    @rmannibucau

    @christianomicron nop, if I want to go on your reasoning then I would cache dependencies cause they are immutable but point to have some CI job cache-less is to ensure there is no local side effects and it includes the formatting, there is nothing specific there.

    Once again, the impl would be optimized and there would be no way to make it fast I would look for some fingerprint based cache yes, but there it is clearly not the case, it is just that the impl is not done to be fast as some other formatters so cache is not the primary solution for this issue.

  26. Frulfump commented on Sep 24, 2026

    @Frulfump

    From the README.md of this repo:

    Repository Consolidation
    Going forward, the .NET SDK team is using the dotnet/sdk repository for further development and respond to issues formerly in this repository.

    For finding the latest installers table and other installation information, see the Installing the SDK section in that repository's readme.

    And pinned issue #2152

    So I think ideally this conversation should be moved there.

    Interestingly enough Microsoft has performance as a feature and does blogs like this https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-11/ each year. Ideally we would get Stephen Toub interested in fixing this issue.

    I assume issues and PRs will be closed/moved/locked and the repo archived but at least new issue/PR creation turned off after .NET 8 and 9 goes out of support in November.

  27. rmannibucau commented on Sep 24, 2026

    @rmannibucau

    maybe some proposal: dotnet/sdk#56394

    Enables a 3.5× speedup, followed by an additional 1.5× speedup with caching (25%): 7s → 2s → 1.3s.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions