Tasks and problem matchers

VS Code · Advanced · 10 min

Tasks give every project one-key build and test commands and integrate tool errors into the Problems panel.

A task is a named command VS Code can run for you. Many are auto-detected (npm scripts, Make, Gulp...); run any with "Tasks: Run Task".

Custom tasks live in .vscode/tasks.json:

  • label names it; type: "shell" runs command in a shell; "process" runs a program directly.
  • group marks it as the default build task (Ctrl+Shift+B) or test task.
  • dependsOn chains tasks, with dependsOrder: "sequence" to run them in order.
  • presentation controls the terminal: reuse, focus, clear.
  • options.cwd and options.env set directory and environment.

Problem matchers parse the task output and turn errors into entries in the Problems panel with clickable locations. Built-in ones include $tsc, $eslint-stylish, $gcc and $go; you can also define a regex-based matcher. For long-running watch tasks (tsc --watch, a dev server), set isBackground: true with a background matcher so VS Code knows when the task is "ready", which matters when another configuration uses it as preLaunchTask.

Tasks are also how debugging setups build before launching. Keep them in the repository so every developer gets the same commands, and prefer calling project scripts (npm run build) rather than duplicating logic in tasks.json.

Example

// .vscode/tasks.json
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build",
      "type": "shell",
      "command": "npx tsc -p .",
      "group": { "kind": "build", "isDefault": true },
      "problemMatcher": ["$tsc"]
    },
    {
      "label": "test",
      "type": "shell",
      "command": "npm test",
      "group": "test",
      "dependsOn": ["build"]
    }
  ]
}

Remember

  • tasks.json defines labelled commands; Ctrl+Shift+B runs the default build
  • problemMatcher turns output into clickable Problems
  • isBackground + matcher for watch tasks used as preLaunchTask

Watch out for

  • A watch task used as preLaunchTask without isBackground, so the debugger waits forever for it to finish.

Check yourself

  1. What does `"problemMatcher": ["$tsc"]` do?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: code.visualstudio.com/docs/debugtest/tasks

Builds on

Unlocks

Nothing builds on this yet.