You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: a workspace edit is refused for a target that was changed and undone while its request was pending - #2287
A rename reply, a resolved code action or an incoming workspace/applyEdit was applied to a document the language server had open at capture, even though the user had changed the document and undone the change while the reply was pending. An undo gives the document back its earlier modification stamp, and the stamp was all that was checked. Meanwhile the server was sent the changed text, and may have answered for it.
Fix
WorkspaceEditSnapshot now also records the document's modification sequence (DocumentEx.getModificationSequence()) at capture, and requires it to be unchanged, together with the stamp. The sequence counts every change, an undone one too. It is read as the stamp is, without a lock or a read action, so workspace/applyEdit, which captures on LSP4J's thread, still never waits for a write action.
What isn't used:
The synchronizer's version. It counts sends, not changes, so it also rises when a change made before capture, or a deferred didOpen, is sent after capture. Comparing it would refuse edits that are fine today.
The sequence rises only with a change of the text, so the only new refusals are a change that was undone or redone back to the captured stamp. Checked in the bytecode of 2024.2, 2025.3 and 2026.3. One rare edge case: a change and its undo that are both still unsent when the edit is checked now refuse the edit, though the server never saw them.
Before
After
A target open at capture, changed and undone while the reply was pending, got the edit
The edit is refused: "The document changed while preparing the edit"
Same case with a version above 0: refused, with "the version changed"
Refused, with "The document changed while preparing the edit"
No change, a save, a change sent before capture, or a didOpen sent after capture: the edit applies
Unchanged: the edit applies
Tests
In WorkspaceEditSnapshotTest:
testATargetChangedAndUndoneSinceCaptureIsRefused uses a real undo through UndoManager. It first checks that the undo restored the stamp, then expects refusals for a reply's edit without a version, one with version 0, and an incoming edit. A snapshot captured after the undo still accepts.
Still accepted: testAChangeSentBeforeCaptureStillApplies, testASaveSinceCaptureStillApplies, testATargetWhoseDidOpenIsSentAfterCaptureStillApplies, and the existing testUnchangedDocumentIsCurrent.
The refusal test fails without the fix: the edit is accepted. The acceptance tests pass both with and without it. On 2024.2, headless, the workspace edit, rename and code action tests pass (67 tests).
…done while its request was pending
A rename reply, a resolved code action or an incoming workspace/applyEdit applied to a document that the language server had open at capture, though the user changed the document and undid the change while the reply was pending. An undo gives the document back its earlier modification stamp, which was all that was checked, while the server was sent the changed text and may have answered for it. Such an edit is now refused with "The document changed while preparing the edit".
WorkspaceEditSnapshot records the document's modification sequence, DocumentEx.getModificationSequence(), with its stamp at capture, and requires both to be unchanged. The sequence counts every change of the document, an undone one too, and is read as the stamp is, without a lock or a read action. The synchronizer's version is not used: it rises when a change made before capture, or a didOpen that was deferred, is sent after capture, which would refuse edits that apply today. A save, a willSave and focus changes change neither the stamp nor the sequence.
Fixesredhat-developer#2284
No actionable comments were generated in the recent review.
Summary
Summary by CodeRabbit
Bug Fixes
Workspace edits are now rejected if a document changes after the edit is prepared, even when undo restores its previous text.
Edits remain applicable when changes were already sent before capture, or when a save or document-open notification occurs afterward.
Tests
Added coverage for document changes, undo, saves, and document-open timing across versioned and unversioned edits.
WorkspaceEditSnapshot now detects document changes by checking modification sequences as well as modification stamps. Tests cover undone edits and snapshots captured at different document synchronization stages.
Changes: Snapshot validation
Layer / File(s)
Summary
Capture and compare document state src/main/java/com/redhat/devtools/lsp4ij/WorkspaceEditSnapshot.java
Snapshots record each document’s modification sequence and stamp. Validation requires both values and the synchronizer identity to match the captured state.
This change makes workspace edits get refused when the target document was changed and then undone while the server reply was pending. Saves and deferred document opens are still accepted. No outstanding defect was found, and the tests cover the changed behavior, so the merge risk is minimal.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title names the main change: a workspace edit is refused for a target changed and undone while its request was pending. It matches the changeset, which adds modification-sequence checking to Works…
Description check
The description is directly related to the changeset. It explains the stale-stamp problem, the fix that records DocumentEx.getModificationSequence() alongside the stamp, the alternatives rejected, the…
Linked Issues check
Directly linked issue #2284 requires that a versionless workspace edit be refused when a target is typed into and undone while the reply is pending. The reviewed head's WorkspaceEditSnapshot.java capt…
Out of Scope Changes check
The changes in the whole-PR diff (merge base 0bd5652 to head 10f2cdb) touch WorkspaceEditSnapshot.java and WorkspaceEditSnapshotTest.java. Both changes implement the stamp and sequence check in #2284 …
Full details: Title check
Explanation
The title names the main change: a workspace edit is refused for a target changed and undone while its request was pending. It matches the changeset, which adds modification-sequence checking to WorkspaceEditSnapshot and tests for the refusal. It is long (103 characters), but it is specific and accurate.
Full details: Description check
Explanation
The description is directly related to the changeset. It explains the stale-stamp problem, the fix that records DocumentEx.getModificationSequence() alongside the stamp, the alternatives rejected, the behavior table, and the added tests in WorkspaceEditSnapshotTest. It also links issue #2284.
Full details: Linked Issues check
Explanation
Directly linked issue #2284 requires that a versionless workspace edit be refused when a target is typed into and undone while the reply is pending. The reviewed head's WorkspaceEditSnapshot.java captures a DocumentState per opened document with the modification sequence (DocumentEx.getModificationSequence(), via DocumentState.modificationSequence) and the modification stamp. validateDocument calls state.matches(synchronizer) and refuses with lsp.workspace.edit.document.changed when the target no longer matches. The record Javadoc states that an undone change still changes the sequence. For a placeholder version below 1, validateDocument refuses when the text was not sent at capture or the state does not match. The issue lists two options: the synchronizer version, or the TextChanges sequence from #2283. The PR uses a third approach: the document's own modification sequence, combined with the stamp. The issue's suggested fix is worded as alternatives, so this approach addresses the requirement. The issue's rare-case conditions are covered by the sequence check. Test coverage is described in the PR: testATargetChangedAndUndoneSinceCaptureIsRefused, which uses a real undo, plus acceptance tests. The raw file read was truncated before the body of DocumentState.matches and the modificationSequence helper, so the exact comparison code was not fully read. The PR description and the Javadoc both describe the comparison, so this is a minor residual uncertainty.
Full details: Out of Scope Changes check
Explanation
The changes in the whole-PR diff (merge base 0bd5652 to head 10f2cdb) touch WorkspaceEditSnapshot.java and WorkspaceEditSnapshotTest.java. Both changes implement the stamp and sequence check in #2284 and its tests. The reviewed head's WorkspaceEditSnapshot.java Javadoc and code relate every change to the validation of workspace edits. The PR does not change the synchronizer version logic or TextChanges, and it documents why it avoids both. No unrelated change was found in the inspected content. The truncated read left the tail of DocumentState unread, but the omitted part is part of the same validation logic and does not show an unrelated change.
Recent review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Organization UI
Review profile: CHILL
Plan: Advanced
Run ID: 717c73fd-80da-4515-b371-1ac332970e8c
Commits
Reviewing files that changed from the base of the PR and between 0bd5652 and 10f2cdb.
Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.
❌ Patch coverage is 91.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 58.42%. Comparing base (0bd5652) to head (10f2cdb).
Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A rename reply, a resolved code action or an incoming
workspace/applyEditwas applied to a document the language server had open at capture, even though the user had changed the document and undone the change while the reply was pending. An undo gives the document back its earlier modification stamp, and the stamp was all that was checked. Meanwhile the server was sent the changed text, and may have answered for it.Fix
WorkspaceEditSnapshotnow also records the document's modification sequence (DocumentEx.getModificationSequence()) at capture, and requires it to be unchanged, together with the stamp. The sequence counts every change, an undone one too. It is read as the stamp is, without a lock or a read action, soworkspace/applyEdit, which captures on LSP4J's thread, still never waits for a write action.What isn't used:
didOpen, is sent after capture. Comparing it would refuse edits that are fine today.TextChanges. It marks a file on a save too. That is right for a document opened since capture, since the server read the file until then, but not for a document it already had open.The sequence rises only with a change of the text, so the only new refusals are a change that was undone or redone back to the captured stamp. Checked in the bytecode of 2024.2, 2025.3 and 2026.3. One rare edge case: a change and its undo that are both still unsent when the edit is checked now refuse the edit, though the server never saw them.
didOpensent after capture: the edit appliesTests
In
WorkspaceEditSnapshotTest:testATargetChangedAndUndoneSinceCaptureIsRefuseduses a real undo throughUndoManager. It first checks that the undo restored the stamp, then expects refusals for a reply's edit without a version, one with version 0, and an incoming edit. A snapshot captured after the undo still accepts.testAChangeSentBeforeCaptureStillApplies,testASaveSinceCaptureStillApplies,testATargetWhoseDidOpenIsSentAfterCaptureStillApplies, and the existingtestUnchangedDocumentIsCurrent.The refusal test fails without the fix: the edit is accepted. The acceptance tests pass both with and without it. On 2024.2, headless, the workspace edit, rename and code action tests pass (67 tests).
Fixes #2284