Skip to content

Undo the file operations of a workspace edit in reverse order - #344772

Open
Marcus Lagergren (lagergren) wants to merge 1 commit into
microsoft:mainfrom
lagergren:fix/bulk-file-edit-undo-order
Open

Marcus Lagergren (lagergren) wants to merge 1 commit into
microsoft:mainfrom
lagergren:fix/bulk-file-edit-undo-order

Conversation

@lagergren

Copy link
Copy Markdown

Fixes #344765

Undo and Redo of a workspace edit's file operations performed the inverse operations in their original order (FileUndoRedoElement._reverse()), and RenameOperation.perform() collected its inverse moves in that order too. When one operation depends on an earlier one, as when a file moves out of a folder that is renamed after it, Undo moved the file back first. That created the old folder again, so the folder could then not be renamed back: "target already exists".

  • FileUndoRedoElement undoes the operations from the last to the first, and reverses their inverses, so that Redo performs them in the original order again.
  • A rename operation's inverse undoes its moves in reverse order.

The inverses of copy, create and delete operations keep their order. Within one operation, their order matters only for nested paths, and an existing test checks the order in which Undo recreates deleted files.

Tests, in bulkFileEdits.test.ts, check the file operations of an edit, its Undo and its Redo:

  • "undo moves a file back into its folder after renaming the folder back": one rename operation with two dependent moves.
  • "undo moves a folder out of a created folder before deleting the created folder": two dependent operations.

Both fail without the change, and the existing BulkFileEdits tests pass with it. Run with ./scripts/test.sh --grep 'BulkFileEdits|Files - ' on main 915c6faed9c: 344 passing. ESLint reports nothing for the changed files.

Copilot AI balanced review requested due to automatic review settings October 28, 2026 15:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active issues were found, and the localized reverse-order fix is accompanied by focused new tests covering the dependent file-operation scenarios without regressing existing behavior.

No open issues 🎉

What changed in this PR

This PR fixes issue #344765, where undoing a WorkspaceEdit that moves a file out of a folder and then renames that folder failed with "target already exists". The root cause was that both FileUndoRedoElement._reverse() and RenameOperation.perform() applied inverse operations in their original (forward) order. When operations are dependent, the inverse must run in reverse order (undo the later operation first). The fix is localized to src/vs/workbench/contrib/bulkEdit/browser/bulkFileEdits.ts.

I traced both the inter-operation reversal (backward loop + operations.reverse() to keep redo replaying in the original order) and the intra-rename reversal (undoes.reverse()), and confirmed they operate on orthogonal dimensions and compose correctly across repeated undo/redo cycles. The change is conservative: reverse order is required for dependent operations and harmless for independent ones, so no pre-existing single-operation behavior (including the existing delete/restore ordering tests) regresses.

Changes:

  • FileUndoRedoElement._reverse() now performs inverses from last to first and reverses the operations array afterward, so undo runs in reverse order while redo replays in the original order.
  • RenameOperation.perform() reverses its collected inverse moves so a rename group's dependent moves are undone in reverse order.
  • Adds two focused tests covering dependent moves within one rename operation and across two operations, each asserting the apply/undo/redo sequence.
File Description
src/​vs/​workbench/​contrib/​bulkEdit/​browser/​bulkFileEdits.ts Undo operations and inverse rename moves in reverse order, reversing back so redo preserves the original order.
src/​vs/​workbench/​contrib/​bulkEdit/​test/​browser/​bulkFileEdits.test.ts Adds an applyUndoRedo helper and two tests verifying reverse-order undo for dependent file operations.

No objective issues were found; the change is correct, well-scoped, and covered by new tests that follow the file's existing patterns and conventions.

Review effort: Balanced

Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Oct 28, 2026
Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Oct 28, 2026
Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Oct 28, 2026
Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Oct 29, 2026
Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Oct 29, 2026
Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Oct 29, 2026
Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Nov 2, 2026
Undo and Redo of a workspace edit's file operations performed the
inverse operations in their original order, and a rename operation
collected its inverse moves in that order too. When one operation
depends on an earlier one, as when a file moves out of a folder that is
renamed after it, Undo moved the file back first, which created the old
folder again, and the folder could then not be renamed back: "target
already exists".

FileUndoRedoElement now undoes the operations from the last to the
first, and reverses their inverses, so that Redo performs them in the
original order again. A rename operation's inverse undoes its moves in
reverse order.

Fixes microsoft#344765
Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Nov 4, 2026
Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Nov 5, 2026
Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Nov 9, 2026
Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Nov 11, 2026
Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Nov 22, 2026
Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Nov 25, 2026
Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Nov 25, 2026
Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Nov 25, 2026
Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Nov 26, 2026
Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Nov 26, 2026
Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Nov 27, 2026
Marcus Lagergren (lagergren) added a commit to xtclang/xvm that referenced this pull request Nov 28, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Undo of a WorkspaceEdit that moves a file out of a folder and then renames the folder fails with "target already exists"

3 participants