vscode-csharp icon indicating copy to clipboard operation
vscode-csharp copied to clipboard

Code formatting runs unexpected sometimes

Open L2750558108 opened this issue 8 months ago • 8 comments

Environment data

dotnet --info output: .NET SDK: Version: 9.0.202 Commit: 3a53853c30 Workload version: 9.0.200-manifests.21502d11 MSBuild version: 17.13.13+1c2026462

运行时环境: OS Name: Windows OS Version: 10.0.19045 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\9.0.202\

已安装 .NET 工作负载: 没有要显示的已安装工作负载。 配置为在安装新清单时使用 loose manifests。

Host: Version: 9.0.3 Architecture: x64 Commit: 831d23e561

.NET SDKs installed: 6.0.405 [C:\Program Files\dotnet\sdk] 7.0.400 [C:\Program Files\dotnet\sdk] 8.0.302 [C:\Program Files\dotnet\sdk] 9.0.202 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.App 6.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 9.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 6.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 9.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 6.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.21 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 9.0.3 [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 VS Code version:1.100.0 C# Extension version:2.72.34

C# logs

C# output window contents:Empty

See this page for collecting more detailed logs

Steps to reproduce

  1. Open a C# project
  2. Install C# extension
  3. Open a .cs file
  4. Right click and select source action
  5. Wait it to tell you "no code action avaliable" and see the file is unexpectly formatted

This also happend when using the extract interface and move to file refactor options

https://github.com/user-attachments/assets/9afc1cb0-1fad-40d2-a743-352d9ab877d5

Expected behavior

N/A

Actual behavior

N/A

Additional context

N/A

L2750558108 avatar May 13 '25 12:05 L2750558108

Have the same issue, vscode (1.100.1) started indenting the outer namespace recently (no more than a week).

It seems that an interaction between the C# (2.76.27) extension and vscode (1.100.1) is causing the problem. In addition to the C# (2.76.27) extension, I'm using C# Dev Kit (1.19.63) and Unity (1.1.2).

The bug is not present:

  • in C++
  • When disabling the C# extension and stuff depending on it; C# Dev Kit and Unity

The bug is present:

  • If the C# extension is enabled, but C# Dev Kit and/or Unity are disabled.
  • When downgrading the C#, C# Dev Kit and Unity extensions (to versions from ~3 months ago).
  • When upgrading the C#, C# Dev Kit and Unity extensions to the pre-release.
  • When C#, C# Dev Kit and Unity are current, and "editor.autoIndent" is set "none".

I have not tried downgrading vscode itself.

melonmouse avatar May 16 '25 14:05 melonmouse

Yeah, totally right, 5/7 days ago vsCode was running OK. With last version and latest versions of all extensions with GitHub Copilot enabled, this is a non sense.

I have a solution with 5 projects, almost 600 files.

Each time I press CTRL+S to save the file, it takes from 2 to 8 seconds to just format the document!!!

Also copilot suggestions take from 4 to 6 seconds to show up.

If there is a mistake on the c# syntax, it takes up to 30 seconds to mark it with the bottom red line.

I've been working on this very project for the last 12 months, and 5/7 days ago there was no problem, but the moment I've installed last version of vscode and upgrade all extensions, this is awful.

barruka avatar May 18 '25 10:05 barruka

Can you confirm if VSCode 1.100.2 also reproduces the issue? If anyone has a project they can as well, please send it our way. I have not been able to trigger formatting to run unexpectedly.

Additionally, if you could collect the trace level Extension Host logs:

  1. Set the log level to 'Trace' Image
  2. Share the contents of the 'Extension Host' output window and the C# LSP trace logs (see https://github.com/dotnet/vscode-csharp/blob/main/SUPPORT.md#c-lsp-trace-logs) that may help us track down what is causing format to run when it shouldn't.

dibarbet avatar May 21 '25 00:05 dibarbet

Can you confirm if VSCode 1.100.2 also reproduces the issue? If anyone has a project they can as well, please send it our way. I have not been able to trigger formatting to run unexpectedly.

Additionally, if you could collect the trace level Extension Host logs:

  1. Set the log level to 'Trace' Image
  2. Share the contents of the 'Extension Host' output window and the C# LSP trace logs (see https://github.com/dotnet/vscode-csharp/blob/main/SUPPORT.md#c-lsp-trace-logs) that may help us track down what is causing format to run when it shouldn't.

Tested in 1.100.2 and still exists https://github.com/user-attachments/assets/0879e205-59a6-4294-adc1-06ab82e8d575

A newly created project like this can also trigger the unexpected formatting running so I dont think its related to project Image

These are extension host log. The C# log window is empty and nothing to send 扩展宿主.log

L2750558108 avatar May 21 '25 01:05 L2750558108

@L2750558108 You are invoking the Source Action command. The RoslynLSP does not use the "source" code action kinds, so I would expect this to always be empty. However, these lines in your extension host might be a clue as to what is going on.

2025-05-21 09:51:39.915 [trace] CommandsConverter#DISPOSE omnisharp.runCodeAction /72
2025-05-21 09:51:39.915 [trace] CommandsConverter#DISPOSE omnisharp.runCodeAction /73
2025-05-21 09:51:39.915 [trace] CommandsConverter#DISPOSE omnisharp.runCodeAction /74
2025-05-21 09:51:39.915 [trace] CommandsConverter#DISPOSE omnisharp.runCodeAction /75
2025-05-21 09:51:39.915 [trace] CommandsConverter#DISPOSE omnisharp.runCodeAction /76
2025-05-21 09:51:39.915 [trace] CommandsConverter#DISPOSE omnisharp.runCodeAction /77
2025-05-21 09:51:39.915 [trace] CommandsConverter#DISPOSE omnisharp.runCodeAction /78
2025-05-21 09:51:39.915 [trace] CommandsConverter#DISPOSE omnisharp.runCodeAction /79
2025-05-21 09:51:39.915 [trace] CommandsConverter#DISPOSE omnisharp.runCodeAction /80
2025-05-21 09:51:39.915 [trace] CommandsConverter#DISPOSE omnisharp.runCodeAction /81
2025-05-21 09:51:39.915 [trace] CommandsConverter#DISPOSE omnisharp.runCodeAction /82
2025-05-21 09:51:39.915 [trace] CommandsConverter#DISPOSE omnisharp.runCodeAction /83
2025-05-21 09:51:39.915 [trace] CommandsConverter#DISPOSE omnisharp.runCodeAction /84

Are you using OmniSharp? OmniSharp supports a source.fixAll.csharp code action which will apply all code actions for the open document.

If you are using OmniSharp, can you also check your VS Code settings to ensure you don't have configuration like the following.

"editor.codeActionsOnSave": {
  "source.fixAll.csharp": true
}

JoeRobich avatar May 21 '25 02:05 JoeRobich

Are you using OmniSharp? OmniSharp supports a code action which will apply all code actions for the open document.source.fixAll.csharp

Yes

If you are using OmniSharp, can you also check your VS Code settings to ensure you don't have configuration like the following.

"editor.codeActionsOnSave": { "source.fixAll.csharp": true }

I checked and didnt find config like this

L2750558108 avatar May 21 '25 14:05 L2750558108

Does this only happen when you use the "Source Action" context menu?

JoeRobich avatar May 21 '25 14:05 JoeRobich

No, also happens when i use "Extract Interface" or "Move to file" refactor options. But these cases seems different, the unexpected formatting trigger by "Source Action" wont respect the format config in omnisharp.json file, but these two cases will

L2750558108 avatar May 21 '25 15:05 L2750558108