Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notebook.source.organizeImports setting does not seem to work when imports are below markdown cell #593

Open
bolliger32 opened this issue Aug 26, 2024 · 5 comments

Comments

@bolliger32
Copy link

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

@dhruvmanila
Copy link
Member

Huh, that is indeed the case. I wonder if it's a bug in VS Code, let me check if the editor is sending the request in the first place.

@dhruvmanila
Copy link
Member

Hmm, VS Code isn't sending the code action request when a markdown cell is present. This is a bit worrying and could be related to the notebook selector filter here: https://github.com/astral-sh/ruff/blob/3e9c7adeeee639043686a1717a1683f6620efd2a/crates/ruff_server/src/server.rs#L335-L344

@dhruvmanila
Copy link
Member

Let me look at this later today. Thanks for raising this issue!

@bolliger32
Copy link
Author

Thanks for taking a look so quickly!

@dhruvmanila
Copy link
Member

This might be related to microsoft/vscode#216653

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants
@bolliger32 @dhruvmanila and others