dotnet / dotnet/sdk

`dotnet format` command replaces some line endings when comments precede multi-line strings

Open
#52,249 0 comments 0 reactions 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

When comment exists before C# multi-line string.
`dotnet format` command replace some line endings unexpectedly.

**Example code to format**
```csharp
public void Test()
{
var text =
// Comment before multiline string.
"""
line1
line2
line3
""";
}
```

### To Reproduce
Run following steps on Windows environment with .NET 10 SDK.

1. Create console app with `dotnet new console` command
2. Edit `Program.cs` and add sample code described above.
3. Run `dotnet format --verify-no-changes -v diag` and confirms there is no diff.
4. Switch to WSL2 (Ubuntu) and run same command. and confirms errors are shown.
```
/mnt/c/Temp/ConsoleApp/Program.cs(4,11): error WHITESPACE: Fix whitespace formatting. Replace 10 characters with '\n\s\s\s\s\s\s\s\s'. [/mnt/c/Temp/ConsoleApp/ConsoleApp.csproj]
/mnt/c/Temp/signoz/ConsoleApp.cs(5,44): error WHITESPACE: Fix whitespace formatting. Replace 10 characters with '\n\s\s\s\s\s\s\s\s'. [/mnt/c/Temp/ConsoleApp/ConsoleApp.csproj]
```

When running without `--verify-no-changes` option.
It replace line endings before and after comment line.
And it cause mixed line encodings (CRLF/LF)

It's not expected line endings are modified by `dotnet format` command
when `end_of_line` is not specified by `.editorconfig` explicitly.

### Exceptions (if any)
It seems caused when using .NET 10 SDK.
When running on .NET 9. It's not occurred.

### Further technical details
I've tested with .NET 10 SDK `10.0.101`.

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.