How to run multiple tasks in VS Code on build? - visual-studio-code

Using tasks.json version 2.0.0, I have not been able to make it so that, when I build my application, multiple tasks are run at the same time. I'm using gulp for my SCSS compilation, and running my Compile/minify cms.scss task on its own works fine, so it's not a problem with the task itself, just VS Code's task runner. When I Run Build Task in VS Code, my gulp task is not being run, even though it has "group": "build" — only the dotnet one is.
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/HpsCoreWeb.csproj"
],
"problemMatcher": "$msCompile",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Compile/minify cms.scss",
"type": "gulp",
"task": "cms.scss:cms.min.css",
"problemMatcher": "$node-sass",
"group": "build"
}
]
}
According to the VS Code Tasks documentation:
group: Defines to which group the task belongs. In the example, it belongs to the test group. Tasks that belong to the test group can be executed by running Run Test Task from the Command Palette.
The dotnet build task is succeeding, so shouldn't the other task, which is also part of the build group, be run as well? What am I doing wrong?

The problem is that "Run Test Task" and "Run Build Task" do not execute all tasks in that specific group. Usually you get a drop down selection so you can choose which task to execute. Since you have specified one of the tasks as default, the selection will be skipped and instead the default task is executed.
You can work around that by adding dependencies. Take the following example:
{
"version": "2.0.0",
"tasks": [
{
"label": "Echo 1",
"command": "echo",
"type": "shell",
"args": [ "echo1" ],
"group": {
"kind": "build",
"isDefault": true
},
"dependsOn":["Echo 2"]
},
{
"label": "Echo 2",
"type": "shell",
"command": "echo",
"args": [ "echo2" ],
"group": "build"
}
]
}
As Echo 1 depends on Echo 2, Echo 2 will be executed prior to executing Echo 1. Note that the definition is a list, so more than one task can be specified. In that case the tasks are executed in parallel.
In your case adding "dependsOn":["Compile/minify cms.scss"] to your main build task should execute both tasks.

You can use Compound Tasks.
The example below executes "Client Build" and "Server Build" tasks when "Build" task is called.
{
"version": "2.0.0",
"tasks": [
{
"label": "Client Build",
"command": "gulp",
"args": ["build"],
"options": {
"cwd": "${workspaceFolder}/client"
}
},
{
"label": "Server Build",
"command": "gulp",
"args": ["build"],
"options": {
"cwd": "${workspaceFolder}/server"
}
},
{
"label": "Build",
"dependsOn": ["Client Build", "Server Build"]
}
]
}

Add 'gulp-load-plugins' plugin in package.json
var gulpLoadPlugins = require('gulp-load-plugins'),
plugins = gulpLoadPlugins();
To read more about that plugin, see How To Build And Develop Websites With Gulp

Double check your settings are enabled for Gulp auto-detection:
"gulp.autoDetect": "on"

Related

How to run tasks in sequence?

