Skip to content

Native Server incorrectly formats notebook code when using source.organizeImports #640

Description

@ethancowardhive

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": true to "ruff.nativeServer": false to disable the native server, format on save works correctly without any problem.

ruff.toml:

line-length = 120

[lint]
extend-safe-fixes = ["E712", "E711"]

.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.0
Extension uses Ruff version: 0.7.1
VS Code version: 1.95.1
OS: MacOS Sequoia 15.1
Python Version: 3.12.7

Code sample:

A new blank notebook with this example code in 2 cells:

from typing import Optional
from typing import Literal
from typing import TypedDict

# Comment
Test = Literal["test"]

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.

Activity

  1. dhruvmanila commented on Nov 7, 2024

    @dhruvmanila
    Member

    Do you have any other extension that support organizing imports? Can you try using source.organizeImports.ruff in your VS Code settings instead so that the client only sends the request to Ruff extension?

  2. ethancowardhive commented on Nov 7, 2024

    @ethancowardhive
    Author

    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.

  3. MichaReiser commented on Nov 7, 2024

    @MichaReiser
    Member

    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.

  4. MichaReiser commented on Nov 7, 2024

    @MichaReiser
    Member

    Maybe format should use TextDocumentEdit instead of the TextEdit return type?

  5. MichaReiser commented on Nov 7, 2024

    @MichaReiser
    Member
  6. dhruvmanila commented on Nov 7, 2024

    @dhruvmanila
    Member

    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?

  7. dhruvmanila commented on Nov 7, 2024

    @dhruvmanila
    Member

    Oh ok, I see the problem. Can you use notebook.source.organizeImports.ruff in your notebook.codeActionsOnSave config?

  8. dhruvmanila commented on Nov 7, 2024

    @dhruvmanila
    Member

    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 for notebook.* prefixed code actions so that it only sends the request once for the entire notebook.

  9. ethancowardhive commented on Nov 7, 2024

    @ethancowardhive
    Author

    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!

  10. dhruvmanila commented on Nov 7, 2024

    @dhruvmanila
    Member

    No worries, glad to help. I'll mark this as resolved but feel free to ask any other questions that you might have.

  11. 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
  12. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionAsking for support or clarification

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions