Skip to content

Ability to set default tasks based on file extension (or glob) #88106

@jasonwilliams

Description

@jasonwilliams

Motivation

our codebase has a mix of different languages. Some .php files some .js files for example.

I need to keep switching the default task depending on which file I’m working on, it can become quite frustrating.

Is it possible to have a default task for that file extension? So when I’m working in JS I can just run the default task, and when I’m in .php I can run the default task for that?

launch.json example from a typical project

[
    {
      "label": "Test JS File",
      "type": "shell",
      "command": "yarn jest ${file}",
      "extension": "js",
      "group": {
        "kind": "test",
      },
    },
    {
      "label": "Test PHP File",
      "type": "shell",
      "command": "phpunit ${file}",
      "extension": "php",
      "group": {
        "kind": "test",
      },
    }
]

Backwards Compatibility

  • Option 1: Default tasks can work as they are now, if a task based on file extension is added this can override the default task. So extension based tasks have higher priority.
  • Option 2: If no default task is set then fallback to checking if an extension has been set. This way defaultTask is still guaranteed to work as it does today
  • Option 3: Another option is to use globs instead, and run the task by default if it matches the particular glob pattern (see below). This can override defaultTask

Example table (if globs are used)

Current Open File Extension File Task
.js javascript Run JS file
.php PHP Run PHP File, or run PHP tests
.test.js Javascript test file Run Tests
.html HTML File Open browser with this file

Metadata

Metadata

Assignees

Labels

feature-requestRequest for new features or functionalityinsiders-releasedPatch has been released in VS Code Insiderson-testplantasksTask system issues

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions