Skip to content

fix: a workspace edit is refused for a target that was changed and undone while its request was pending - #2287

Merged
lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/workspace-edit-undone-target
Nov 26, 2026
Merged

lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/workspace-edit-undone-target

Conversation

@lagergren

Copy link
Copy Markdown
Collaborator

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 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).

Fixes #2284

…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.

Fixes redhat-developer#2284
@lagergren lagergren added this to the 0.30.2 milestone Nov 26, 2026
@lagergren lagergren added the bug Something isn't working label Nov 26, 2026
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

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.
Snapshot validation tests
src/test/java/com/redhat/devtools/lsp4ij/WorkspaceEditSnapshotTest.java
Tests cover edits that are undone and snapshots captured after text dispatch, save, or before didOpen is sent.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Merge Risk: Minimal · up to 10f2c

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: Passed 4

Check name Status Explanation
Title check Passed 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 Passed 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 Passed 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 Passed 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.

Files selected for processing: 2

  • src/main/java/com/redhat/devtools/lsp4ij/WorkspaceEditSnapshot.java
  • src/test/java/com/redhat/devtools/lsp4ij/WorkspaceEditSnapshotTest.java

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.

@codecov

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ 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).

Files with missing lines Patch % Lines
.../redhat/devtools/lsp4ij/WorkspaceEditSnapshot.java 91.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2287      +/-   ##
==========================================
- Coverage   58.43%   58.42%   -0.02%     
==========================================
  Files         846      846              
  Lines       36455    36463       +8     
  Branches     7545     7549       +4     
==========================================
+ Hits        21304    21305       +1     
  Misses      12152    12152              
- Partials     2999     3006       +7     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lagergren

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Action performed

Review finished.

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.

@lagergren
lagergren merged commit 76a1a61 into redhat-developer:main Nov 26, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A workspace edit applies to a target that was typed into and undone while its request was pending

1 participant