Skip to content

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

Description

Does this issue occur when all extensions are disabled?: An extension has to apply the edit (workspace.applyEdit). The Undo that fails is VS Code's own.

  • VS Code Version: 1.140.0. The code below is unchanged on main (915c6faed9c).
  • OS Version: macOS (Darwin 27.0.0)

Steps to Reproduce:

  1. In a workspace with a folder a that contains a file a/b.txt, apply this edit from an extension. It moves the file out of the folder, then renames the folder:

    const root = vscode.workspace.workspaceFolders![0].uri;
    const edit = new vscode.WorkspaceEdit();
    edit.renameFile(vscode.Uri.joinPath(root, 'a', 'b.txt'), vscode.Uri.joinPath(root, 'b.txt'));
    edit.renameFile(vscode.Uri.joinPath(root, 'a'), vscode.Uri.joinPath(root, 'c'));
    await vscode.workspace.applyEdit(edit);

    The edit applies: the workspace now has b.txt and the folder c.

  2. Focus the Explorer and run Edit > Undo.

Expected: Undo renames c back to a, then moves b.txt back into it.

Actual: Undo moves b.txt back first, which creates a new folder a. Renaming c back to a then fails with "target already exists".

Cause: FileUndoRedoElement._reverse() in src/vs/workbench/contrib/bulkEdit/browser/bulkFileEdits.ts performs the inverse operations in the original order, for Undo and for Redo. RenameOperation.perform() also collects its inverse moves in the original order. When one move depends on another, as here, the inverse moves must run in the reverse order: the folder first, then the file.

An extension can't avoid this by ordering the moves. If it renames the folder first and then moves the file from c/b.txt, Undo renames c back to a first, and then moves b.txt into a new folder c. Moves that depend on each other have to be undone in the reverse order of their application.

We hit this in an extension's automated test, where a language server's rename returns such an edit for a file and its folder. The steps above are its minimal form.

Activity

  1. vs-code-engineering commented on Oct 28, 2026

    @vs-code-engineering
    Contributor

    Thanks for creating this issue! It looks like you may be using an old version of VS Code; the latest stable release is 1.143.1. Please try upgrading to the latest version and checking whether this issue remains.

    Happy Coding!

  2. added a commit that references this issue on Oct 28, 2026
    ef37349
  3. lagergren commented on Oct 28, 2026

    @lagergren
    Author

    This is still the case on main (915c6faed9c, 2026-10-28), which is newer than 1.143.1: FileUndoRedoElement._reverse() and RenameOperation.perform() in bulkFileEdits.ts haven't changed. #344772 fixes it.

  4. ayushkrsingh946 commented on Oct 28, 2026

    @ayushkrsingh946
    Contributor

    I investigated this issue and verified the root causes:

    1. In RenameOperation.perform(), inverse moves (undoes) were being returned in forward application order. When an edit moves a file out of a folder and then renames the folder, undoing in original order attempts to move the file back first (which creates the folder before the folder rename can occur), causing the folder rename to fail with "target already exists". Returning undoes.reverse() ensures inverse moves run in reverse order during undo (and restores original forward order upon redo).
    2. In FileUndoRedoElement._reverse(), operations across different groups were also executed in forward order. Reversing this.operations ensures multi-group operations are undone in reverse order and redone in forward order.

    Submitting a PR with this fix and regression tests shortly.

  5. lagergren commented on Oct 28, 2026

    @lagergren
    Author

    Ayush Kr Singh (@ayushkrsingh946) Thanks for looking into it. #344772 already fixes both, with regression tests in bulkFileEdits.test.ts, so a second PR isn't needed.

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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions