Skip to content

Generate VSCode Tasks

Roman Kuzmin edited this page Aug 20, 2022 · 13 revisions

The script New-VSCodeTask.ps1 generates VSCode tasks from the specified or default build script and the specified or global Invoke-Build as the task runner.

In addition, if the file .vscode/tasks-merge.json exists or another file is specified by the parameter Merge then its tasks are merged with generated. Thus, you can add extra tasks or alter properties of generated. Example: tasks-merge.json

The default task ("." or the first) becomes so called VSCode build task which is invoked by Ctrl+Shift+B.

To invoke another task, use F1 or Ctrl+Shift+P and type Run Task. Then type a task name or select it from the list. Even better, set your keyboard shortcut in keybindings.json, e.g.

{ "key": "ctrl+k k", "command": "workbench.action.tasks.runTask" }

Included task names consist of alphanumeric characters, "_", ".", "-", with the first character other than "-". Other tasks are excluded. Additional filters may be specified by the parameter WhereTask.


How to use the script

Step 1. Change to your VSCode workspace directory. The new file to be generated is .vscode/tasks.json. Check for the existing file, it will be replaced, with confirmation if it was not generated by this tool.

Step 2. Invoke New-VSCodeTask.ps1.

If the build script is in the workspace root, Invoke-Build.ps1 is in the workspace, or in the path, or available as the module command then parameters are not needed:

New-VSCodeTask

Otherwise, specify the parameter BuildFile or InvokeBuild or both. If the workspace directory is not going to be moved then absolute paths may be used. Otherwise use workspace relative paths. Example:

New-VSCodeTask ./Scripts/Build.ps1 ./packages/InvokeBuild/Invoke-Build.ps1

Note, the second argument makes sense only if there are several versions of Invoke-Build.ps1 in the workspace for some reasons. Otherwise Invoke-Build.ps1 will be discovered automatically.


Notes

When you run generated tasks, build errors are shown in the VSCode terminal with links to errors and tasks. Use Ctrl-Click to navigate to sources.

When you generate VSCode tasks make sure you do not have anything important in your .vscode/tasks.json. The file is replaced. Consider moving some tasks to .vscode/tasks-merge.json which is merged with generated.

Do not edit the generated file. Modify the build script and tasks-merge.json. VSCode tasks do not have to be regenerated on every change. This is needed when you add, remove, rename tasks, change script locations or tasks-merge.json.