How to run tasks in sequence? - visual-studio-code

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.

Related

VS CODE SaveAll from Task

I want to execute SaveAll from a task such as evoking workbench.action.files.saveAll from within Task.
The objective is to make sure all files are saved before triggering series of other Tasks.
Task #1: SaveAll (workbench.action.files.saveAll)
Task #2: Run Grunt that dependsOn Task#1
The tasks.json looks something like this
{
"version": "2.0.0",
"tasks": [
{
"command": "workbench.action.files.saveAll",
"label": "SaveAllFiles",
},
{
"type": "grunt",
"task": "default",
"label": "Execute Grunt",
"dependsOn": [
"SaveAllFiles"
],
"problemMatcher": []
}
]
}
In theory, all I should have to do is execute the task labeled Execute Grunt, and it will call the SaveAllFiles task. To be precise, I don't want to trigger tasks based on Save or SaveAll events; there are times when I want to save files, but not trigger other tasks.
{
"label": "SaveAllFiles",
"command": "${command:workbench.action.files.saveAll}",
"type": "shell",
"problemMatcher": []
},
{
"type": "grunt",
"task": "default",
"label": "Execute Grunt",
"dependsOn": [
"SaveAllFiles"
],
"dependsOrder": "sequence", // need this, "parallel" is the default otherwise
"problemMatcher": []
}
${command:workbench.action.files.saveAll} is the general form for calling a vscode command, you can also call extension or macro commands this way.
The info on this is in https://code.visualstudio.com/docs/editor/variables-reference#_command-variables which makes it a little tricky to find as it isn't in the Tasks documentation.

RUST cargo run task with arguments in vscode

Is there a way to specify arguments to a RUST cargo command running as VS CODE task?
Or should I be trying this as an NPM script? (of course, this is RUST, so I am using CARGO and npm, creating a package.json would be odd).
The Build task works fine:
"version": "2.0.0",
"tasks": [
{
"type": "cargo",
"subcommand": "build",
"problemMatcher": [
"$rustc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
But I have no idea where to put the arguments since I want it to be
$cargo run [filename]
{
"type": "cargo",
"subcommand": "run",
"problemMatcher": [
"$rustc"
]
}
There absolutely is, the args option allows you to pass additional arguments to your tasks and you can use various ${template} parameters to pass things like the currently opened file.
It's also worth calling out that the type of the command should probably be shell, with cargo being specified as the command itself.
For your use case, you may consider using the following (to execute cargo run $currentFile).
{
"version": "2.0.0",
"tasks": [
{
"label": "run",
"type": "shell",
"problemMatcher": [
"$rustc"
],
"command": "cargo",
"args": [
"run",
"${file}"
]
}
]
}

How to run multiple tasks in VS Code on build?

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"

Visual Studio Code: running preLaunchTask with multiple tasks

I am trying to figure out how to run multiple tasks at once in the prelaunchtask of the launch.json file.
My code in the tasks.json is as follows:
"version": "2.0.0",
"tasks": [
{
"label": "CleanUp_Client",
"type": "shell",
"command": "rm",
"args": [
"-f",
"Client"
],
},
{
"label": "Client_Build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"client.cpp",
"-o",
"Client",
"-lssl",
"-lcrypto"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$gcc"
}
]
In the launch.json for the preLaunchTask parameter if I only put the build task it works, however I want to run multiple tasks, in this case is the CleanUp_Client and Client_Build.
I tried adding another preLaunchTask - However it looks like you can only use that parameter once, so then I tried:
"preLaunchTask": "build" + "clean",
"preLaunchTask": "build"; "clean",
"preLaunchTask": "build" & "clean",
"preLaunchTask": "build" && "clean",
All with no success, not the correct syntax.
Also as a second part to this I would like to know how the group part of this works, and what it means for "isDefault": true.
For your reference: https://code.visualstudio.com/docs/editor/tasks
Here is something that will work. Basically you make another task in which you include all the other tasks that you want to run on your preLaunchTask with the dependsOn keyword.
Code for reference:
"tasks": [
{
"label": "CleanUp_Client",
"type": "shell",
"command": "rm",
"args": [
"-f",
"Client"
]
},
{
"label": "Client_Build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"client.cpp",
"-o",
"Client",
"-lssl",
"-lcrypto"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$gcc"
},
{
"label": "Build",
"dependsOn": [
"CleanUp_Client",
"Client_Build"
]
}
]
In this case you would set your preLaunchTask to "Build" and it will run both tasks.
I am curious if anybody else knows an alternative or the correct syntax to just run several tasks from the launch.json preLaunchTask
I agree with #Revx0r answer, but there is important notice: you need to add to last task dependsOrder field, if you want to run it in sequence:
{
"label": "Build",
"dependsOrder": "sequence",
"dependsOn": [
"CleanUp_Client",
"Client_Build"
]
}

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.