Skip to content

dotnet format doesn't write to disk after updating to dotnet SDK 9.0.200 #47012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ferrarimarco opened this issue Feb 21, 2025 · 7 comments
Open
Assignees
Labels
untriaged Request triage from a team member

Comments

@ferrarimarco
Copy link

Describe the bug

dotnet format doesn't write formatting changes back to the file after updating the dotnet SDK to 9.0.200. It works up to 9.0.102.

To Reproduce

  1. Create a file named csharp_bad_01.cs in the current working directory with the following contents (ensure to respect spaces and blank lines):
using System;

namespace HelloWorld {
  class Program {
    static void Main(string[] args) {
      Console.WriteLine("Hello World");
    }
  }
}
  1. Run docker run --rm -it --entrypoint /bin/ash -v $(pwd):/app mcr.microsoft.com/dotnet/sdk:9.0.200-alpine3.21

  2. Run cd /app

  3. Hash the content of the file: sha256sum csharp_bad_01.cs. Output:

813c093e202045f5f6c88ab88e988aafd2120feb6479b0e667717714d1e4e39d  csharp_bad_01.cs
  1. Run dotnet format whitespace --folder --exclude / --include csharp_bad_01.cs -v diag --verify-no-changes. Output:
  Formatting code files in workspace '/app/'.
  Loading workspace.
  Complete in 1271ms.
  Determining formattable files.
  Complete in 191ms.
  Running formatters.
/app/csharp_bad_01.cs(3,21): error WHITESPACE: Fix whitespace formatting. Replace 1 characters with '\n'. [/app//]
/app/csharp_bad_01.cs(4,3): error WHITESPACE: Fix whitespace formatting. Insert '\s\s'. [/app/]
/app/csharp_bad_01.cs(4,16): error WHITESPACE: Fix whitespace formatting. Replace 1 characters with '\n\s\s\s\s'. [/app/]
/app/csharp_bad_01.cs(5,5): error WHITESPACE: Fix whitespace formatting. Insert '\s\s\s\s'. [/app/]
/app/csharp_bad_01.cs(5,36): error WHITESPACE: Fix whitespace formatting. Replace 1 characters with '\n\s\s\s\s\s\s\s\s'. [/app/]
/app/csharp_bad_01.cs(6,7): error WHITESPACE: Fix whitespace formatting. Insert '\s\s\s\s\s\s'. [/app/]
/app/csharp_bad_01.cs(7,5): error WHITESPACE: Fix whitespace formatting. Insert '\s\s\s\s'. [/app/]
/app/csharp_bad_01.cs(8,3): error WHITESPACE: Fix whitespace formatting. Insert '\s\s'. [/app/]
  Complete in 436ms.
  Formatted code file '/app/csharp_bad_01.cs'.
  Formatted 1 of 1 files.
  Format complete in 1899ms.
/app/test/linters/csharp # exit
exit status 2
  1. Run dotnet format whitespace --folder --exclude / --include csharp_bad_01.cs -v diag. Output:
  Formatting code files in workspace '/app/'.
  Loading workspace.
  Complete in 1257ms.
  Determining formattable files.
  Complete in 191ms.
  Running formatters.
  Complete in 467ms.
  Formatted code file '/app/csharp_bad_01.cs'.
  Formatted 1 of 1 files.
  Format complete in 1978ms.
  1. Hash the content of the file: sha256sum csharp_bad_01.cs, observing that it has the same hash as before, and the same content. Output:
813c093e202045f5f6c88ab88e988aafd2120feb6479b0e667717714d1e4e39d  csharp_bad_01.cs

If you repeat the steps described above using the 9.0.102-alpine3.21 container image, you can observe that dotnet format correctly writes the formatted file.

Exceptions (if any)

Further technical details

  • Include the output of dotnet --info:
/ # dotnet --info
.NET SDK:
 Version:           9.0.200
 Commit:            90e8b202f2
 Workload version:  9.0.200-manifests.b4a8049f
 MSBuild version:   17.13.8+cbc39bea8

Runtime Environment:
 OS Name:     alpine
 OS Version:  3.21
 OS Platform: Linux
 RID:         linux-musl-x64
 Base Path:   /usr/share/dotnet/sdk/9.0.200/

.NET workloads installed:
There are no installed workloads to display.
Configured to use loose manifests when installing new manifests.

Host:
  Version:      9.0.2
  Architecture: x64
  Commit:       80aa709f5d

.NET SDKs installed:
  9.0.200 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 9.0.2 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 9.0.2 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  None

Environment variables:
  Not set

global.json file:
  Not found

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

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

Also, not sure if #46780 is related to this.

@ghost ghost added the untriaged Request triage from a team member label Feb 21, 2025
@ghost
Copy link

ghost commented Feb 21, 2025

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

1 similar comment
@ghost
Copy link

ghost commented Feb 21, 2025

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@jdaigle
Copy link

jdaigle commented Feb 25, 2025

I've run into this exact same issue. Reproduced on MacOS.

@edvilme
Copy link
Contributor

edvilme commented Mar 12, 2025

Hello @dotnet/roslyn-ide do we have some information on this?
cc @sharwell

@JoeRobich
Copy link
Member

JoeRobich commented Mar 12, 2025

This issue is caused by the same null Encoding Roslyn bug as #46780. It only affects saving files when using the --folder option.

protected override void ApplyDocumentTextChanged(DocumentId documentId, SourceText text)
{
var document = CurrentSolution.GetDocument(documentId);
if (document?.FilePath != null && text.Encoding != null)
{
SaveDocumentText(documentId, document.FilePath, text, text.Encoding);
OnDocumentTextChanged(documentId, text, PreservationMode.PreserveValue);
}
}

A fix has been merged that should flow into the 9.0.2xx SDK (see dotnet/roslyn#77362). I have confirmed that the issue is fixed in the nightly .NET 10 build.

@JoeRobich
Copy link
Member

Verified that the nightly 9.0.300 build works, also found the insertion of the fix into the 9.0.2xx branch (See #46931)

@edvilme
Copy link
Contributor

edvilme commented Mar 13, 2025

Thank for the confirmation @JoeRobich

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
untriaged Request triage from a team member
Projects
None yet
Development

No branches or pull requests

4 participants