Closed
Description
Below is my reproducing script.
- Create an empty
file1
andfile2
in your workspace root, and open it in VS Code. - Run
extension.helloWorld
, it failed with errors. The second insert failed.
But if commenting the operation to rename file2, it succeeds. It looks like adding a different resource change between two inserts, applying the second insert will fail.
import * as vscode from 'vscode';
import * as path from 'path';
export function activate(context: vscode.ExtensionContext) {
let disposable = vscode.commands.registerCommand('extension.helloWorld', async () => {
let files = await vscode.workspace.findFiles("file*");
if (!files || files.length < 2) {
vscode.window.showErrorMessage("Cannot find the test files.");
return;
}
const file1 = files[0];
const file2 = files[1];
let we = new vscode.WorkspaceEdit();
we.insert(file1, new vscode.Position(0, 0), "import1;");
const file2Name = path.basename(file2.fsPath);
const file2NewUri = vscode.Uri.parse(file2.toString().replace(file2Name, `new/${file2Name}`));
we.renameFile(file2, file2NewUri);
we.insert(file1, new vscode.Position(0, 0), "import2;");
await vscode.workspace.applyEdit(we);
const document = await vscode.workspace.openTextDocument(file1);
const expected = "import1;import2;";
if (document.getText() !== expected) {
vscode.window.showErrorMessage(`Failed. Expected: '${expected}', Actual: '${document.getText()}'`);
}
});
context.subscriptions.push(disposable);
}