Skip to content

dotnet format throws because it can't determine file encodings in 9.0.200 #46780

Closed
dotnet/roslyn
#77354

Description

@DavidZidar

Describe the bug

It is no longer possible to use dotnet format without arguments to format code in certain projects because it has started throwing an error in 9.0.200. dotnet format --verify-no-changes does still work however.

What's worse, it doesn't specify what file might be the issue so it is very hard to figure out what might be going wrong.

To Reproduce

I'm not exactly sure since it doesn't tell me what file it has a problem with, but I'm just running dotnet format in an ASP.NET Core 9 project with about 1500 C# files.

Exceptions (if any)

Unhandled exception: System.Exception: source text did not have an identifiable encoding
   at Microsoft.CodeAnalysis.Tools.Formatters.CharsetFormatter.IsEncodingEquivalent(SourceText sourceText, Encoding encoding)
   at Microsoft.CodeAnalysis.Tools.Formatters.CharsetFormatter.<>c__DisplayClass10_0.<FormatFileAsync>b__0()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at Microsoft.CodeAnalysis.Tools.Formatters.DocumentFormatter.GetFormattedSourceTextAsync(Document document, OptionSet optionSet, AnalyzerConfigOptions analyzerConfigOptions, FormatOptions formatOptions, ILogger logger, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Tools.Formatters.DocumentFormatter.<>c__DisplayClass8_1.<<FormatFiles>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.CodeAnalysis.Tools.Formatters.DocumentFormatter.ApplyFileChangesAsync(Solution solution, ImmutableArray`1 formattedDocuments, FormatOptions formatOptions, ILogger logger, List`1 formattedFiles, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Tools.Formatters.DocumentFormatter.FormatAsync(Workspace workspace, Solution solution, ImmutableArray`1 formattableDocuments, FormatOptions formatOptions, ILogger logger, List`1 formattedFiles, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Tools.CodeFormatter.RunCodeFormattersAsync(Workspace workspace, Solution solution, ImmutableArray`1 formattableDocuments, FormatOptions formatOptions, ILogger logger, List`1 formattedFiles, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Tools.CodeFormatter.FormatWorkspaceAsync(FormatOptions formatOptions, ILogger logger, CancellationToken cancellationToken, String binaryLogPath)
   at Microsoft.CodeAnalysis.Tools.FormatCommandCommon.FormatAsync(FormatOptions formatOptions, ILogger`1 logger, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Tools.Commands.RootFormatCommand.FormatCommandDefaultHandler.InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken)
   at System.CommandLine.Invocation.InvocationPipeline.InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken)

Further technical details

The error started showing up after installing Visual Studio 17.3, but it is also happening in WSL2 with the latest .NET SDK.

Activity

  1. ghost added
    untriagedRequest triage from a team member
    on Feb 12, 2025
  2. DavidZidar commented on Feb 12, 2025

    @DavidZidar
    Author

    I did try running dotnet format --verbosity diag but did not receive any additional actionable information.

  3. adam-knights commented on Feb 12, 2025

    @adam-knights

    Incase its useful in fixing this, we see similar today having taken 9.0.200, but on Windows only, our Mac arm64 users do not see the errors. May not apply to all cases, but if someone is looking at this and can't reproduce, try Windows.

  4. caleblloyd commented on Feb 12, 2025

    @caleblloyd

    I am hitting this on linux after the 9.0.200 upgrade today. Same deal, --verbosity diag doesn't show any other useful information. Reverting to 9.0.103 fixes.

  5. ChrisTTian667 commented on Feb 13, 2025

    @ChrisTTian667

    Incase its useful in fixing this, we see similar today having taken 9.0.200, but on Windows only, our Mac arm64 users do not see the errors. May not apply to all cases, but if someone is looking at this and can't reproduce, try Windows.

    I'm a Mac ARM 64 User and have the same issue. I don't think that its related to the platform.

  6. MattEdwardsWaggleBee commented on Feb 14, 2025

    @MattEdwardsWaggleBee

    I'm seeing this on my Windows 11 machine and my GitHub action running on "ubuntu-latest"

    update: I'm on 9.0.200 co-worker doesn't see this on 9.0.103

  7. added a commit that references this issue on Feb 15, 2025
  8. swharden commented on Feb 15, 2025

    @swharden

    Thanks for opening this issue and following-up with your suggestions!

    I'm here to report I fixed this by locking the dotnet version to 9.0.103 too, as shown in ScottPlot/ScottPlot@57c9b02

  9. david-driscoll commented on Feb 16, 2025

    @david-driscoll
    Contributor

    I'm also hitting this as well on a mac m4.

  10. Kiruyuto commented on Feb 17, 2025

    @Kiruyuto

    This happened to me today, Win11 and .NET 9.0.200.
    In my case, it was related to the fact that some files had UTF-8-BOM encoding which they should not (Per .editorconfig). Running dotnet format --severity info -v diag --verify-no-changes to locate them and then removing it via Rider fixed the usual dotnet format.

  11. 21 remaining items

  12. added 2 commits that reference this issue on Feb 27, 2025
    d3d694f
    76bb718
  13. added a commit that references this issue on Mar 11, 2025
    c641c52
  14. rubo commented on Mar 11, 2025

    @rubo

    When will the fix be released? v9.0.201 still has this problem.

  15. mattmc-jtb commented on Mar 11, 2025

    @mattmc-jtb

    Agreed rubo. I'm surprised this wasn't considered a critical fix since dotnet format has been effectively broken for a month.

  16. just-jim commented on Mar 12, 2025

    @just-jim

    So when should we expect to see the fix? In which version?

  17. JoeRobich commented on Mar 13, 2025

    @JoeRobich
    Member

    The fix merged into the 9.0.2xx branch and will be in the 9.0.202 SDK. I have verified the fix in the 9.0.300 nightly SDK (see https://github.com/dotnet/sdk/blob/main/documentation/package-table.md) build as well.

  18. dqwork commented on Mar 26, 2025

    @dqwork

    When would 9.0.202 be included with Visual Studio - Just updated to the latest (17.13.5) and only got SDK 9.0.201 which still has this bug

  19. added a commit that references this issue on Apr 18, 2025
  20. added a commit that references this issue on Apr 21, 2025
  21. mtmk commented on May 12, 2025

    @mtmk

    fyi I was having the same issue (even after removing BOM) and wasted about an hour before I found this issue.

    Upgrading to latest (as of today 9.0.203) fixed it for me.

    > dotnet --version
    9.0.203
    
  22. idgogoasa commented on May 15, 2025

    @idgogoasa

    Running
    dotnet format --verify-no-changes
    will list the files that need encoding changes
    Did that, fixed the encoding using Notepad++ and all is OK.

  23. jantrmer commented on May 23, 2025

    @jantrmer

    fyi I was having the same issue (even after removing BOM) and wasted about an hour before I found this issue.

    Upgrading to latest (as of today ) fixed it for me.9.0.203

    > dotnet --version
    9.0.203
    

    I was having the same issue, and downloading and installing SDK 9.0.300 fixed it. Release date: May 13, 2025. Thanks.

  24. added theissue type on Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions