Using "preLaunchTasks" and "Naming Tasks in Visual Studio Code"

According to the documentation , you can run the program before debugging:

To start a task before the start of each debugging session, preLaunchTask name of one of the tasks specified in tasks.json.

I have not seen an example of the syntax for a "named" task, but a property called taskName in the schema documentation . I tried using this to associate my preLaunchTasks Json preLaunchTasks with a task, but that didn't work. When I ran my program, Visual Studio Code reported this error:

Could not find unique task 'launch-core'. Make sure the task exists and has a unique name.

My custom "named" task looked something like this:

 { "taskName": "launch-core", "version": "0.1.0", "command": "C:\\utils\\mystuff.exe", // The command is a shell script "isShellCommand": true, // Show the output window only if unrecognized errors occur. "showOutput": "silent", } 

Then I tried to change the property name from taskName to just name based on this link . That didn't work either.

Intellisense does not give any clues on how to name the task.

Does anyone know how to uniquely name a task in a tasks.json file? What is the syntax? What is the name of the property?

Ultimately, I would like to run two or three node.js processes before starting my own node.js application. For example, I would like to launch the following three applications before my application runs in the debugger:

 sh -c 'cd ./manager/ && node manager.js' sh -c 'cd ./adapter/ && node adapter.js' sh -c 'cd ./core/ && node core.js' 

If I work on a Windows box, my task might look something like this:

 { "taskName": "core-launch", "version": "0.1.0", // The command is tsc. Assumes that tsc has been installed using npm install -g typescript "command": "start", // The command is a shell script "isShellCommand": true, // Show the output window only if unrecognized errors occur. "showOutput": "silent", // args is the HelloWorld program to compile. "args": [ "ACD-Manager", "/B", "/D", "./manager/", "node", "manager.js" ] } 

The above task using the start cmd feature . I'm still not sure how to get several node tasks to start instead of one, but I can't even run one task because of this problem with task names.

How can I name a task in tasks.json file?

+23
visual-studio-code
source share
5 answers

So, if this is still relevant, or if someone finds this thread with the same problem, I just figured out how this works:

In tasks.json you need to create an array of 'tasks - a code hint will help you with this - which contains an array of objects. Inside the object, you may have a key pair of the 'taskName' key.

Example:

 { "version": "0.1.0", "command": "npm", "isShellCommand": true, "args": ["run-script", "webpack"], "showOutput": "always", "tasks": [ { "taskName": "runwebpack", "suppressTaskName": true } ] } 

In my case, I had to run the npm run-script webpack before starting my project. In the launch.json file "preLaunchTask": "runwebpack" will work.

Note: in my example, the value of suppressTaskName true. By omitting it or setting it to false, the VS code will add the taskName command after the command.

A more general approach would be something like this:

 { "version": "0.1.0", "command": "npm", "isShellCommand": true, "args": ["run-script"], "showOutput": "always", "tasks": [ { "taskName": "webpack" } ] } 

In the last example, you can expand the tasks array with other scripts to run.

Hint for my use: npm run-script chooses what to do from the package.json scripts file.

Edit: this works with VS Code 1.3.1

+18
source share

FWIW, I am using VS Code 1.20.1 , and this is how I got my preLaunchTask :

In launch.json :

 { "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", ... "preLaunchTask": "npm: build", } ] } 

In my package.json :

 { ... "scripts": { "build": "tsc" ... } } 
+14
source share

For configuration version 2.0.0, you now use label instead of taskName .

package.json:

 ... "scripts": { "tsc": "tsc", ... } ... 

launch.json (my source is in the src directory and tsc compiles to the dist directory):

 { "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "preLaunchTask": "Compile", "name": "Launch Program", "program": "${workspaceFolder}/src/index.ts", "outFiles": [ "${workspaceFolder}/dist/**/*.js" ], "protocol": "inspector", "sourceMaps": true } ] } 

tasks.json:

 { "version": "2.0.0", "tasks": [ { "label": "Compile", "type": "npm", "script": "tsc", "problemMatcher": [] } ] } 
+6
source share

I really only saw the task name used in relation to Gulp; I am sure there are others, but I do not understand anything. Perhaps this can help you get started with what you already have?

Run task before running in VSCODE

0
source share

Question title:

"Using" preLaunchTasks "and naming tasks in Visual Studio Code

I needed to define preLaunchTask *** s ***.

You can configure multiple tasks using the dependent property described here.

For example, a composite task in your tasks.json:

 { "version": "2.0.0", "tasks": [ { "label": "Client Build", "command": "gulp", "args": ["build"], "options": { "cwd": "${workspaceRoot}/client" } }, { "label": "Server Build", "command": "gulp", "args": ["build"], "options": { "cwd": "${workspaceRoot}/server" } }, { "label": "Build", "dependsOn": ["Client Build", "Server Build"] } ] } 

You can find more information about naming tasks here .

0
source share

All Articles