Integrate with External Tools via Tasks
AI agents: See llms.txt for documentation discovery and navigation. Documentation articles are also available as Markdown by appending .md to the article URL. 🎬 Watch The Story of VS Code! VS Code is now on Instagram, follow @vscode.ig for updates, tips, and more. Rewatch GitHub Copilot Day. Integrate with External Tools via Tasks View as Markdown Ask in VS Code Ask in VS Code Insiders Lots of tools exist to automate tasks like linting, building, packaging, testing, or deploying software systems.
Examples include the TypeScript Compiler , linters like ESLint and TSLint as well as build systems like Make , Ant , Gulp , Jake , Rake , and MSBuild . These tools are mostly run from the command line and automate jobs inside and outside the inner software development loop (edit, compile, test, and debug). Given their importance in the development life cycle, it is helpful to be able to run tools and analyze their results from within VS Code.
Tasks in VS Code can be configured to run scripts and start processes so that many of these existing tools can be used from within VS Code without having to enter a command line or write new code. Workspace or folder specific tasks are configured from the tasks.json file in the .vscode folder for a workspace. Extensions can also contribute tasks using a Task Provider , and these contributed tasks can add workspace-specific configurations defined in the tasks.json file.
Note: Task support is only available when working on a workspace folder. It is not available when editing single files. TypeScript Hello World Let's start with a simple "Hello World" TypeScript program that we want to compile to JavaScript. Create an empty folder "mytask", generate a tsconfig.json file and start VS Code from that folder. mkdir mytask cd mytask tsc --init code . Now create a HelloWorld.ts file with the following content function sayHello ( name : string ): void { console . log ( `Hello ${ name } !` ); } sayHello ( 'Dave' ); Pressing ⇧⌘B (Windows, Linux Ctrl+Shift+B ) or running Run Build Task from the global Terminal menu show the following picker: The first entry executes the TypeScript compiler and translates the TypeScript file to a JavaScript file.
When the compiler has finished, there should be a HelloWorld.js file. The second entry starts the TypeScript compiler in watch mode. Every save to the HelloWorld.ts file will regenerate the HelloWorld.js file. You can also define the TypeScript build or watch task as the default build task so that it is executed directly when triggering Run Build Task ( ⇧⌘B (Windows, Linux Ctrl+Shift+B ) ). To do so, select Configure Default Build Task from the global Terminal menu.
This shows you a picker with the available build tasks. Select tsc: build or tsc: watch and VS Code will generate a tasks.json file. The one shown below makes the tsc: build task the default build task: { // 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" : { "kind" : "build" , "isDefault" : true } } ] } The tasks.json example above does not define a new task.
It annotates the tsc: build tasks contributed by VS Code's TypeScript extension to be the default build task. You can now execute the TypeScript compiler by pressing ⇧⌘B (Windows, Linux Ctrl+Shift+B ) . Task auto-detection VS Code currently auto-detects tasks for the following systems: Gulp, Grunt, Jake, and npm. We are working with the corresponding extension authors to add support for Maven and the C# dotnet command as well.
If you develop a JavaScript application using Node.js as the runtime, you usually have a package.json file describing your dependencies and the scripts to run. If you have cloned the eslint-starter example, then executing Run Tasks from the global menu shows the following list: If you have not already done so, install the necessary npm modules by running npm install . Now open the server.js file and add a semicolon to the end of a statement (note the ESLint starter assumes statements without a semicolon) and execute the Run Tasks again.
This time select the npm: lint task. When prompted for the problem matcher to use, select ESLint stylish Executing the task produces one error, shown in the Problems view: In addition, VS Code created a tasks.json file with the following content: { // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version" : "2.0.0" , "tasks" : [ { "type" : "npm" , "script" : "lint" , "problemMatcher" : [ "$eslint-stylish" ] } ] } This instructs VS Code to scan the output of the npm lint script for problems using the ESLint stylish format.
For Gulp, Grunt, and Jake, the task auto-detection works the same. Below is an example of the tasks detected for the vscode-node-debug extension. Tip: You can run your task through Quick Open ( ⌘P (Windows, Linux Ctrl+P ) ) by typing 'task', Space and the command name. In this case, 'task lint'. Task auto detection can be disabled using the following settings: { "js/ts.tsc.autoDetect" : "off" , "grunt.autoDetect" : "off" , "jake.autoDetect" : "off" , "gulp.autoDetect" : "off" , "npm.autoDetect" : "off" } Custom tasks Not all tasks or scripts can be auto-detected in your workspace.
Sometimes it is necessary to define your own custom tasks. Assume you have a script to run your tests in order to set up some environment correctly. The script is stored in a script folder inside your workspace and named test.sh for Linux and macOS and test.cmd for Windows. Run Configure Tasks from the global Terminal menu and select the Create tasks.json file from template entry. This opens the following picker: Note: If you don't see the list of task runner templates, you may already have a tasks.json file in your folder and its contents will be open in the editor.
Close the file and either delete or rename it for this example. We are working on more auto-detection support, so this list will get smaller and smaller in the future. Since we want to write our own custom task, select Others from the list. This opens the tasks.json file with a task skeleton. Replace the contents with the following: { // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version" : "2.0.0" , "tasks" : [ { "label" : "Run tests" , "type" : "shell" , "command" : "./scripts/test.sh" , "windows" : { "command" : ". \\ scripts \\ test.cmd" }, "group" : "test" , "presentation" : { "reveal" : "always" , "panel" : "new" } } ] } The task's properties have the following semantic: label : The task's label used in the user interface. type : The task's type.
For a custom task, this can either be shell or process . If shell is specified, the command is interpreted as a shell command (for example: bash, cmd, or PowerShell). If process is specified, the command is interpreted as a process to execute. command : The actual command to execute. windows : Any Windows specific properties. Will be used instead of the default properties when the command is executed on the Windows operating system. 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 . presentation : Defines how the task output is handled in the user interface. In this example, the Integrated Terminal showing the output is always revealed and a new terminal is created on every task run. options : Override the defaults for cwd (current working directory), env (environment variables), or shell (default shell).
Options can be set per task but also globally or per platform. Environment variables configured here can only be referenced from within your task script or process and will not be resolved if they are part of your args, command, or other task attri
The rest of this page is on the live site.