Skip to content

Code formatting runs unexpected sometimes #8284

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
L2750558108 opened this issue May 13, 2025 · 8 comments
Open

Code formatting runs unexpected sometimes #8284

L2750558108 opened this issue May 13, 2025 · 8 comments

Comments

@L2750558108
Copy link

L2750558108 commented May 13, 2025

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

eab416aea4c1addd1dbdd7dd25ef2bd5.mp4

Expected behavior

N/A

Actual behavior

N/A

Additional context

N/A

@melonmouse
Copy link

melonmouse commented May 16, 2025

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.

@barruka
Copy link

barruka commented May 18, 2025

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.

@dibarbet
Copy link
Member

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.

@L2750558108
Copy link
Author

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

@JoeRobich
Copy link
Member

@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
}

@L2750558108
Copy link
Author

L2750558108 commented May 21, 2025

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

@JoeRobich
Copy link
Member

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

@L2750558108
Copy link
Author

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

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

No branches or pull requests

5 participants