I would like to configure one task labeled "Build and then Run" that would execute a specific build task and then run the executable. I thought that the dependsOn property was what I was looking for but as it turns out it runs the tasks in parallel instead.
Here is an example of tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"type": "shell",
"command": "g++ -g ${workspaceFolder}/main.cpp -o ${workspaceFolder}/main.exe",
"problemMatcher": "$gcc"
},
{
"label": "Run",
"type": "shell",
"command": "${workspaceFolder}/main.exe"
},
{
"label": "Build and then Run",
"type": "shell",
"dependsOn": [ // <------ important
"Run",
"Build"
]
}
]
}
It doesn't matter in what order I put the tasks into the dependsOn array. The "Run" task executes with error because the "Build" task creates the executable too late.
Is there some property or trick that allows two tasks to run in sequence?
{
"label": "Build and then Run",
"type": "shell",
"dependsOrder": "sequence", <= `parallel must be the default
"dependsOn": [
"Run",
"Build"
]
}
If you specify "dependsOrder": "sequence" then your task dependencies
are executed in the order they are listed in dependsOn. Any
background/watch tasks used in dependsOn with "dependsOrder":
"sequence" must have a problem matcher that tracks when they are
"done".
from https://code.visualstudio.com/Docs/editor/tasks#_compound-tasks
That implies to me, plus your experience, that omittiing the option dependsOn means by default the tasks will run in parallel, not in sequence.

Is it possible to open a new terminal inside VSCode from inside a script?

I want to start 3 servers from a single command.
I have package.json scripts like so:
"serve_auth": "cd dev/mock/auth && nodemon --exec babel-node ./server.js --presets #babel/env",
"serve_db": "cd dev/mock/db && nodemon --exec babel-node ./server.js --presets #babel/env",
"start": "react-scripts start",
"develop": "./launch_script.sh"
and I have a script launch_script.sh like so:
#!/bin/bash
( yarn serve_db ) & ( yarn serve_auth ) & ( yarn start )
but this opens them all in a single terminal window, and they end up tripping all over each other.
I know you can open new terminals from the VSCode GUI, but is it possible to open a new terminal from within one? Or to tell VSCode to open 3 terminals each with a separate command ?
I think this could be something for compound tasks
{
"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"]
}
]
}
Compound tasks
You can also compose tasks out of simpler tasks with
the dependsOn property. For example, if you have a workspace with a
client and server folder and both contain a build script, you can
create a task that starts both build scripts in separate terminals. If
you list more than one task in the dependsOn property, they are
executed in parallel by default.
Also compound launch configurations may be interesting to you as it seems like your scripts are for starting a frontend and backend app.
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Server",
"program": "${workspaceFolder}/server.js",
"cwd": "${workspaceFolder}"
},
{
"type": "node",
"request": "launch",
"name": "Client",
"program": "${workspaceFolder}/client.js",
"cwd": "${workspaceFolder}"
}
],
"compounds": [
{
"name": "Server/Client",
"configurations": ["Server", "Client"]
}
]
}
Both are examples from the corresponding docs page but adjusting them to your scripts should be straightforward.

Any source or idea, how to do delayed compound in VSCode?

I have a build task that takes at least 2 seconds to finish in tasks.json.
I also have different very quick task for cleaning some files in tasks.json.
I have 3 configurations in launch.json: server, server_running_on_top_of_server and client. All of them can be running separately, so all of them should have the build as a preLaunchTask.
So if I run those 3 configurations separately with build as preLaunchTask and cleaning specified in dependsOn of build, it's quite fine.
But when I want to run those 3 configurations together as a compound, it's not very intuitive. I would like to first run build task and then server and after the server is up, then server_running_on_top_of_server and client. The cleaning configuration should be run only for the client, but can be run everytime the build task is run.
"compounds": [
{
"name": "server, server_running_on_top_of_server and client",
"configurations": ["server", "server_running_on_top_of_server", "client"]
}
and
"tasks": [
{
"label": "build",
"type": "shell",
"command": "make",
"args": [
"-j4",
"debug"
],
"group": {
"kind": "build",
"isDefault": true
},
"dependsOn": "delete something",
"presentation": {
"panel": "shared"
}
},
{
"label": "delete something",
"type": "shell",
"command": "rm",
"args": [
"-f", "something"
],
"presentation": {
"panel": "shared"
}
},
{
"label": "wait 5 seconds",
"type": "shell",
"command": "sleep",
"args": [
"5"
]
}
]
But the build task is run 3 times this way and also separately in 3 terminals even with presentation.panel: "shared" so it uses 12 cores instead of 4 so it totally lags my PC. How to fix that?
And how to run the remaining 2 configurations after the server is up? Is there a better approach than to create another task that just waits some time?
Is there a possibility to specify more than 1 task in preLaunchTask? So I can assign build and cleaning task only for client.

How to chain tasks in Visual Studio Code using only tasks.json?

I have been ploughing through the documentation of Visual Studio Code to figure out how to add multiple consecutive tasks to the tasks.json file.
The tasks array only allows for creating different arguments to the same command. In this example the command is echo.
{
"version": "0.1.0",
"command": "echo",
"isShellCommand": true,
"args": [],
"showOutput": "always",
"echoCommand": true,
"suppressTaskName": true,
"tasks": [
{
"taskName": "hello",
"args": ["Hello World"]
},
{
"taskName": "bye",
"args": ["Good Bye"]
}
]
}
Does tasks.json allow several tasks to be executed consecutively? For example, tsc followed by uglify?
The dependsOn feature was shipped in version 1.10.0. For example, I am using this to compile and run single file scripts in TypeScript:
{
"version": "2.0.0",
"tasks": [
{
"command": "tsc -p ${cwd}/2017-play",
"label": "tsc-compile",
"type": "shell"
},
{
"command": "node ${cwd}/2017-play/build/${fileBasenameNoExtension}.js",
"label": "node-exec",
"type": "shell",
"dependsOn": [
"tsc-compile"
],
"problemMatcher": []
}
]
}
Here is a working example that runs the tcs build and copies the source to another folder using a shell script.
This is based on various posts on StackOverflow and the documentation found here:
https://code.visualstudio.com/updates/v1_10#_more-work-on-terminal-runner
One could also make a tasks.json with two tasks with the second having a dependsOn on the first one as shown in Ben Creasy post, the two tasks would get executed when the second one is called. I needed to be able to execute one, the other or both. Many thanks to Ben, I had a hard time finding a solution before hitting this post.
BTW, when including a shell file, the commands are run with reference to the project folder, not the one where the script is located.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": [
"$tsc"
],
"group": "build",
"identifier": "build"
},
{
"label": "Copy files",
"type": "shell",
"command": "./scripts/copysrc.sh",
"windows": {
"command": ".\\scripts\\copysrc.cmd"
},
"group": "build",
"presentation": {
"reveal": "always"
},
"problemMatcher": [],
"dependsOn": "build"
},
{
"label": "Build and copy",
"dependsOn": [
"build",
"Copy files"
],
"group": "build",
"problemMatcher": []
}
]
}

How do I run multiple vscode tasks simultaneously?

For example, running a typescript watch task & a gulp task at the same time. (Without using the terminal)
I use a tasks.json like this to run two watch tasks simultaneously:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Watch all",
"dependsOn": [
"Watch package 'core'",
"Watch package 'ui'"
],
"dependsOrder": "parallel",
"group": "build",
"problemMatcher": [
"$tsc-watch"
],
"isBackground": true
},
{
"label": "Watch package 'core'",
"type": "typescript",
"tsconfig": "packages/core/tsconfig.json",
"option": "watch",
"problemMatcher": [
"$tsc-watch"
],
"group": "build"
},
{
"label": "Watch package 'ui'",
"type": "typescript",
"tsconfig": "packages/ui/tsconfig.json",
"option": "watch",
"problemMatcher": [
"$tsc-watch"
],
"group": "build"
}
]
}
When you open the build menu in vscode then you can choose to run the two separate watch tasks or the "Watch all" task which runs the other two tasks.
I guess you can easily replace one of the watch tasks with your gulp task.
See running multiple tasks. You can use the "dependsOn" key in a version 2.0.0 tasks.json. Example from above link:
{
"version": "2.0.0",
"tasks": [
{
"taskName": "Client Build",
"command": "gulp",
"args": ["build"],
"isShellCommand": true,
"options": {
"cwd": "${workspaceRoot}/client"
}
},
{
"taskName": "Server Build",
"command": "gulp",
"args": ["build"],
"isShellCommand": true,
"options": {
"cwd": "${workspaceRoot}/server"
}
},
{
"taskName": "Build",
"dependsOn": ["Client Build", "Server Build"]
}
]
}
Apparently, this is still preliminary? and so difficult to find documentation unless I am just missing it. But I tested it and it works. It was added in vscode 1.10.
VS Code has a built in Task Runner that you can configure with multiple tasks.
In VS Code, type Ctrl+Shift+P and search for "Tasks: Configure Task Runner." A tasks.json file will be created. Here's some sample code showing how to configure multiple tasks.
{
"version": "0.1.0",
"tasks": [
{
"taskName": "tsc",
"command": "tsc",
"args": ["-w"],
"isShellCommand": true,
"isBackground": true,
"problemMatcher": "$tsc-watch"
},
{
"taskName": "build",
"command": "gulp",
"args": ["build"],
"isShellCommand": true
}
]
}
Run the task by pressing Ctrl+Shift+P and searching for "Task: Run task."
See more documentation at https://code.visualstudio.com/docs/editor/tasks.