Repository navigation
Native Server incorrectly formats notebook code when using source.organizeImports #640
Description
Activity
Do you have any other extension that support organizing imports? Can you try using
source.organizeImports.ruffin your VS Code settings instead so that the client only sends the request to Ruff extension?I don't have any other extensions that would format python/notebooks - I have prettier for js/ts but the problem is still there with that disabled.
Updating the settings to add what you mentioned doesn't fix it either, still the exact same problem.
I wonder if this is a problem because we process both the format and organize imports requests concurrently (we don't synchronize on the document). VS code then e.g. updates the format changes first before applying the organize import actions or the other way round. I expected the format response to contain the document version but it doesn't.
Maybe format should use
TextDocumentEditinstead of theTextEditreturn type?Same for code actions. There's also this comment
I wonder if this is a problem because we process both the format and organize imports requests concurrently (we don't synchronize on the document). VS code then e.g. updates the format changes first before applying the organize import actions or the other way round. I expected the format response to contain the document version but it doesn't.
If this is the case, then it should be true for Python files as well, right? Or does this problem not exists there because we use versioned edits there?
Oh ok, I see the problem. Can you use
notebook.source.organizeImports.ruffin yournotebook.codeActionsOnSaveconfig?The difference between the two is that for
source.organizeImports.ruff, VS Code will send parallel requests to the server for each cell which is where the problem lies. For this specific reason, the VS Code team added support fornotebook.*prefixed code actions so that it only sends the request once for the entire notebook.Yeah that seems to fix it, thanks!
Sorry for raising this since it wasn't actually an issue (and I've just found documentation that would've fixed it myself...), but thank you anyway for responding so quickly and helping me!
No worries, glad to help. I'll mark this as resolved but feel free to ask any other questions that you might have.
- addedquestionAsking for support or clarificationAsking for support or clarification
on Nov 7, 2024 - changed the title
[-]Native Server incorrectly formats notebook code with formatOnSave enabled and organizeImports enabled[/-][+]Native Server incorrectly formats notebook code with `source.organizeImports` enabled[/+]on Nov 7, 2024 - changed the title
[-]Native Server incorrectly formats notebook code with `source.organizeImports` enabled[/-][+]Native Server incorrectly formats notebook code when using `source.organizeImports`[/+]on Nov 7, 2024
I noticed that when trying to enable ruff formatting in Jupyter notebooks with import sorting enabled as well, the Ruff native server doesn't correctly format the cells, leading to garbled imports and sometimes completely missing lines of code.
When I change
"ruff.nativeServer": trueto"ruff.nativeServer": falseto disable the native server, format on save works correctly without any problem.ruff.toml:.vscode/settings.json{ "editor.formatOnSave": true, "[python]": { "editor.defaultFormatter": "charliermarsh.ruff", "editor.codeActionsOnSave": { "source.organizeImports": "explicit" } }, "ruff.nativeServer": true, "notebook.formatOnSave.enabled": true, "notebook.codeActionsOnSave": { "source.organizeImports": "explicit" }, "notebook.defaultFormatter": "charliermarsh.ruff", "ruff.trace.server": "verbose" }Environment details:
Extension version:
v2024.54.0Extension uses Ruff version:
0.7.1VS Code version:
1.95.1OS: MacOS Sequoia
15.1Python Version:
3.12.7Code sample:
A new blank notebook with this example code in 2 cells:
Note: the code doesn't have to be the same in both cells, and it doesn't have to just be 2 cells in the notebook, but this is a minimal example I've found where the issue happens. The issue doesn't seem to happen when there is just 1 cell in the notebook.
Raw json contents of notebook:
```json { "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from typing import Optional\n", "from typing import Literal\n", "from typing import TypedDict\n", "\n", "# Comment\n", "Test = Literal[\"test\"]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from typing import Optional\n", "from typing import Literal\n", "from typing import TypedDict\n", "\n", "# Comment\n", "Test = Literal[\"test\"]" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.12.7" } }, "nbformat": 4, "nbformat_minor": 2 } ```Extension logs: https://pastebin.com/pvXc9HZY
Video (with native server enabled):
Screen.Recording.2024-11-07.at.10.08.45.mov
Video (with native server disabled):
Screen.Recording.2024-11-07.at.10.10.51.mov
If you need any more information or need to clarify anything else with me please let me know, thanks.