Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 274
f10w Avatar asked Jul 18 '26 16:07

f10w


1 Answers

Update for latest vscode

The following will create a default "build" script, so you can use the keyboard shortcut to build your project. (Below for a javascript project, but shows general outline for other languages/projects.)

(1) Assuming you have a script named "build.js" at the root of your project.

(2) Check that you have a ".vscode" hidden folder at the root of your project. If not, create it.

(3) Create a file named "tasks.json" in the ".vscode" folder at the root of project (workspace) with the following contents:

// .vscode/tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "npm",
            "script": "mybuildscript", // use same name as in package.json
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
     ]
}

(4) In your package.json, add "scripts" as:

// package.json
{
    "name": "my",
    "version": "1.0.0",
    "description": "",
    "scripts": {
        "mybuildscript": "node ./build.js"
    },
    "dependencies": {
        "myfs": "^1.0.22"
    }
}

I created a rudimentary vscode extension that does all this for you: https://marketplace.visualstudio.com/items?itemName=gieson.make-build-task

The extension isn't perfect (doesn't cover all the possible ways a project/workspace can be configured), but it's a good starting point.

Here's the repo: https://github.com/bobtherobot/make-build-task

like image 190
bob Avatar answered Jul 25 '26 19:07

bob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!