Is there a way to run a specific task on a resolved promise from front end

Rephrasing the question, is there a programmatic way to run a specific task or a configuration from task.json and launch.json

[original thread by Nitika Sharma]

[Nitika Sharma]

Hi … any recommendations?

Hello @nitika
by using extensions ?


[Nitika Sharma]

Hi @florentbenoit Yes i am working on an extension to run a node task programatically.
i was looking into

export interface TaskRunnerContribution {
    registerRunner(runners: TaskRunnerRegistry): void;
}

but not quite sure how to use it.

[Nitika Sharma]

@florentbenoit so i am using runConfiguredTask(source, label) and getting error Can’t get task launch configuration for label… can i run this without passing any source, or can you help me with an example what will the source be like eg: task.json or workspace folder/task.json</code>

[Peter Haumer]

I did something similar by just creating a TaskConfiguration manually and calling taskServer.run on it.

[Nitika Sharma]

so My tasks run but when i try to attach it with a terminal i get this error
terminal ERROR Error attaching to terminal id 6, the terminal is most likely gone. Starting up a new terminal instead.
and the terminal doesn’t gets attached to it neither the task is completed. any Idea? strangely it runs on my local machine (windows) but not with Linux

@elaihau Could someone from Ericsson team help out?

[Liang]

the error of Can’t get task launch configuration for label… means the task provider is not properly registered, or the code that runs the task is not providing right information, so that the TaskService is unable to find it from the memory.

If you are building a Theia extension that makes contribution to the Task, {theia}/packages/cpp/source/browser/cpp-task-provider.ts could serve as a good example.

If you simply want to programatically run a task that already exists, either use TaskService from the frontend, or TaskServer from the backend.

If you need an example that shows you how to make a Task in {theia}/.theia/tasks.json, you could find resources from {theia}/packages/task/test-resources/.theia/tasks.json.

[Liang]

could you point us to your code, or share the related snippet, if possible?

[Nitika Sharma]

 async launchTask(arg): Promise<void> {
const taskConfig: TaskConfiguration = {
label: sampleTaskConfig.label,
command: sampleTaskConfig.command,
args: [
arg.args
],
_source: '.',
cwd: '${workspaceFolder}',
type: 'shell'
};
let taskInfo: TaskInfo;
try {
this.logger.info(
'Executing this task
);
    taskInfo = await this.taskServer.run(taskConfig);

     if (taskInfo.terminalId !== undefined) {
       this.attach(taskInfo.terminalId, taskInfo.taskId);
     }

[Nitika Sharma]

@elaihau the above code is executed from menu contribution

[Peter Haumer]

The way this works for me is to compute the workspace path myself using the workspace service and setting it for _source and cwd.

[Liang]

+1 to what @phaumer commented.

@nikita , this.taskServer.run() expects a taskConfig that has been resolved. In other words, your function should pass resolved _source and cwd, instead of using variables such as ${workspaceFolder}.

ThetaskService in front end does the variable resolution for you. You could find how it works fromTaskService.run() function.