Skip to content

fix: a resolved code action applies to a file opened while it was resolved, when the file's text is unchanged - #2283

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

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

Conversation

@lagergren

Copy link
Copy Markdown
Collaborator

A code action that is resolved after it is chosen applied nothing, and showed "The document was opened after the edit was requested", when a file its edit changes was opened while codeAction/resolve was pending, though opening it changed none of its text. Rename replies and workspace/applyEdit had the same check.

Cause

WorkspaceEditSnapshot records only the documents the server has open at capture, since a reply's targets aren't known then. It refused the whole edit for any target the server had opened since, because it couldn't tell an unchanged open from a changed one.

Fix

The targets can't be recorded at capture, and modification stamps can't be compared with it later, since an undo restores an earlier stamp. Instead, WorkspaceEditSnapshot.TextChanges marks a file with the next number of a sequence after each change of its document, and after each VFS event that changes, creates, copies, moves, renames or deletes it. It does this as a document listener and as a VFS listener. A snapshot records the sequence at capture.

A target the server opened since capture is accepted only if both hold:

  • its document has no unsaved changes;
  • neither its file nor a directory containing it was marked since capture.

Until the open, the server could only read the file, and from the open on it was sent the document, so both are the text the file had at capture. A change that is undone still refuses the edit. Versions are checked as for a document open at capture: one above 0 must be current, and a placeholder below 1 is accepted in a reply and refused in an incoming applyEdit. Other targets keep their existing checks.

The listeners cost one counter increment and one user-data write per document change or VFS batch.

Case Before After
A target opened (didOpen) while the reply was pending, with its text unchanged Nothing applied, error hint Applied
Same, but typed into (even if undone), changed on disk, saved after a change, moved there, or unsaved at the request Refused Refused
Targets open at the request, or never opened Existing checks Unchanged

Tests

  • ResolvedCodeActionTest: a file opened in an editor after the action is resolved, before its edit is applied, gets the edit. Typed in, it doesn't.
  • RenameVersionTest: a file opened while the rename is pending gets the reply's edit.
  • WorkspaceEditSnapshotTest: acceptance for a reply and for an incoming edit, with each kind of version; a document loaded at the request; a refusal for each kind of change.

The four tests that expect the edit to be applied fail without the fix. The refusal tests pass either way. On 2024.2, the code action, rename and workspace edit tests pass (77 tests); CI runs them on all three platforms.

Not covered end to end: an incoming workspace/applyEdit. Between its receipt and its validation there is a single invokeLater, and no deterministic way was found to open a file in between. It shares validate with rename and code actions.

Fixes #2282

…olved, when the file's text is unchanged

A resolved code action, a rename reply and a workspace/applyEdit are checked
against a WorkspaceEditSnapshot, captured before the request, or on receipt
for applyEdit. The snapshot records only the documents that the server has
open at capture: the targets of a reply aren't known then. It refused the
whole edit for any target that the server had opened since, as it couldn't
tell an open that left the text as it was from one that changed it. Opening
a file in an editor while the resolve was pending was enough: nothing was
applied, and the editor showed "The document was opened after the edit was
requested".

Since the targets aren't known at capture, their state can't be recorded
then, and modification stamps can't be compared with the capture later: an
undo restores an earlier stamp. WorkspaceEditSnapshot.TextChanges, a
document listener and a VFS listener, marks a file with the next number of a
sequence after each change of its document, and after each VFS event that
changes, creates, copies, moves, renames or deletes it. A snapshot records
the sequence at capture.

A target that the server opened since capture is accepted only if its
document has no unsaved changes, so that its text is the file's, and neither
the file, its document, nor a directory that contains it was marked since
capture. Until the open the server could only read the file, and from the
open on it was sent the document, so both are the text the file had at
capture. A change that is undone still refuses the edit, since the server
may have been sent the changed text. A version in the edit is checked as for
a document open at capture: one above 0 must be current, and a placeholder
below 1 is accepted in a reply and refused in an incoming applyEdit.

Targets open at capture, and targets the server never opened, keep their
existing checks.

Tests:
- ResolvedCodeActionTest: a file opened in an editor, whose didOpen the
  server receives, after the action is resolved and before its edit is
  applied, gets the edit; typed in, it doesn't.
- RenameVersionTest: a file opened while the rename is pending gets the
  reply's unversioned edit.
- WorkspaceEditSnapshotTest: a target opened since capture is accepted while
  unchanged, for a reply and an incoming edit, with each kind of version; a
  document loaded at capture is accepted when unchanged; refused when changed
  and saved before the open, changed and restored after it, unsaved at
  capture, changed on disk before its document was loaded, or moved there
  with its directory. testTargetOpenedAfterRequestIsRefused, which refused an
  unchanged target, is replaced.

Without the fix, the four tests that expect the edit applied fail, among them
ResolvedCodeActionTest's ("world" instead of "hello world"); the tests that
expect a refusal pass. Headless on 2024.2, the code action tests,
WorkspaceEditSnapshotTest, WorkspaceEditVersionTest and RenameVersionTest:
77 tests, 0 failures, 0 skipped.

Fixes redhat-developer#2282
@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 from code actions and rename requests can now apply to files opened after the request, provided their contents remain unchanged.
    • Edits are refused when the target file has changed, contains unsaved changes, or has moved since the request began.
    • Version checks are applied more consistently to synchronized documents, while edits with placeholder versions remain rejected in incoming updates.

