Repository: astral-sh/ruff-vscode

Issue #593: Notebook (`notebook.*`) code actions does not work when first cell is markdown
Created: 2024-08-26T21:08:45Z
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

Duplicate issue #722: formatOnSave not working on notebook
Created: 2025-03-17T17:58:21Z
Description:
I have tried some configurations to make ruff work with notebook on vscode, what I have achieved so far is just well ruff format the code ( indentation)
mi current Setup/Environment:

MacBook M1
ruff with brew ( ruff 0.11.0 )
vscode ruff extension ( 2025.18.0)
using vscode setting to setup ruff rules
vscode (1.98.2)

settings.json
// #region Python
  "[python]": {
    "editor.defaultFormatter": "charliermarsh.ruff",
    "editor.formatOnSave": true,

    // fix lint violation on-save
    "editor.codeActionsOnSave": {
      "source.fixAll": "explicit",
      "source.organizeImports": "explicit"
    }
  },
  "notebook.formatOnSave.enabled": true,
  "notebook.codeActionsOnSave": {
    "notebook.source.fixAll": "explicit",
    "notebook.source.organizeImports": "explicit"
  },
  "python.defaultInterpreterPath": "/opt/homebrew/bin/python3",
  "python.analysis.typeCheckingMode": "off",
also tried to use [jupyter] but that doesn't worked

I created my project with uv init . so I have pyproject.toml
added

[tool.ruff]
extend-include = ["*.ipynb"]

[tool.ruff.lint]
extend-safe-fixes = ["E", "F401"]
but that doesn't work either
Expected Results
I expected ruff to remove unused imports with cmd + s ← formats all my code, all cells I expected just to format on active cell
Actual Results

Python (.py)

cmd + s: remove unused imports, format code (fix all)
option + shift + f: format code


Notebook (.ipynb)

cmd + s: format all cells
option + shift + f: format active cell

Closing comment (2025-03-19T11:54:12Z): Yeah, so this is same as #593 as the notebook in the repository that you've linked does contain markdown cells at the top. I'll close this in favor of that issue. Thanks again for reporting.