How to chain tasks in Visual Studio Code using only tasks.json? - visual-studio-code

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": []
}
]
}

Related

VS CODE / MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file

I've tried many different ways but I get the error MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file. in Visual Studio code when trying to run my Net Core.
I think I have this configured correctly. The project builds with no errors. I even tried building a new empty project and still get the error.
launch.json
{
// 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": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/API/API/bin/Debug/net5.0/API.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
}
]
}
Tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "shell",
"args": [
"build",
// Ask dotnet build to generate full paths for file names.
"/property:GenerateFullPaths=true",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}

How to add multiple build tasks in Visual Studio Code?

I am trying to use Visual Studio Code to build a Haxe project. I would like to have two build commands, one to build with the -debug option, and one to build without it. Here is my tasks.json file code:
{
"version": "2.0.0",
"tasks":
[
{
"type": "lime",
"command": "test",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "build: flash",
"command": "haxelib",
"group": {
"kind": "build",
"isDefault": true
},
"args": [
"run",
"lime",
"build",
"flash",
],
"problemMatcher": [
"$haxe-absolute",
"$haxe",
"$haxe-error",
"$haxe-trace"
]
},
{
"label": "debug: flash",
"command": "haxelib",
"args": [
"run",
"lime",
"build",
"flash",
"-debug",
"-Dfdb"
],
"problemMatcher": [
"$haxe-absolute",
"$haxe",
"$haxe-error",
"$haxe-trace"
]
}
]
}
This gives me two tasks named "lime: test flash -debug" and "build: flash". The "lime: test flash -debug" command works fine, but when I run "build:flash" nothing happens. The haxelib command shows up in the terminal, but nothing pops up. I've read both the tasks documentation here and the tasks.json schema here and I still can't figure out how to do this. Can anyone tell me how to do this? Thanks.

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"
]
}

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.

Chain pre-launch tasks in VS Code

Is it possible to invoke more than one pre-launch task using VS Code?
I try to restore packages then build then run but I can only get to configure build.
My launch.json:
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/src/myProject/bin/Debug/netcoreapp1.0/myProject.dll",
"args": [],
"cwd": "${workspaceRoot}/src/myProject",
"stopAtEntry": false,
"externalConsole": false
},
My tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": [
"./**/project.json"
],
"isBuildCommand": true,
"showOutput": "always",
"problemMatcher": "$msCompile"
}
]
}
so I tried to specify the dotnet restore command however it does not work.
I know this is long over due. However, I think I figured out the solution. Steps I did.
Create a workspace to include all the projects in the workspace.
Go to Run and Debug, and click on "Add Config (workspace)
the format is the following:
{
"folders": [
{
"path": "project1"
},
{
"path": "project2"
},
],
"launch": {
"version": "0.2.0",
"compounds": [{
"name": "Chain Stars",
"configurations": [
"ConfigurationName1",
"ConfigurationName2",
]
}]
}
}
ConfigurationName1, and ConfigurationName2 is the profile name you would like to put them in sequence to launch your website.
4. Save the profile. In this case. "Chain Stars" is going to show up in the profile name for you to run it. Let me know if you have any questions.