Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 40 ยท Tasks and Terminal

Tasks and tasks.json

Automate builds, tests and scripts with VS Code tasks. Learn tasks.json fields, shell and process types, groups, dependsOn, variables, and input prompts.

Developers run the same commands all day: build, test, lint, start a server. A task is a command that VS Code knows about. You define it once in .vscode/tasks.json, then run it from the Command Palette, a keyboard shortcut or a menu, and the output appears in the terminal panel. Because the file is part of the project, the whole team gets the same buttons.

This lesson explains the file, the properties you will use most, task dependencies and the variables you can write inside a task. The runnable code blocks are stand-ins that model how variables are substituted and in what order tasks run; they do not start VS Code.

Running tasks

  • Terminal > Run Task... lists all tasks. Pick one to run it.
  • Terminal > Run Build Task (Ctrl+Shift+B) runs the task in the build group.
  • Terminal > Run Test Task runs the default test group task.
  • Tasks: Rerun Last Task repeats the last one.
  • Terminal > Configure Tasks... creates or opens tasks.json. VS Code can detect scripts from package.json, Gulp, Grunt, Make and others, so many tasks work with no file at all.

A first tasks.json

JSON
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "test",
      "type": "shell",
      "command": "npm test",
      "group": "test",
      "problemMatcher": []
    },
    {
      "label": "build",
      "type": "shell",
      "command": "npm run build",
      "group": { "kind": "build", "isDefault": true },
      "problemMatcher": []
    }
  ]
}

version must be "2.0.0". Each entry in tasks is one task.

Property Meaning
label Name shown in the task list and used in dependsOn
type shell (run through a shell) or process (run the executable directly)
command What to run
args Arguments, as an array (the safe way to avoid quoting problems)
group build, test or none, optionally with isDefault: true
options cwd, env and shell overrides
presentation How the terminal behaves: reveal, panel, clear, focus
problemMatcher How to turn output into Problems-panel entries (next lesson)
isBackground Marks a task that never ends, such as a watcher
dependsOn Other tasks that must run first
runOptions For example runOn: "folderOpen" to start on opening the folder

With type: "shell" the command goes through your shell, so pipes and && work. With type: "process" there is no shell, and you list arguments in args.

Dependencies

dependsOn runs other tasks first, in parallel by default. Use dependsOrder: "sequence" to run them in order:

JSON
{
  "label": "check",
  "dependsOn": ["lint", "test", "build"],
  "dependsOrder": "sequence",
  "problemMatcher": []
}

A task with dependsOn and no command is just a group of other tasks, which is perfect for a "run everything" button. A common recipe is to make a debug launch configuration wait for a build with preLaunchTask; see the debugging chapter's lesson on launch configurations.

flowchart LR
    L["lint"] --> T["test"] --> B["build"] --> C["check (done)"]

With dependsOrder: sequence, each step waits for the previous one.

Variables

You can use variables inside command, args, options.cwd and more:

Variable Value
${workspaceFolder} Path of the opened folder
${file} The file open in the active editor
${fileBasename} app.js
${fileBasenameNoExtension} app
${fileDirname} Directory of the active file
${relativeFile} Active file relative to the workspace
${env:NAME} Environment variable
${input:id} Value asked from the user (below)

Example: run the current Python file in a task named "run file":

JSON
{
  "label": "run file",
  "type": "shell",
  "command": "python",
  "args": ["${file}"],
  "presentation": { "reveal": "always", "panel": "shared" },
  "problemMatcher": []
}

Asking for input

Add an inputs array at the top level and reference it with ${input:id}:

JSON
{
  "version": "2.0.0",
  "inputs": [
    {
      "id": "env",
      "type": "pickString",
      "description": "Deploy target",
      "options": ["staging", "production"],
      "default": "staging"
    }
  ],
  "tasks": [
    {
      "label": "deploy",
      "type": "shell",
      "command": "./deploy.sh ${input:env}",
      "problemMatcher": []
    }
  ]
}

Input types are promptString, pickString and command. Quote the choices carefully; asking before a deploy is a good safety habit.

Platform overrides

Add windows, osx or linux blocks to change a command per platform:

JSON
{
  "label": "open docs",
  "type": "shell",
  "command": "xdg-open docs/index.html",
  "windows": { "command": "start docs\\index.html" },
  "osx": { "command": "open docs/index.html" }
}

Runnable stand-ins

JS Stand-in: substitute task variables

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Stand-in: order tasks by dependsOn

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Where is the task configuration stored?

    tasks.json in the .vscode folder holds task definitions. launch.json is for debug configurations.
  2. 2. Which shortcut runs the default build task?

    Ctrl+Shift+B runs the build-group default task; F5 starts debugging.
  3. 3. What is the difference between type shell and type process?

    Shell tasks can use pipes and && because a shell interprets them. Process tasks start the program with args.
  4. 4. How do you make tasks run one after another?

    dependsOn lists prerequisites; by default they run in parallel, and dependsOrder sequence makes them sequential.
  5. 5. What does ${file} contain?

    ${file} expands to the active editor's file path; ${workspaceFolder} is the folder.

Summary

  • Tasks are commands defined in .vscode/tasks.json and run from the Terminal menu or Ctrl+Shift+B.
  • Key properties: label, type, command, args, group, dependsOn, presentation, problemMatcher.
  • dependsOn composes tasks; dependsOrder: "sequence" orders them.
  • Variables such as ${workspaceFolder} and ${file} make tasks reusable; ${input:id} asks the user.
  • Platform blocks (windows, osx, linux) override commands per system.

Next: Problem Matchers