Reputation: 51
I looked at various sites and created the minimum required TaskProvider code. However, I get the message "task not found" and can't get it to work. Am I missing something that I need?
Environment:
VSCode: latest as of 2/20/2021
OS: Windows 10 (latest update)
package.json
"contributes": {
"taskDefinitions": [
{
"type": "mytask"
}
]
extension.ts
import * as vscode from 'vscode';
import { MyTaskProvider } from './myTaskProvider';
let myTaskProvider: vscode.Disposable | undefined;
export function activate(context: vscode.ExtensionContext) {
myTaskProvider = vscode.tasks.registerTaskProvider(
MyTaskProvider.TASK_TYPE, new MyTaskProvider()
);
}
export function deactivate() {
if (myTaskProvider) {
myTaskProvider.dispose();
}
}
myTaskProvider.ts
import * as vscode from 'vscode';
export class MyTaskProvider implements vscode.TaskProvider {
static TASK_TYPE = 'mytask';
public provideTasks(): Thenable<vscode.Task[]> | undefined {
return _generateTasks();
}
public resolveTask(_task: vscode.Task): vscode.Task | undefined {
const definition = _task.definition;
return _createTask(definition)
}
}
function _createTask(definition: vscode.TaskDefinition) {
return new vscode.Task(
definition,
vscode.TaskScope.Workspace,
"my task name",
definition.type,
new vscode.ShellExecution("echo \"Hello!\"")
);
}
async function _generateTasks(): Promise<vscode.Task[]> {
const taskList: vscode.Task[] = [
_createTask({type: MyTaskProvider.TASK_TYPE})
];
return taskList
}
This code mimics the way vscode-sample is written in some parts. If you run this code, you will not get any errors, but you will also not get any information about the list of tasks.
Do you have any ideas?
Upvotes: 5
Views: 266
Reputation: 25
Not sure if this was your issue (not enough info in the post to know), but I found this post while trying to solve my issue, so I'm answering.
It's silly but the problem was that I was trying to run my task right after beginning a debug session, but that was opening a fresh vscode instance which had no workspace open. This will cause no tasks to show up if your tasks are scoped to Workspace (set using the Task constructor).
Upvotes: 0