Skip to content

Notebook (notebook.*) code actions does not work when first cell is markdown #593

Open
@bolliger32

Description

@bolliger32

It took me a little while, but I finally figured out the use of notebook.source.fixAll and notebook.source.organizeImports settings (instead of their source.* counterparts) to prevent vscode from turning my import cells in jupyter notebooks into gibberish when using ruff as the formatter. However, it appears that when a notebook starts with a markdown cell (say, to title the notebook) and then has a python cell with imports after the markdown, notebook.source.organizeImports doesn't work. Here are my relevant vscode settings:

{
    "editor.defaultFormatter": "charliermarsh.ruff",
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": "explicit",
        "source.fixAll": "explicit"
    },
    "notebook.defaultFormatter": "charliermarsh.ruff",
    "notebook.formatOnSave.enabled": true,
    "notebook.codeActionsOnSave": {
        "notebook.source.organizeImports": "explicit",
        "notebook.source.fixAll": "explicit"
    }
}

And here's the relevant part of my pyproject.toml file in the workspace open in vscode:

[tool.ruff.lint]
unfixable = ["F401"]
select = ["E4", "E7", "E9", "F", "NPY", "I"]

[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["E402"]

[tool.ruff.format]
docstring-code-format = true

I'm using ruff 0.6.2.

Side Note:
If I use source.organizeImports instead of notebook.source.organizeImports, it "works" in the sense that something happens, but the effect is to introduce gibberish. Here's an example of the "gibberish" that occurs when using source.* instead of notebook.source.*. The first screenshot is from "pre-save", the second is after saving once, and the third after saving a second time
image
image
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcode-actionsRelated to code actionsupstream

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions