Skip to content

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

@ethancowardhive

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.

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