astral-sh/ruff-vscode

Broken jupyter notebook cell formatting.

gabethehuman opened this issue · 2 comments

Hello.

Ever since ruff v2024.40.0 jupyter notebook cells are not being formatted, neither on on save nor manually. I use the latest VS Code (as of September 17th 2024) and latest ruff, although I noticed that v2024.40.0 was the last version for which it worked.

Settings:

  // jupyter
  "jupyter.askForKernelRestart": false,
  "notebook.consolidatedRunButton": true,
  "notebook.formatOnSave.enabled": true,
  "notebook.defaultFormatter": "charliermarsh.ruff",

  // ruff
  "ruff.enable": true,
  "ruff.organizeImports": true,
  "ruff.importStrategy": "useBundled",
  "ruff.format.args": ["--line-length=120"],
  "ruff.lint.run": "onSave",
  "ruff.lint.args": [
      "--line-length=120",
      "--ignore=ANN101,ANN204,ANN401,C408,D,EM101,EM102,ERA001,FA100,FA102,INP001,N812,NPY002,PLR0402,PLR0913,PTH123,RET504,S101,S311,S603,S607,SIM118,T201,TRY002,TRY003,TRY300",
      "--select=ALL,D102,D103,D106,D207,D208,D214,D215,D300,D301,D417,D418,D419",
  ],

  // python linter and formatter
  "[python]": {
      "editor.formatOnSave": true,
      "editor.defaultFormatter": "charliermarsh.ruff",
      "editor.codeActionsOnSave": {
          "source.fixAll": "never",
          "source.organizeImports": "explicit"
      }
  },

I'm a bit confused. Is v2024.40.0 the last version for which formatting did work or the version when it stopped working? If 2024.40.0 was the last version where it worked, then I suspect that we broke ruff-lsp when we promoted notebooks to be enabled by default.

Okay, I think I see the issue.

Formatting a notebook with ruff-lsp gives me:

[crates/ruff/src/commands/format_stdin.rs:72:11] format_source_code(path, cli.range, settings, source_type, mode) = Err(
    Read(
        Some(
            "/home/micha/astral/test/src/test-notebook.ipynb",
        ),
        Notebook(
            InvalidSchema(
                Error("invalid type: null, expected struct CellMetadata", line: 1, column: 283),
            ),
        ),
    ),
)

So it seems that ruff lsp doesn't send a notebook with a cell.metadata field but the field is required according to the notebook specification