Using "preLaunchTasks" and Naming a Task in Visual Studio Code - node.js

According to the documentation, it is possible to launch a program before debugging:
To launch a task before the start of each debug session, set the preLaunchTask to the name of one of the tasks specified in tasks.json.
I've not seen example syntax of a "named" task, but the schema documentation reveals a property called taskName. I tried using that to link my launch.json preLaunchTasks to the task, but it didn't work. When I launched my program, Visual Studio Code reported this error:
Could not find a unique task 'launch-core'. Make sure the task exists and that it 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",
}
I then tried changing the property name from taskName to just name, based on this link. That also didn't work.
Intellisense gives no suggestions of how to name a task.
Does anybody know how to uniquely name a task in the tasks.json file? What is the syntax? What is the property name?
Ultimately I'd like to execute two or three node.js processes before my own node.js app is launched. For example, I'd like to have the following three apps launched before my app is launched into 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'm working 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 using the cmd start capability. I'm not sure yet how to make several node tasks launch instead of one, but I can't even get one task to launch because of this task-naming issue.
How do I name a task in the tasks.json file?

FWIW, I'm using VS Code 1.20.1 and here's how I got my preLaunchTask to work:
In launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
...
"preLaunchTask": "npm: build",
}
]
}
In my package.json:
{
...
"scripts": {
"build": "tsc"
...
}
}

So, if it's still relevant, or if someone finds this thread with the same problem, I've just figured it out how it works:
In the tasks.json, you need to create a 'tasks' array - code hint will help you with that - which holds an array of objects. Inside an object, you can have the 'taskName' key-value pair.
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 command before running my project.
In the launch.json file, the "preLaunchTask": "runwebpack" will work now.
Note: the suppressTaskName is true in my example. Omitting it, or setting it to false will result in VS Code appending the taskName 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" }
]
}
With the latter example, you can extend the tasks array with other scripts to be run also.
Hint for my usage: npm run-script fetches what to do from the package.json file's scripts object.
Edit: this works with VS Code 1.3.1

For version 2.0.0 configuration 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": []
}
]
}

For vscode 1.36.1 (1.36.1):
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "build:tsc",
"type": "npm",
"script": "build:tsc"
},
{
"label": "clean",
"type": "npm",
"script": "clean"
},
{
"label": "build",
"dependsOrder": "sequence",
"dependsOn": ["clean", "build:tsc"]
}
]
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/dist/main.js",
"preLaunchTask": "build",
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"console": "integratedTerminal"
}
]
}
Before running node ${workspaceFolder}/dist/main.js, the preLaunchTask will run build task firstly which includes two subtasks: clean and build. It will run clean task firstly, then run build task.
You can specify the label of a task to preLaunchTask option.

The question title is:
"Using “preLaunchTasks” and Naming a Task in Visual Studio Code
I needed to define preLaunchTask***s***.
You can config multiple tasks using the dependsOn property described here
For example, a compound 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.

I've only really seen the taskName used in relation to Gulp; I'm sure there are others but nothing that I have much insight into. Perhaps this can get you off to a start with what you already have?
Run a pre-launch task in VSCODE

Related

Setting VSCode to transpile typescript and start debugging with NodeJS with one F5 press?

Things seem unnecessarily complicated with VSCode+TypeScript+NodeJS. In a C# project in VS, just pressing F5 compiles the project and start debugging. How can I set VSCode do the same thing for TypeScript+NodeJS? What did I do wrong in the following?
I have searched Google and followed the top search result, but pressing F5 with the "test.ts" tab being the current tab seems doing nothing. It briefly shows the debugging tool bar at the top-right, and it disappears, with no output. I see no "test.js". If I press Ctrl+Shift+B, I see the "test.js", but all the output I get is
> Executing task: tsc -p c:\......\tsconfig.json <
Terminal will be reused by tasks, press any key to close it.
and not the output "101" I expected. The following is all the file contents in the directory:
test.ts
var a:number = 101;
console.log(a);
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"sourceMap": true
}
}
.vscode\launch.json (auto-created when I cliekd the link)
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"preLaunchTask": "tsc: build - tsconfig.json",
"program": "${file}",
"outFiles": [
"${workspaceFolder}/**/*.js"
]
}
]
}
tasks.json (auto-created by selecting the menu)
{
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": [
"$tsc"
],
"group": {
"kind": "build",
"isDefault": true
},
"label": "tsc: build - tsconfig.json"
}
]
}
That's what npm custom scripts are for
In your package.json file:
"scripts": {
"start": "tsc && node test.js"
}
Then you can run npm run start where start is the name of the script. You may want to have several scripts, one for each use case
With some npm scripts in your package.json file, if you try to debug a Node.js session on Visual Studio Code it will prompt you which script you want to use and it will run with just pressing F5.

Debugging multiple Node js Function apps in VS Code

I am currently developing a couple of node js function apps in the same project that I would like to be able to debug together. After some effort I was able to run them without a debugger with compounds. And it kinda works since all functions are running although im only able to see output from one at a time.
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Func1",
"type": "node",
"request": "launch",
"port": 9000,
"preLaunchTask": "npm: start - func1"
},
{
"name": "Func2",
"type": "node",
"request": "launch",
"port": 9001,
"preLaunchTask": "npm: start - func2"
}
],
"compounds": [
{
"name": "Compound",
"configurations": [
"Func1",
"Func2"
]
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "start",
"path": "func1/",
"problemMatcher": [],
"label": "npm: start - func1",
"detail": "func start"
},
{
"type": "npm",
"script": "start",
"path": "func2/",
"problemMatcher": [],
"label": "npm: start - func2",
"detail": "func start"
}
]
}
I have also tried different configurations in tasks.json and launch.json using func start with moderate success and I have been able to attach a single function app to debugger while starting the rest.
"type": "func",
"label": "func: start - func1"
"command": "host start",
"problemMatcher": "$func-node-watch",
"isBackground": true,
"dependsOn": "npm: install - func1",
"options": {
"cwd": "${workspaceFolder}/func1"
}
But when I attempt to add a second one with another label it is only able to recognize one of them and the launch of the other app receives error "Could not find the task 'func: start - func2'".
My questions are if it is possible to have multiple Function App attached to debugger and in that case how? Would it also be possible to get the logs in the same output terminal or multiple terminals for each Function App. I know that both can be achived in Visual Studio with c# but is it possible in VS Code with Node.
I am currently developing a couple of node js function apps in the
same project
1, If your function app here refers to Trigger, then the default settings of VS Code should be able to be achieved (unprocessed functions will be in a waiting state, but every place marked with a breakpoint will be executed, but you cannot be in different at the same time Breakpoints).
2, If your function app here refers to a standalone app, then the key is the port. Below settings can change the default port of function app on local:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python"
},
"Host": {
"LocalHttpPort": 5861,
"CORS": "*",
"CORSCredentials": false
}
}
After the above settings, you can start two VS Code programs Debug different function app at the same time without causing conflicts.

How do I run a shell script prior to launch.json in vs code for cucumber-js

