astral-sh / ruff-lsp

A Language Server Protocol implementation for Ruff.
Other
1.34k stars 46 forks source link

Notebook Format Cell//Notebook not working with `ruff-lsp` #490

Open jamesvrt opened 2 months ago

jamesvrt commented 2 months ago

Create test.ipynb, create a cell with

from abc import (a, b, c
)

Perform Format Select/Cell/Document/Notebook via ctrl+shift+P, the right click menu or the keyboard shortcuts.

No changes observed in the cell.

Ruff output:

2024-09-04 11:28:50.174 [info] Using interpreter executable: /home/jamesvrt/miniconda3/envs/test/bin/ruff
2024-09-04 11:28:50.174 [info] Found ruff 0.6.4 at /home/jamesvrt/miniconda3/envs/test/bin/ruff
2024-09-04 11:28:50.174 [info] Running Ruff with: /home/jamesvrt/miniconda3/envs/test/bin/ruff ['format', '--force-exclude', '--quiet', '--stdin-filename', '/home/jamesvrt/test/test.ipynb', '--config', 'line-length=120']

Performing ruff format test.ipynb on the CLI works as expected.

Including "ruff.nativeServer": "on" in settings.json resolves the issue.

No ruff.toml or pyproject.toml

settings.json includes:

    "[python]": {
        "editor.defaultFormatter": "charliermarsh.ruff",
    },
    "ruff.format.args": ["--config", "line-length=120"],
    "notebook.defaultFormatter": "charliermarsh.ruff",

ruff 0.6.3 ruff vscode extension 2024.42.0

Ubuntu 22.04 VSCode 1.92.2 Python 3.10.11

dhruvmanila commented 2 months ago

Do you see anything in the logs? Refer to the troubleshooting guide.

There was a bug that's been fixed in https://github.com/astral-sh/ruff-lsp/pull/485, I'll bump the version of ruff-lsp today to get it out.

dhruvmanila commented 2 months ago

Including "ruff.nativeServer": "off" in settings.json resolves the issue.

What do you mean by this? Like, using the ruff-lsp resolves the issue? But, the issue title says that it doesn't work with ruff-lsp?

jamesvrt commented 2 months ago

What do you mean by this?

Sorry, typo! I meant:

Including "ruff.nativeServer": "on" in settings.json resolves the issue.

I updated the issue. So using the Rust based version is a workaround. I'll try the updated ruff-lsp when it's out.

jamesvrt commented 2 months ago

Do you see anything in the logs? Refer to the troubleshooting guide.

Sorry, I missed that bit. Logs:

2024-09-05 09:31:17.874 [info] [Trace - 9:31:17 AM] Sending request 'textDocument/rangeFormatting - (6)'.
2024-09-05 09:31:17.877 [info] [Trace - 9:31:17 AM] Received notification 'window/logMessage'.
2024-09-05 09:31:17.877 [info] Using interpreter executable: /home/jamesvrt/miniconda3/envs/test/bin/ruff
2024-09-05 09:31:17.877 [info] [Trace - 9:31:17 AM] Received notification 'window/logMessage'.
2024-09-05 09:31:17.877 [info] Found ruff 0.6.3 at /home/jamesvrt/miniconda3/envs/test/bin/ruff
2024-09-05 09:31:17.877 [info] [Trace - 9:31:17 AM] Received notification 'window/logMessage'.
2024-09-05 09:31:17.877 [info] Running Ruff with: /home/jamesvrt/miniconda3/envs/test/bin/ruff ['format', '--force-exclude', '--quiet', '--stdin-filename', '/home/jamesvrt/code/test/test.ipynb', '--config', 'line-length=120']
dhruvmanila commented 2 months ago

@jamesvrt the new ruff-lsp version is out, can you try it out?

Although I don't see any error in the logs :(

dhruvmanila commented 2 months ago

Also, I'd recommend to switch over to the Rust based server as it has the same functionalities (+ a few more) with no additional installation and high performance :)

jamesvrt commented 2 months ago

How do you get the VSCode extension to use the latest ruff-lsp? The latest extension version still uses the previous ruff-lsp and it doesn't pick up ruff-lsp in my Python environment, only ruff.

