dotnet / dotnet/vscode-csharp

Code formatter sometimes inserts close braces in weird places when code is not valid C#

Open
#8,460 0 comments 0 reactions 0 assignees View on GitHub
Bug Editor-Formatting
Dominant language
TypeScript
Stars
3.1k
Forks
737
Avg merge
17h 56m
Merged PRs (30d)
21

Description

Type: Bug

## Issue Description ##

I have been translating a project from platform-independent C to C#. In order to do this, one by one, I am copy/pasting the C code into the body of a C# class and then going through the file, translating the code from C structures and idioms to the corresponding C#. Sometimes, though, I randomly encounter a close brace in the middle of code that was not originally there. Today, I pared it down to figure out exactly where it's coming from.

## Steps to Reproduce ##

There is a specific conjunction of conditions that produces this behaviour:

* If the file is in a `namespace`.
* ...and the class is implementing an abstract base class
* ...and the code after the cursor contains at least two function definitions
* ...and both of the function definitions include the word `struct` within the parameter lists (common in C)
* ...and the first function definition contains at least one line with open and close parentheses
* ...and the cursor is before these functions
* ...and the user implements an abstract property of type `string` from the base class
* ...and this implementation is provided by a completion from the C# extension.

Then,

* A stray close brace is inserted before the word `struct` in the second function's argument list.

[Demonstration Video](https://youtu.be/UmU7gC7wGAs)

## Expected Behavior ##

No extra, unnecessary brace is inserted.

## Actual Behavior ##

A brace is inserted that isn't syntactically-correct under any circumstance.

```
void f3(}

struct);
```

## Logs ##

[exthost.zip](https://github.com/user-attachments/files/21472155/exthost.zip)

### C# log ###

[C#Trace.txt](https://github.com/user-attachments/files/21472095/C.Trace.txt)

### C# LSP Trace Logs ###

[LSPTrace.txt](https://github.com/user-attachments/files/21472096/LSPTrace.txt)

## Environment information ##

**VSCode version**: 1.96.4
**C# Extension**: 2.84.19
**Using OmniSharp**: false

Dotnet Information
.NET SDK:
Version: 10.0.100-preview.6.25358.103
Commit: 75972a5ba7
Workload version: 10.0.100-manifests.b6c7f53e
MSBuild version: 17.15.0-preview-25358-103+75972a5ba

Runtime Environment:
OS Name: ubuntu
OS Version: 24.04
OS Platform: Linux
RID: linux-x64
Base Path: /usr/lib/dotnet/sdk/10.0.100-preview.6.25358.103/

.NET workloads installed:
There are no installed workloads to display.
Configured to use workload sets when installing new manifests.
Workloads are configured to install and update using workload versions, but none were found. Run "dotnet workload restore" to install a workload version.

Host:
Version: 10.0.0-preview.6.25358.103
Architecture: x64
Commit: 75972a5ba7

.NET SDKs installed:
8.0.117 [/usr/lib/dotnet/sdk]
10.0.100-preview.6.25358.103 [/usr/lib/dotnet/sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.17 [/usr/lib/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 10.0.0-preview.6.25358.103 [/usr/lib/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 8.0.17 [/usr/lib/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.0-preview.6.25358.103 [/usr/lib/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

Visual Studio Code Extensions

|Extension|Author|Version|Folder Name|
|---|---|---|---|
|cmake-tools|ms-vscode|1.21.36|ms-vscode.cmake-tools-1.21.36|
|cpptools|ms-vscode|1.26.3|ms-vscode.cpptools-1.26.3-linux-x64|
|cpptools-extension-pack|ms-vscode|1.3.1|ms-vscode.cpptools-extension-pack-1.3.1|
|cpptools-themes|ms-vscode|2.0.0|ms-vscode.cpptools-themes-2.0.0|
|csdevkit|ms-dotnettools|1.30.44|ms-dotnettools.csdevkit-1.30.44-linux-x64|
|csharp|ms-dotnettools|2.84.19|ms-dotnettools.csharp-2.84.19-linux-x64|
|pdf|tomoki1207|1.2.2|tomoki1207.pdf-1.2.2|
|vscode-dotnet-runtime|ms-dotnettools|2.3.2|ms-dotnettools.vscode-dotnet-runtime-2.3.2|;

Extension version: 2.84.19
VS Code version: Code 1.96.4 (cd4ee3b1c348a13bafd8f9ad8060705f6d4b9cba, 2025-01-16T00:16:19.038Z)
OS version: Linux x64 6.8.0-63-generic
Modes:

System Info

|Item|Value|
|---|---|
|CPUs|AMD Ryzen 7 7840HS w/ Radeon 780M Graphics (16 x 4483)|
|GPU Status|2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: disabled_software
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: disabled_off
webnn: disabled_off|
|Load (avg)|2, 2, 2|
|Memory (System)|30.66GB (3.93GB free)|
|Process Argv|. --crash-reporter-id c84b78fa-4a64-44f9-82b7-5288ac2ecc62|
|Screen Reader|no|
|VM|0%|
|DESKTOP_SESSION|plasma|
|XDG_CURRENT_DESKTOP|KDE|
|XDG_SESSION_DESKTOP|plasma|
|XDG_SESSION_TYPE|x11|

A/B Experiments

```
vsliv368:30146709
binariesv615:30325510
2e7ec940:31000449
nativeloc1:31344060
dwcopilot:31170013
6074i472:31201624
dwoutputs:31242946
hdaa2157:31222309
copilot_t_ci:31333650
pythoneinst12:31285622
c7cif404:31314491
pythonpulldiag:31343502
996jf627:31283433
pythonrdcb7:31342333
usemplatestapi:31297334
0aa6g176:31307128
aj953862:31281341
generatesymbolt:31295002
convertfstringf:31295003
pylancequickfixt:31350061
usemarketplace:31343026
nes-diff-11:31337487
onetestforazureexpcf:31335614
6abeh943:31336334
yijiwantestdri0626-c:31336931
0927b901:31350571
ji9b5146:31348712
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.