So I have a shell script that holds environment variables env.sh:
echo "Running Environment Variables Script"
#########################################################
# Run-time Options
# set to 'dev', 'qa', or 'stg'
export NODE_ENV='qa'
# Running Mode
export HEADLESS='truef'
# Set Debug mode 'true' or 'false'`enter code here`
export EPAY_GUI_AUTOMATION_TEST_DEBUG_MODE='true'
#########################################################
# yadda
# yadda
# yadda
In VS Code, I also am using the following .vscode/tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "loadEnvironmentVariables",
"type": "shell",
"command": ". ${workspaceFolder}/env.sh",
"isBackground": true,
"presentation": {
"reveal": "silent",
"focus": false
}
}
]
}
In VS Code, I also am using the following .vscode/launch.json:
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "foo",
"preLaunchTask": "loadEnvironmentVariables",
"program": "${workspaceFolder}/node_modules/.bin/cucumber-js",
"args": [
"${workspaceFolder}/features/01_MAT_ManualWorkflow_QBO.feature"
]
},
I use the same shell script env.sh in my package.json. This way, I have one set of environment variables I need to manage.
I when I run this in debug mode, the env file is run (I see "Running Environment Variables Script" in the terminal output). But then I get a pop-up saying "The specified task can not be tracked."
I can't seem to get past this issue to be able to run the cucumber-js.
I'm running this on MAcOs.
UPDATE 1
I tried just referring to the npm script, by having the launch.json:
"configurations": [
{
"type": "node",
"request": "launch",
"name": "mat",
"runtimeExecutable": "npm",
"args": [ "mat" ]
},
and where "mat" refers to the package.son:
"mat": ". ./env.sh; node ./node_modules/.bin/cucumber-js --tags #MAT
but that did do jack-squat for me :(

Debug jasmine tests written in typescript node in vs code

I have my unit tests written in jasmine and those are in typescript
// about.service.spec.ts
// say 4 to 5 test cases
// spec/support/jasmine.json
{
"spec_dir": "src/tests/",
"spec_files": ["**/*.spec.ts"],
"helpers": ["jasmine-helpers/**/*.ts"],
...
}
// launch.json - vscode file
{
"version": "0.2.0",
"configurations": [{
"type": "node",
"request": "launch",
"name": "Jasmine tests",
"preLaunchTask": "debuggertests",
}]
}
// tasks.json - vscode
{
"version": "2.0.0",
"tasks": [{
"label": "debuggertests",
"type": "npm",
"script": "test:unit",
"problemMatcher": []
}]
}
// package.json
// have to use jasmine-ts which is flavor over ts-node
"test:unit": "jasmine-ts JASMINE_CONFIG_PATH=spec/support/jasmine.json"
I have used this configuration to debug .spec.ts files in vscode but it did not fire debugger instead it run all tests and debugging started.
I have put a breakpoint in one of the test case of about.service.spec.ts but no breakpoint fired. Could anyone help me on setting up vscode debugging for jasmine tests?
In new jasmine-ts version, you have to include the jasmine.json to the args as this:
{
"type": "node",
"request": "launch",
"name": "Jasmine Current File",
"program": "${workspaceFolder}/node_modules/jasmine-ts/lib/index",
"args": ["--config=jasmine.json", "${file}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
To avoid this issue:
No specs found
Finished in 0.003 seconds
Incomplete: No specs found
Randomized with seed 60766 (jasmine --random=true --seed=60766)
Below configuration will debug current test file - please open the required test file in VS Code and start debugging with this configuration:
{
"type": "node",
"request": "launch",
"name": "Jasmine Current File",
"program": "${workspaceFolder}/node_modules/jasmine-ts/lib/index",
"args": ["${file}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}

How do I create a shortcut key for Rust: cargo run?

I am just starting with Rust and VSCode.
If I hit F1 and choose Tasks: Run Task, I see a list of tasks. If I choose the Run: cargo run task and hit Enter, it is executed in the terminal.
So three keystrokes are needed. Is there a way to assign a shortcut key so that this could be done with one keystroke?
Observation: Ctrl+Shift+B makes a list with two items appear: Rust: cargo build and Rust: cargo check.
I use this in my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"type": "gdb",
"request": "launch",
"target": "./target/debug/my_program",
"cwd": "${workspaceRoot}",
"preLaunchTask": "Cargo build"
}
]
}
of course, you must have a "Cargo build" task in the tasks.json:
"tasks": [
{
"label": "Cargo build",
"command": "cargo build",
"group": {
"kind": "build",
"isDefault": true
}
}
]
Then when I press F5, I can run my program in debug mode.

Resources