11

I have a workspace that I use for smaller test programs I write to practice concepts. Hence, in VS Code, I have a build task for each file in the folder.

By default, VS Code builds the task with the "isDefault": true, flag. Ideally, I would like to figure out a way for me to build the currently opened file, so that when I switch files I am editing, I do not need to manually reset the flag to the build task I want to use.

To my knowledge, the VS Code Tasks Documentation doen't provide a solution. There must be a way to accomplish this without manually adjusting the flag. Any help is appreciated.

ifconfig
  • 6,242
  • 7
  • 41
  • 65
  • see https://medium.com/@jerrygoyal/run-debug-intellisense-c-c-in-vscode-within-5-minutes-3ed956e059d6 – GorvGoyl Apr 26 '18 at 13:44

2 Answers2

17

You can use the ${file} wildcard to pass the current file to your build program/script.

There's an example given in the docs for TS: https://code.visualstudio.com/Docs/editor/tasks#_operating-system-specific-properties

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "presentation": {
        "panel": "new"
    },
    "tasks": [
        {
            "taskName": "TS - Compile current file",
            "type": "shell",
            "command": "tsc ${file}",
            "problemMatcher": [
                "$tsc"
            ]
        }
    ]
}
Rob Lourens
  • 15,081
  • 5
  • 76
  • 91
  • Thank you for this, @RobLourens . I appreciate you taking the time to help. However, this method only works if all of the files I want to work with in the given workspace are able to be compiled the same way, i.e. a Python script. Is there a way to make it work for those? – ifconfig Aug 14 '17 at 23:49
  • 1
    And by the way, now that you mention it, `${relativeFile}` is more relevant, as the compiler is run from the workspace directory. – ifconfig Aug 14 '17 at 23:58
  • In that case, you should write your own script which is capable of checking the type of file, then invoking the correct compiler. Your task can invoke the script and simply pass the file path. But tasks themselves aren't capable of conditional logic. – Rob Lourens Aug 15 '17 at 00:20
  • How would I write a workspace script? It has to work with VS Code somehow, right? – ifconfig Aug 15 '17 at 01:05
  • 1
    Just any script - in bash or node or python or whatever, nothing vscode specific – Rob Lourens Aug 15 '17 at 15:36
6

To Build on Rob's Answer, you can also set a shortcut key combination to trigger each task by adding to keybindings.json, e.g.

{
    "key": "ctrl+h",
    "command": "workbench.action.tasks.runTask",
    "args": "Run tests"
}

where the args component is replaced with the task name.

You could use this in combination with ${file} in tasks.json to differentiate between the separate build types.

e.g. hit Ctrl+h for python and Ctrl+Shift+h for tsc

ifconfig
  • 6,242
  • 7
  • 41
  • 65
jdow
  • 316
  • 2
  • 9