Workspace-edit snapshots now track document and VFS changes. Validation can accept an unchanged target opened after capture and rejects targets whose saved state or file location changed. Tests cover snapshot validation, resolved code actions, and rename replies.

Changes: Workspace Edit Validation

Layer / File(s) Summary
Change tracking and edit validation
src/main/java/com/redhat/devtools/lsp4ij/WorkspaceEditSnapshot.java, src/main/resources/META-INF/plugin.xml
Snapshots record a text-change sequence. The registered listener tracks document edits and VFS events. Validation checks whether targets opened after capture remain saved and unchanged, and applies version checks to synchronized documents.
Snapshot and feature edit tests
src/test/java/com/redhat/devtools/lsp4ij/WorkspaceEditSnapshotTest.java, src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/ResolvedCodeActionTest.java, src/test/java/com/redhat/devtools/lsp4ij/features/rename/RenameVersionTest.java
Tests cover unchanged and changed targets, version handling, resolved edits to another file, and a versionless rename edit to an unchanged file opened while the request is pending.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · up to ce37f

Resolved code actions, rename replies, and incoming edits can now apply to a file opened while the request was pending, as long as its text is unchanged. They are still refused when the text or location may have changed. No actionable merge-blocking risk remains after review.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: a resolved code action applies to a file opened after resolution when the file's text is unchanged. The changeset adds TextChanges tracking and the acceptance rule in …
Description check Passed The description explains the bug, its cause in WorkspaceEditSnapshot, the fix using TextChanges sequences and the new acceptance rules, a before/after table, the tests added across ResolvedCodeActionT…
Linked Issues check Passed Directly linked issue #2282 requires that a resolved code action, rename reply, or incoming workspace/applyEdit not be refused when a target is opened after capture and its text is unchanged. At head,…
Out of Scope Changes check Passed The changes stay within the #2282 fix. WorkspaceEditSnapshot.java adds the TextChanges sequence listener, which is needed to implement the 'changed since capture' check. The version rules for synchron…
Full details: Title check

Explanation

The title names the main change: a resolved code action applies to a file opened after resolution when the file's text is unchanged. The changeset adds TextChanges tracking and the acceptance rule in WorkspaceEditSnapshot, with tests for resolved code actions, rename, and workspace edits. The title is specific and matches the primary behavior change. It is somewhat long (109 characters), but it is related and clear.

Full details: Description check

Explanation

The description explains the bug, its cause in WorkspaceEditSnapshot, the fix using TextChanges sequences and the new acceptance rules, a before/after table, the tests added across ResolvedCodeActionTest, RenameVersionTest, and WorkspaceEditSnapshotTest, and a known coverage gap for incoming workspace/applyEdit. It matches the changeset closely.

Full details: Linked Issues check

Explanation

Directly linked issue #2282 requires that a resolved code action, rename reply, or incoming workspace/applyEdit not be refused when a target is opened after capture and its text is unchanged. At head, WorkspaceEditSnapshot.validateDocument replaces the blanket 'document opened' refusal with isUnchangedSinceCapture(). That check accepts a newly opened document only when FileDocumentManager reports no unsaved changes and TextChanges.changedSince(file, changes) is false. The issue proposed modification stamps and document-loaded state. The PR uses a change sequence instead. The objectives accept this as a different approach, and it meets the issue's goal: unchanged targets apply, and possibly changed targets are refused. Undone changes are refused, which is conservative and within the issue's 'refuse only when the text may have changed' rule. Tests cover the resolve, rename, and snapshot cases. The incoming applyEdit path is not covered end to end, as the PR states. This does not block the issue's core requirement. I read only the head file WorkspaceEditSnapshot.java, the TextChanges body was truncated and not read, and I did not read the test files, so test behavior is taken from the PR description and summary.

Full details: Out of Scope Changes check

Explanation

The changes stay within the #2282 fix. WorkspaceEditSnapshot.java adds the TextChanges sequence listener, which is needed to implement the 'changed since capture' check. The version rules for synchronized documents without captured state apply to the same validation path that the issue describes. plugin.xml registers TextChanges as a document and VFS listener, which the listener design requires. The tests in ResolvedCodeActionTest, RenameVersionTest, and WorkspaceEditSnapshotTest verify the changed behavior. No unrelated functional change was identified in the reviewed evidence.

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: 2074ef76-fee5-4f1c-849c-2e67dbbcf294
Commits

Reviewing files that changed from the base of the PR and between b1b270a and ce37f4b.

Files selected for processing: 5

  • src/main/java/com/redhat/devtools/lsp4ij/WorkspaceEditSnapshot.java
  • src/main/resources/META-INF/plugin.xml
  • src/test/java/com/redhat/devtools/lsp4ij/WorkspaceEditSnapshotTest.java
  • src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/ResolvedCodeActionTest.java
  • src/test/java/com/redhat/devtools/lsp4ij/features/rename/RenameVersionTest.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 79.31034% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.41%. Comparing base (b1b270a) to head (ce37f4b).

Files with missing lines Patch % Lines
.../redhat/devtools/lsp4ij/WorkspaceEditSnapshot.java 79.31% 4 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2283      +/-   ##
==========================================
+ Coverage   58.40%   58.41%   +0.01%     
==========================================
  Files         846      846              
  Lines       36429    36455      +26     
  Branches     7537     7545       +8     
==========================================
+ Hits        21275    21295      +20     
- Misses      12153    12156       +3     
- Partials     3001     3004       +3     

☔ 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 0bd5652 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 resolved code action applies nothing when a file it edits is opened while the resolve is pending, though its text is unchanged

1 participant