Cannot define multiple commands in tasks.json
Hi,
I cannot find a way to define multiple tasks which executes different commands in the .vscode/tasks.json file of my project directory.
From what I understood, I can only declare a single TaskConfiguration within this file. Am I wrong?
To make my problem more understandable, let's say I would like to define 3 tasks with their own set of arguments:
- foo:
foo --arg - bar1:
bar --arg 1 - bar2:
bar --arg 2
How can I make tasks.json reflect this need?
Let me suggest a simple example to implementation (tasks.json) ....
{
// ROOT WITHOUT COMMAND
"version": "0.1.0",
"isShellCommand": true,
"showOutput": "silent",
"args": [],
"tasks": [
{
//CHILDREN WITH COMMAND ;)
"taskName": "Build Type Script",
"suppressTaskName": true,
"isBuildCommand": true,
"command": "tsc",
"args": ["tsc -p ."]
},
{
"taskName": "Open Browser",
"suppressTaskName": true,
"command": "live-server",
"args": ["--open public_html"]
}
]
}
AND CTRL+SHIFT+B:
{ "key": "ctrl+shift+b", "command": "workbench.action.tasks.runTask" }
Hi, I need the feature too :+1: But, priority is low. Because, we can multiple command with the trick: http://qiita.com/usagi/items/5a0f4edc99420173abb3 ( Sorry, its wrote in Japanese. )
The abstract of the trick are:
- Use the top-level command to
shorcmd.exeorpowershell.exeor any other common shell program. - Set the top-level args to
-c( withsh) or/c( withcmd.exe) or-Command( withpowershell.exe). - Set sub-level args to any your command with
\"quoted. ( eg. :"args" : "\"your-command arg1 arg2 arg3\"")
Example tasks.json:
{ "version": "0.1.0"
, "command": "sh"
, "isShellCommand": true
, "showOutput": "always"
, "args": [ "-c" ]
, "tasks":
[ { "taskName": "la"
, "suppressTaskName": true
, "args": [ "\"ls -la\"" ]
}
, { "taskName": "hoge"
, "suppressTaskName": true
, "args": [ "\"echo hoge\"" ]
}
, { "taskName": "build"
, "suppressTaskName": true
, "args": [ "\"mkdir -k build; cd build; cmake ..; make\"" ]
}
, { "taskName": "debug"
, "suppressTaskName": true
, "args": [ "\"cd build; gdb bin/my-executable\"" ]
}
]
}
Its can run in the current release version ( 0.10.3 ). :rabbit:
Nice @usagi , this is my solution:
{
"version": "0.1.0",
"command": "cmd",
"isShellCommand": true,
"showOutput": "silent",
"args": ["/C"],
"tasks": [
{
"taskName": "Build Type Script",
"suppressTaskName": true,
"isBuildCommand": true,
"args": ["tsc -p ."]
},
{
"taskName": "Open Browser",
"suppressTaskName": true,
"isBuildCommand": true,
"args": ["live-server --open=public_html"]
}
]
}
tip: cmd for windows, sh for linux
shortcut: (override default build command)
{ "key": "ctrl+shift+b", "command": "workbench.action.tasks.runTask" }
when i hit ctrl+shift+b, subtasks show up:

Bonus:
you can define your own shortcut:
{ "key": "ctrl+shift+alt+o", "command": "workbench.action.tasks.runTask/taskName" }
This is very important. Say I want a test command and a build command. The test command needs to run mocha and the build command babel or tsc for example. "command" should be allowed for individual tasks in the tasks array.
The workaround is nice, but it is not cross-platform and quotes have to be escaped. For cross-platform you would have to define
{
"linux": {
"command": "sh",
"args": ["-c"]
},
"osx": {
"command": "sh",
"args": ["-c"]
},
"windows": {
"command": "powershell",
"args": ["-Command"]
}
}
and then duplicate the tasks property for every single OS :/
@felixfbecker It's nice follow-up :+1: We can use "windows", "linux", and "osx" for cross-platform portability.
Another plus one - and in my case, I can't use the workaround since I need a per OS switch and I can't rely on sh or cmd.
@cfjedimaster Why can't you use the workaround exactly?
Because my top level command would be start on Windows and open on OSX. So I can't use just cmd or just start and pass different args.
@cfjedimaster You mean like this?
{
"version": "0.1.0",
"isShellCommand": true,
"osx": {
"command": "sh",
"args": ["-c"],
"tasks": [
{"name": "run", "args": ["open mysuperawesomefile.txt"]}
]
},
"windows": {
"command": "cmd",
"args": ["/C"],
"tasks": [
{"name": "run", "args": ["start mysuperawesomefile.txt"]}
]
}
}
Yes. But to be clear, I wouldn't want to replicate all the tasks for both. Basically, I want to be able to do N tasks per file as the original requestor suggested. For me, 9 of the 10 tasks I want can all be in one definition. One of them is different though and needs to do X for Windows and Y for OSX.
Yeah I know it is a lot of code duplication, which is why they should really implement this feature. But for the time being, it's what works.
@danielschmitz From your example, only the first taskName runs when I press Ctrl+Shift+B. I don't get presented with a list as in your screenshot.
task does however present me with that list and both my options runs.
{
"version": "0.1.0",
"command": "cmd",
"isShellCommand": true,
"showOutput": "silent",
"args": ["/C"],
"tasks": [
{
"taskName": "Build CustApp",
"suppressTaskName": true,
"isBuildCommand": true,
"args": ["${cwd}/Sw/A_XMC/make_all.bat"]
},
{
"taskName": "GTAGS",
"suppressTaskName": true,
"isBuildCommand": true,
"args": ["gtags"]
}
]
}
Sorry @danielschmitz I misread your comment on adding workbench.action.tasks.runTask. Though I created a ticket for the same popup behavior with workbench.action.tasks.build.
https://github.com/Microsoft/vscode/issues/2840
@bbenoist I set up my VS Code tasks to use my Gruntfile.js tasks.
I simply hit F1 and enter task test and it runs my tests. I can do this for any task I have configured.
{
"version": "0.1.0",
"command": "grunt",
"isShellCommand": true,
"args": [
"--no-color"
],
"tasks": [
{
"taskName": "clean",
"args": [],
"isBuildCommand": false,
"problemMatcher": "$msCompile"
},
{
"taskName": "build",
"args": [],
"isBuildCommand": true,
"problemMatcher": "$tsc"
},
{
"taskName": "tslint",
"args": [],
"isBuildCommand": false,
"problemMatcher": "$msCompile"
}
]
}
@brennanMKE Thanks for the info but I needed to call different executables for different tasks. See @danielschmitz's example implementation for a practical example. https://github.com/Microsoft/vscode/issues/981#issuecomment-162330360
@danielschmitz @usagi @felixfbecker @cfjedimaster @csholmq Thanks everyone for the feedback and suggestions!
FYI, I ended up using Jake to define all my tasks and pushed my concept up to calling a vscode-configure task which makes the Jakefile regenerate its tasks and the appropriate .vscode configuration from the content of the JSON file.
First here is a bit of context about the app I wanted to configure VS Code for:
- Native development: C++ and Fortran
- Build is CMake-based.
- Daily usage of remote clusters and Vagrant boxes to build and debug.
- Cross platform code on both Windows and Linux.
My custom Jakefile allowed me to add the following capabilities to VS Code:
- Multiple target configuration management. You can create variants of any property.
- Execute task locally, via SSH or via Vagrant SSH and WinRM commands.
- Support remote debugging by generating appropriate configuration for
webfreaks.debug. - Support Error and Warning locations by generating the appropriate problem matchers. It even works when building via SSH.
- Allow configuration inheritance to prevent configuration duplication.
- Declare virtual configurations whose purpose is only to be inherited.
- Any task can make a reference to a single custom problem matcher.
Unfortunately, I cannot share my work for now because I need a special agreement for this. However, I can show you an example of a JSON configuration which would be compatible with the system I described:
{
"defaultConfig": "ubuntu-debug",
"problemMatchers": {
"gcc": {
"owner": "gcc",
"fileLocation": [ "relative", "${workspaceRoot}" ],
"pattern": {
"regexp": "^(${sourceDir}\\/)?(.*):(\\d+):\\d+:\\s+(warning|error):\\s+(.*)$",
"file": 2, "line": 3, "severity": 4, "message": 5
}
},
"gfortran": {
"owner": "gfortran",
"fileLocation": [ "relative", "${workspaceRoot}" ],
"pattern": [
{
"regexp": "^(${sourceDir}\\/)?(.*):(\\d+)\\.\\d+:\\s*$",
"file": 2, "line": 3
},
{
"regexp": "^\\s*(.*)$",
"code": 1
},
{ "regexp": "^.*$" },
{
"regexp": "(Error|Warning):\\s*(.*)$",
"severity": 1, "message": 2, "loop": true
}
]
}
},
"configs": {
"local": {
"enabled": false,
"sourceDir": "${workspaceRoot}",
"cpus": 8,
"problemMatchers": [ "$msCompile" ]
},
"vagrant": {
"enabled": false,
"useVagrant": true,
"sourceDir": "/src",
"cpus": 1,
"problemMatchers": [ "gcc", "gfortran" ]
},
"debug": {
"enabled": false,
"cmake": { "buildType": "Debug" }
},
"release": {
"enabled": false,
"cmake": { "buildType": "Release" }
},
"ubuntu-debug": {
"enabled": true,
"buildDir": "/home/vagrant/build-debug",
"parents": [ "vagrant", "debug" ]
},
"ubuntu-release": {
"enabled": true,
"buildDir": "/home/vagrant/build-release",
"parents": [ "vagrant", "release" ]
},
"local-debug": {
"enabled": true,
"build-dir": "${workspaceRoot}/build-debug",
"parents": [ "local", "debug" ]
},
"local-release": {
"enabled": true,
"build-dir": "${workspaceRoot}/build-release",
"parents": [ "local", "release" ]
}
}
}
Alternatives which would provide all the capabilities listed below in a simpler configuration file are welcome :smiley:
Not 100% related but interested people can take a look at the new Shell extension I made. It's able to run shell commands directly from the editor :rocket:
I think the schema in this request could solve the problem: https://github.com/Microsoft/vscode/issues/4475
It allows to define multiple commands and it is very easy to transform to the current tasks.json schema.
@danielschmitz Hmm, the pop up seems to work, but that "runTask/<task_name>" doesn't seem to work. It says "No handler found" when I invoke the keybinding. I'd like to keybind 2 different task_names. Can't seem to find any documentation on that "/<task_name>"....
I'm getting questions on this issue for my Status Bar Tasks extension. My extension simply passes whatever is in the arguments directly to Node's child_process module for execution. To make VS Code happy, I have a shell command in the top-level command (aka the @usagi method).
It looks like this slipped for the April iteration. Can we expect this to be fixed in May?
@DanB91 this code is a suggestion.