Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to await a build task in a VS Code extension?

let result = await vscode.commands.executeCommand('workbench.action.tasks.build');

resolves immediately.

How can I await a build task with VS Code API?

like image 513
Dae Avatar asked Apr 25 '20 16:04

Dae


People also ask

How do I run a build task in VS Code?

Tip: You can run your task through Quick Open (Ctrl+P) by typing 'task', Space and the command name.

Do VS Code extensions update automatically?

VS Code checks for extension updates and installs them automatically. After an update, you will be prompted to reload VS Code. If you'd rather update your extensions manually, you can disable auto-update with the Disable Auto Updating Extensions command that sets the extensions.autoUpdate setting to false .

How do you use an extension in VS Code?

Find extensions to install using the Extensions view. Install an extension from the VS Code Extension Marketplace. See what features are added via the Features Contributions tab or Command Palette (Ctrl+Shift+P). See recommendations for other extensions.


1 Answers

I figured it out! Tasks cannot be awaited from vscode.tasks.executeTask, but we can await vscode.tasks.onDidEndTask and check if ended task is our task.

async function executeBuildTask(task: vscode.Task) {
    const execution = await vscode.tasks.executeTask(task);

    return new Promise<void>(resolve => {
        let disposable = vscode.tasks.onDidEndTask(e => {
            if (e.execution.task.group === vscode.TaskGroup.Build) {
                disposable.dispose();
                resolve();
            }
        });
    });
}

async function getBuildTasks() {
    return new Promise<vscode.Task[]>(resolve => {
        vscode.tasks.fetchTasks().then((tasks) => {
            resolve(tasks.filter((task) => task.group === vscode.TaskGroup.Build));
        });
    });
}

export function activate(context: vscode.ExtensionContext) {
    context.subscriptions.push(vscode.commands.registerCommand('extension.helloWorld', async () => {
        const buildTasks = await getBuildTasks();
        await executeBuildTask(buildTasks[0]);
    }));
}

Note that currently there is a bug #96643, which prevents us from doing a comparison of vscode.Task objects: if (e.execution.task === execution.task) { ... }

like image 72
Dae Avatar answered Sep 28 '22 02:09

Dae