{"record":{"id":"12748485061be3d1","repo":"astral-sh/ruff","slug":"attempted-to-add-edits-for-a-document-that-was-alr","errorCode":null,"errorMessage":"Attempted to add edits for a document that was already edited","messagePattern":"Attempted to add edits for a document that was already edited","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ruff_server/src/edit.rs","lineNumber":146,"sourceCode":"        Ok(())\n    }\n\n    /// Sets the edits made to a specific document. This should only be called\n    /// once for each document `uri`, and will fail if this is called for the same `uri`\n    /// multiple times.\n    pub(crate) fn set_edits_for_document(\n        &mut self,\n        uri: Uri,\n        _version: DocumentVersion,\n        edits: Vec<lsp_types::TextEdit>,\n    ) -> crate::Result<()> {\n        match self {\n            Self::DocumentChanges(document_edits) => {\n                if document_edits\n                    .iter()\n                    .any(|document| document.text_document.text_document_identifier.uri == uri)\n                {\n                    return Err(anyhow::anyhow!(\n                        \"Attempted to add edits for a document that was already edited\"\n                    ));\n                }\n                document_edits.push(lsp_types::TextDocumentEdit {\n                    text_document: lsp_types::OptionalVersionedTextDocumentIdentifier {\n                        text_document_identifier: TextDocumentIdentifier { uri },\n                        // TODO(jane): Re-enable versioned edits after investigating whether it could work with notebook cells\n                        version: None,\n                    },\n                    edits: edits.into_iter().map(lsp_types::Edit::TextEdit).collect(),\n                });\n                Ok(())\n            }\n            Self::Changes(changes) => {\n                if changes.get(&uri).is_some() {\n                    return Err(anyhow::anyhow!(\n                        \"Attempted to add edits for a document that was already edited\"\n                    ));","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_server/src/edit.rs#L128-L164","documentation":"The LSP server's `DocumentChanges` edit container forbids two `TextDocumentEdit` entries for the same document URI. `set_edits_for_document` scans existing entries and throws this anyhow error if the URI was already added, guarding against duplicate/conflicting edit batches in a single WorkspaceEdit.","triggerScenarios":"Calling `set_edits_for_document(uri, ...)` (directly or via `set_fixes_for_document`, `quick_fix`, or `noqa_comments`) twice for the same open document URI within one WorkspaceEdit being built.","commonSituations":"A code action request that both applies quick fixes and adds noqa edits for the same file; overlapping diagnostics each producing a fix for the same document; custom integrations invoking fix and format paths together.","solutions":["Ensure each document's edits are collected once: merge edit lists before calling set_edits_for_document","Combine fixes and noqa edits for a document into a single set_edits_for_document call","Check callers (quick_fix, noqa_comments) so a document with multiple diagnostics funnels through one path","If duplicate entry is legitimate, convert the flow to use the `Changes` map variant, which keys by URI"],"exampleFix":"// before\nchanges.set_edits_for_document(uri, fix_edits)?;\nchanges.set_edits_for_document(uri, noqa_edits)?; // panics: duplicate\n// after\nlet mut all_edits = fix_edits;\nall_edits.extend(noqa_edits);\nchanges.set_edits_for_document(uri, all_edits)?;","handlingStrategy":"validation","validationCode":"// Rust: check membership before inserting\nfn can_add(changes: &DocumentChanges, uri: &Url) -> bool {\n    !changes.iter().any(|d| d.text_document.text_document_identifier.uri == *uri)\n}\n// call: assert!(can_add(&document_changes, &uri));","typeGuard":null,"tryCatchPattern":"// Rust\nchanges.set_edits_for_document(uri.clone(), edits)\n    .with_context(|| format(\"failed to register edits for {uri}\"))?;","preventionTips":["Aggregate all edits for a document (fixes + noqa) before one set_edits_for_document call","Keep a seen-URIs set in request handlers that loop over multiple diagnostics","Ensure each handler builds its own fresh WorkspaceEdit per request","Deduplicate diagnostics by URI before generating fixes"],"tags":["rust","lsp","server","edits"],"backgroundTag":"duplicate-document-edit","analyzedSha":"26f38c119cac42e4d320ba08f09224fdec74af2c","analyzedAt":"2026-09-05T10:32:37.492Z","contentChangedAt":"2026-09-05T10:32:37.492Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}