BTW, I'm content to use ruff rather than ruff-lsp since it's faster anyway.

dhruvmanila commented 2 months ago

How do you get the VSCode extension to use the latest ruff-lsp? The latest extension version still uses the previous ruff-lsp and it doesn't pick up ruff-lsp in my Python environment, only ruff.

Ah right, sorry. I'd need to release a new VS Code extension as well.

dhruvmanila commented 2 months ago

I've released a new extension version which should be available in a couple of minutes.

HenryDashwood commented 2 months ago

Just to say I've been having similar issues as the people above.

adding "ruff.nativeServer": "on" to my workspace's settings.json got ruff formatting my notebook cells again.

However, I'm on the latest version of your extension, v2024.44.0 so should setting that explicitly have been necessary?

I'm also on ruff 0.6.4 everywhere else and in the documentation I think it says that it should default to the native server without explicit setting?

dhruvmanila commented 2 months ago

@HenryDashwood can you share the VS Code settings and Ruff logs? I just tested with 2024.44.0 VS Code with ruff.nativeServer: "off" and it works. And, it doesn't work when using ruff-lsp with 2024.42.0.

jamesvrt commented 2 months ago

ruff-lsp still doesn't work for me with the VSCode extension version 2024.44.0. Logs:

2024-09-09 09:13:16.808 [info] Interpreter executable (/home/jamesvrt/miniconda3/envs/test/bin/ruff) not found
2024-09-09 09:13:16.808 [info] [Trace - 9:13:16 AM] Received notification 'window/logMessage'.
2024-09-09 09:13:16.808 [info] Falling back to bundled executable: /home/jamesvrt/.vscode/extensions/charliermarsh.ruff-2024.44.0-linux-x64/bundled/libs/bin/ruff
2024-09-09 09:13:16.808 [info] [Trace - 9:13:16 AM] Received notification 'window/logMessage'.
2024-09-09 09:13:16.808 [info] Found ruff 0.6.4 at /home/jamesvrt/.vscode/extensions/charliermarsh.ruff-2024.44.0-linux-x64/bundled/libs/bin/ruff
2024-09-09 09:13:16.808 [info] [Trace - 9:13:16 AM] Received notification 'window/logMessage'.
2024-09-09 09:13:16.808 [info] Running Ruff with: /home/jamesvrt/.vscode/extensions/charliermarsh.ruff-2024.44.0-linux-x64/bundled/libs/bin/ruff ['format', '--force-exclude', '--quiet', '--stdin-filename', '/home/jamesvrt/code/test/test.ipynb', '--config', 'line-length=120']
2024-09-09 09:13:16.812 [info] [Trace - 9:13:16 AM] Received response 'textDocument/rangeFormatting - (15)' in 6ms.

Appreciate your hard work with ruff btw!

dhruvmanila commented 2 months ago
2024-09-09 09:13:16.812 [info] [Trace - 9:13:16 AM] Received response 'textDocument/rangeFormatting - (15)' in 6ms.

I think we don't support range formatting for Notebooks in ruff-lsp and that's only supported in the native server. Can you try running other formatting operations that apply on the entire notebook and verify? Although it might just be better to completely switch to the native server ;)

jamesvrt commented 2 months ago

"Format Notebook", "Format Cell" and "Format Range" don't work, but "Ruff: Format document" (only available through ctrl+shift+P) does work. I need more granular control than document formatting, so I'll stick with the native server. Thanks!

You can set this to closed if this is expected behaviour, though it might be nice to mention it in the extension page or default to the native server.

dhruvmanila commented 2 months ago

"Format Notebook", "Format Cell" and "Format Range" don't work, but "Ruff: Format document" (only available through ctrl+shift+P) does work.

Oh, that's unexpected and could very well be a bug in ruff-lsp. I think at least "Format Notebook" and "Format Cell" are suppose to work.

HenryDashwood commented 2 months ago

@HenryDashwood can you share the VS Code settings and Ruff logs? I just tested with 2024.44.0 VS Code with ruff.nativeServer: "off" and it works. And, it doesn't work when using ruff-lsp with 2024.42.0.

Tried to replicate this evening and couldn't I'm afraid. But I guess that does mean it's working!