-
Notifications
You must be signed in to change notification settings - Fork 56
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
Formatting imports in notebook in VSCode injects random code into cell #680
Comments
Hi, thanks for the report. Can you provide more details regarding the issue? I'm specifically looking for:
There's the troubleshooting guide which can be used to get the logs which will be very useful to debug the issue. |
Cross posting my reproducer from astral-sh/ruff#15991 (reply in thread) I've got a reproducer, here are the steps:
The problematic behavior goes away if you either:
Final note: I saw this behavior both on Linux Debian 12 and on Windows 11 with the latest (and with earlier) versions of VS Code and the ruff extension |
We don't recommend using Maybe we can improve the UX around code actions between notebook and Python files to disallow this kind of problems. |
Yes but now you run into #593
I think that if the new ruff server doesn’t support the Since the only combination of server/settings that always work is the |
Ok, I think I've narrowed this down to why it's happening in the native server and not
I think the reason is that Ruff requires context of the entire notebook but that's only relevant for
I'm leaning towards (1) because (2) will still be problematic for the "fixAll" code action and will only work for "organizeImports" code action. (I initially posted this in #593 but it's specific to this issue) |
Would it help if we included the document version into the edit responses? I think that came up somewhere before |
I'll have to check, not sure |
Looked into versioned edits a bit, I think it might require some more time to invest into how that is being considered but I tried the following patch and that didn't seem to work: diff --git a/crates/ruff_server/src/edit.rs b/crates/ruff_server/src/edit.rs
index 3a7ffb4e3..4d5546461 100644
--- a/crates/ruff_server/src/edit.rs
+++ b/crates/ruff_server/src/edit.rs
@@ -120,7 +120,7 @@ impl WorkspaceEditTracker {
pub(crate) fn set_edits_for_document(
&mut self,
uri: Url,
- _version: DocumentVersion,
+ version: DocumentVersion,
edits: Vec<lsp_types::TextEdit>,
) -> crate::Result<()> {
match self {
@@ -136,8 +136,7 @@ impl WorkspaceEditTracker {
document_edits.push(lsp_types::TextDocumentEdit {
text_document: lsp_types::OptionalVersionedTextDocumentIdentifier {
uri,
- // TODO(jane): Re-enable versioned edits after investigating whether it could work with notebook cells
- version: None,
+ version: Some(version),
},
edits: edits.into_iter().map(lsp_types::OneOf::Left).collect(),
});
diff --git a/crates/ruff_server/src/session/index.rs b/crates/ruff_server/src/session/index.rs
index 4067d6be1..c0d40f8bf 100644
--- a/crates/ruff_server/src/session/index.rs
+++ b/crates/ruff_server/src/session/index.rs
@@ -583,7 +583,16 @@ impl DocumentQuery {
pub(crate) fn version(&self) -> DocumentVersion {
match self {
Self::Text { document, .. } => document.version(),
- Self::Notebook { notebook, .. } => notebook.version(),
+ Self::Notebook {
+ notebook,
+ cell_url: None,
+ ..
+ } => notebook.version(),
+ Self::Notebook {
+ notebook,
+ cell_url: Some(cell_url),
+ ..
+ } => notebook.cell_document_by_uri(cell_url).unwrap().version(),
}
} I see many different version numbers in the trace logs (3x, 7x, etc.) for notebooks and cells so I would need to understand the lifecycle of it and how it's being considered. |
Different version numbers sort of make sense, no? I'd expect that the notebook and every cell are tracked individually (or, at least, VS Code has the freedom to do so) |
I looked into this a bit more and I think there might be a problem here. Considering my patch above, the would be the version of either a text document (Python file), notebook document or a single notebook cell. So, for instance, when VS Code is requesting to resolve a code action for a specific notebook cell (2 separate cells in the following example):
The server will use the version of that specific cell for all the cells that makes up the workspace edit. Here, the I could see if fixing this would resolve the issue. |
I don't think it's possible as I just realized that these requests only have access to a single document and thus can't query the version for other notebook cells. |
When I save the notebook after making changes to my imports, random bits of code from the file are interspersed and repeated in the cell containing the imports.
Version
2025.4.0
Last Updated
2025-01-24, 08:43:26
In this video, I click File|Save, and the reformatting happens. I closed and reopened the file, and it still happened. I closed and reopened VSCode, and it still happened, though the amount of code injected was less.
I haven't seen this occur in .py files, only .ipynb files.
The text was updated successfully, but these errors were encountered: