How to run a command in Visual Studio Code with launch.json

You can define a task in your tasks.json file and specify that as the preLaunchTask in your launch.json and the task will be executed before debugging begins.

Example:

In tasks.json:

For version 0.1.0:

{
    "version": "0.1.0",
    "tasks": [{
        "taskName": "echotest",
        "command": "echo", // Could be any other shell command
        "args": ["test"],
        "isShellCommand": true
    }]
}

For version 2.0.0 (newer and recommended):

{
    "version": "2.0.0",
    "tasks": [{
        "label": "echotest",
        "command": "echo", // Could be any other shell command
        "args": ["test"],
        "type": "shell"
    }]
}

In launch.json:

{
    "configurations": [
        {
            // ...
            "preLaunchTask": "echotest", // The name of the task defined above
            // ...
        }
    ]   
}

Tasks documentation: https://code.visualstudio.com/docs/editor/tasks

Launch configuration: https://code.visualstudio.com/docs/editor/debugging#_launch-configurations

Leave a Comment