visual-studio-code

Configure VS Code default Build Task based on file extension


I would like to know if there is a way to define a default Build Task for VS Code depending on file extension.

When working in some folder of Python code, I define the following Build Task:

{
    "version": "0.1.0",
    "command": "python",
    "isShellCommand": true,
    "showOutput": "always",
    "args": ["${file}"]
}

Then if next time I go to another Python folder, I have to redefine it again.

Is it possible to configure VS Code in such a way that if it detects the current file as a Python script, then it will automatically define the above Build Task?


Solution

  • This is possible, but it requires writing an extension (unless somebody has already written one with a tasks provider for Python). Since 1.14.0, there's a new API which allows extensions to dynamically provide tasks. Check out the Task Provider Example.

    Alternatiely, the Code Runner extension probably does the trick in this case as well. It doesn't use the Tasks system though.