microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.
Other
5.52k stars 1.55k forks source link

Implement support format request: provideDocumentRangesFormattingEdits #12082

Open ArnoGW1 opened 7 months ago

ArnoGW1 commented 7 months ago

Type: Bug

Formatting any simple 60-line C++ file is sometimes instant, sometimes so long that I give up and try again. I've seen it take 20 seconds or more.

This doesn't depend on contents; formatting randomly takes a long or short time when doing minor changes in the same small .cpp file.

Extension version: 1.19.6 VS Code version: Code 1.87.0 (Universal) (019f4d1419fbc8219a181fab7892ebccf7ee29a2, 2024-02-27T23:42:56.944Z) OS version: Darwin arm64 23.2.0 Modes: Remote OS version: Windows_NT x64 10.0.22631

System Info |Item|Value| |---|---| |CPUs|Apple M1 (8 x 24)| |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: enabled
webgl: enabled
webgl2: enabled
webgpu: enabled| |Load (avg)|2, 2, 3| |Memory (System)|16.00GB (0.08GB free)| |Process Argv|--crash-reporter-id 177e6a93-bd0d-4e86-b1ce-b463c9692a68| |Screen Reader|no| |VM|0%| |Item|Value| |---|---| |Remote|pc1| |OS|Windows_NT x64 10.0.22631| |CPUs|AMD Ryzen 9 5900X 12-Core Processor (24 x 3700)| |Memory (System)|63.93GB (36.37GB free)| |VM|0%|
A/B Experiments ``` vsliv368:30146709 vspor879:30202332 vspor708:30202333 vspor363:30204092 vstes627:30244334 vscorecescf:30445987 vscod805:30301674 binariesv615:30325510 vsaa593:30376534 py29gd2263:30899288 c4g48928:30535728 azure-dev_surveyone:30548225 962ge761:30959799 pythongtdpath:30769146 welcomedialogc:30910334 pythonidxpt:30866567 pythonnoceb:30805159 asynctok:30898717 pythontestfixt:30902429 pythonregdiag2:30936856 pyreplss1:30897532 pythonmypyd1:30879173 pythoncet0:30885854 pythontbext0:30879054 dsvsc016:30899300 dsvsc017:30899301 dsvsc018:30899302 pydisww1:30981707 cppperfcontrol:30979541 d34g3935:30971562 fegfb526:30981948 bg6jg535:30979843 ccp1r3:30958155 dsvsc020:30976470 ```
sean-mcmanus commented 7 months ago

This is caused by the formatting task getting stuck behind a long IntelliSense parsing operation. It's being tracked by https://github.com/microsoft/vscode-cpptools/issues/6156 already, although that issue mentions format on type and vcFormat, the same issue would repro with clang-format and not on type.

...unless you don't see the C/C++ task bar progress indicator showing "IntelliSense: Updating" (and/or cpptools-srv isn't using CPU).

ArnoGW1 commented 7 months ago

...unless you don't see the C/C++ task bar progress indicator showing "IntelliSense: Updating" (and/or cpptools-srv isn't using CPU).

I don't see this indicator. The problem happens no matter which IntelliSense operations are underway. If I wait for the open file to be fully parsed and for VSCode to do nothing, then save, the save operation can still hang for a long time.

sean-mcmanus commented 7 months ago

@ArnoGW1 Can you set C_Cpp.loggingLevel to "Debug" and look in the C/C++ logging to see what it shows when your format and the bug repros?

e.g. normally, you should see something like

LSP: (received) cpptools/formatDocument: file:///
LSP: (invoked) cpptools/formatDocument: file:///
Formatting document: file:///
Formatting Engine: clangFormat
LSP: Sending response (id: 47)

but if there are other messages then those could be contributing to the issue.

romanholidaypancakes commented 7 months ago

I also encountered this problem. I have been worried about the performance issues of vs (api components and the like), because as far as I know, we only need to call the clang-format executable file to format the source code. If this is the case The technical solution should be optimized very soon! Formatting using visual studio only takes a moment (also clang-format), and if there are syntax errors in the code, it will take a long time and fail.

ArnoGW1 commented 7 months ago

@ArnoGW1 Can you set C_Cpp.loggingLevel to "Debug" and look in the C/C++ logging to see what it shows when your format and the bug repros?

Copying a bit more than what you asked; this was with formatOnSave and saving took around 4 seconds in a 116-line file:

LSP: (invoked) cpptools/getCodeActions: file:///d%3A/projects/my_project/include/header_1.h (id: 278)
LSP: (invoked) textDocument/didChange: file:///d%3A/projects/my_project/include/header_1.h
LSP: (invoked) cpptools/didChangeTextEditorSelection
LSP: (invoked) cpptools/getCodeActions: file:///d%3A/projects/my_project/include/header_1.h (id: 279)
LSP: Sending response (id: 279)
LSP: (invoked) cpptools/getFoldingRanges: file:///d%3A/projects/my_project/include/header_1.h (id: 280)
LSP: (invoked) cpptools/formatRange: file:///d%3A/projects/my_project/include/header_1.h (id: 283)
Formatting document: file:///d%3A/projects/my_project/include/header_1.h
Formatting Engine: clangFormat
LSP: Sending response (id: 280)
c:\Users\user\.vscode-server\extensions\ms-vscode.cpptools-1.19.6-win32-x64/bin/../LLVM/bin/clang-format.exe -style=file -fallback-style=LLVM --Wno-error=unknown -offset=4956 -length=84 -assume-filename=D:\projects\my_project\header_1.h
LSP: Sending response (id: 283)
LSP: (received) cpptools/formatRange: file:///d%3A/projects/my_project/include/header_1.h (id: 284)
LSP: (invoked) cpptools/formatRange: file:///d%3A/projects/my_project/include/header_1.h (id: 284)
Formatting document: file:///d%3A/projects/my_project/include/header_1.h
Formatting Engine: clangFormat
c:\Users\user\.vscode-server\extensions\ms-vscode.cpptools-1.19.6-win32-x64/bin/../LLVM/bin/clang-format.exe -style=file -fallback-style=LLVM --Wno-error=unknown -offset=5301 -length=148 -assume-filename=D:\projects\my_project\header_1.h
LSP: Sending response (id: 284)
LSP: (received) textDocument/willSaveWaitUntil: file:///d%3A/projects/my_project/include/header_1.h (id: 285)
LSP: (invoked) textDocument/willSaveWaitUntil: file:///d%3A/projects/my_project/include/header_1.h (id: 285)
LSP: Sending response (id: 285)
willSaveWaitUntil: 0ms

Note that my settings contain:

    "[cpp]": {
        "editor.formatOnType": true,
        "editor.formatOnSave": true,
        "editor.formatOnSaveMode": "modificationsIfAvailable"
    },

But I remember having that issue before adding formatOnSaveMode.

ArnoGW1 commented 7 months ago

Note that it sometimes doesn't finish at all. It's been happening a lot lately, which is blocking. Here's the log:

Formatting document: file:///d%3A/projects/my_project/include/header_1.h
Formatting Engine: clangFormat
c:\Users\user\.vscode-server\extensions\ms-vscode.cpptools-1.19.6-win32-x64/bin/../LLVM/bin/clang-format.exe -style=file -fallback-style=LLVM --Wno-error=unknown -offset=5443 -length=96 -assume-filename=D:\projects\my_project\header_1.h
LSP: Sending response (id: 812)
ArnoGW1 commented 7 months ago

I just had a formatting failure when saving a small change, not sure it's related:

LSP: (received) cpptools/formatRange: file:///d%3A/projects/my_project/src/src_1.cpp (id: 1435)
LSP: (invoked) cpptools/formatRange: file:///d%3A/projects/my_project/src/src_1.cpp (id: 1435)
Formatting document: file:///d%3A/projects/my_project/src/src_1.cpp
Formatting Engine: clangFormat
Formatting failed:
c:\Users\user\.vscode-server\extensions\ms-vscode.cpptools-1.19.7-win32-x64/bin/../LLVM/bin/clang-format.exe -style=file -fallback-style=LLVM --Wno-error=unknown -offset=10906 -length=0 -assume-filename=D:\projects\my_project\src\src_1.cpp D:\PROJECTS\MY_PROJECT\SRC\SRC_1.CPP
error: offset 10906 is outside the file

LSP: Sending response (id: 1435)
LSP: (received) cpptools/fileChanged: file:///d%3A/projects/my_project/src/src_1.cpp
LSP: (invoked) cpptools/fileChanged: file:///d%3A/projects/my_project/src/src_1.cpp
LSP: (received) textDocument/didSave: file:///d%3A/projects/my_project/src/src_1.cpp
LSP: (invoked) textDocument/didSave: file:///d%3A/projects/my_project/src/src_1.cpp
sean-mcmanus commented 7 months ago

@ArnoGW1 Is the id of the LSP: Sending response (id: 812) when formatting gets permanently stuck the id of the formatting message? We have one known IntelliSense deadlock at https://github.com/microsoft/vscode-cpptools/issues/12051 which should be fixed with our pending 1.19.8 release. After the formatting gets stuck, are most other operations stuck?

So is the issue just with formatOnSave or do you see an issue when invoking format document too?? I'm seeing 4 formatRange requests being sent when formatOnSave is invoked.

Do you have any more repro details for the "error' offset is outside the file"?

sean-mcmanus commented 7 months ago

@ArnoGW1 When a file has many edits with "editor.formatOnSaveMode": "modificationsIfAvailable", I've gotten VS Code to send 100+ formatRange requests which can take a very long time -- do you want us to create a separate issue to track that or use this issue? I'm unclear whether the issue you're hitting is due to these multiple formatRange requests or if there's a separate issue you want us to track with this issue that can repro with just a single formatRange being processed.

ArnoGW1 commented 7 months ago

@ArnoGW1 When a file has many edits with "editor.formatOnSaveMode": "modificationsIfAvailable", I've gotten VS Code to send 100+ formatRange requests which can take a very long time -- do you want us to create a separate issue to track that or use this issue? I'm unclear whether the issue you're hitting is due to these multiple formatRange requests or if there's a separate issue you want us to track with this issue that can repro with just a single formatRange being processed.

There were very few changes, less than 10 lines in a 160-line file. I will answer your other questions when I have time to investigate.