Search code examples
visual-studio-codevscode-tasks

Several "build tasks" for visual studio code (python)


My tasks.json looks like this:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    // A task runner that runs a python program
    "command": "python3",
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": true
    },
    "args": [
        "${file}"
    ]
}

When I run ctrl+shift+B the top panel asks "Select the build task to run", and there's one alternative: python3. Now if I'd like to add a new build-task (for example a runspider command with scrapy), so it's added to the build tasks. How would I go about adding this?


Solution

  • You can define multiple tasks in your tasks.json by assigning an array of task objects to the tasks property, like this:

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            {
                "taskName": "python3",
                "type": "shell",
                "command": "python3",
                "args": [
                    "${file}"
                ],
                "presentation": {
                    "echo": true,
                    "reveal": "always",
                    "focus": true
                }
            },
            {
                "taskName": "runspider",
                "type": "shell",
                "command": "runspider"
            }
        ]
    }
    

    Also, Ctrl+Shift+B runs the default build task, so you might want to set your "workbench.action.tasks.runTask" keybinding.

    {
        "key": "ctrl+shift+b",
        "command": "workbench.action.tasks.runTask"
    }
    

    Once that is done, you can select the task when you use workbench.action.tasks.runTask command, as shown below:

    Choose which task to run

    You can also choose your default build task by setting the "group" property on the task. Here, in the following snippet, your "python3" task will run as the default build task.

    ...
    "tasks": [
        {
            "taskName": "python3",
            "type": "shell",
            "command": "python3",
            "args": [
                "${file}"
            ],
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": true
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "taskName": "runspider",
            "type": "shell",
            "command": "runspider"
        }
    ]
    ...
    

    You can read more about tasks here: Tasks in VSCode