Several "build tasks" for visual studio code (python) - visual-studio-code

My tasks.json looks like this:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
// A task runner that runs a python program
"command": "python3",
"presentation": {
"echo": true,
"reveal": "always",
"focus": true
},
"args": [
"${file}"
]
}
When I run ctrl+shift+B the top panel asks "Select the build task to run", and there's one alternative: python3. Now if I'd like to add a new build-task (for example a runspider command with scrapy), so it's added to the build tasks. How would I go about adding this?

You can define multiple tasks in your tasks.json by assigning an array of task objects to the tasks property, like this:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "python3",
"type": "shell",
"command": "python3",
"args": [
"${file}"
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": true
}
},
{
"taskName": "runspider",
"type": "shell",
"command": "runspider"
}
]
}
Also, Ctrl+Shift+B runs the default build task, so you might want to set your "workbench.action.tasks.runTask" keybinding.
{
"key": "ctrl+shift+b",
"command": "workbench.action.tasks.runTask"
}
Once that is done, you can select the task when you use workbench.action.tasks.runTask command, as shown below:
You can also choose your default build task by setting the "group" property on the task. Here, in the following snippet, your "python3" task will run as the default build task.
...
"tasks": [
{
"taskName": "python3",
"type": "shell",
"command": "python3",
"args": [
"${file}"
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": true
},
"group": {
"kind": "build",
"isDefault": true
}
},
{
"taskName": "runspider",
"type": "shell",
"command": "runspider"
}
]
...
You can read more about tasks here: Tasks in VSCode

Related

How can I trigger a command palette action from a task?

I have this task I've created:
{
"label": "Regen Coverage",
"type": "shell",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "dedicated"
},
"command": [
"go test ./internal/... --tags=dynamic,integration -coverpkg=./... -count=1 -coverprofile ./cover.out",
"gcov2lcov -infile=cover.out -outfile=cover.lcov",
]
}
I'd like to trigger a command palette action from an extension (the command is Coverage Gutters: Watch. I think this should be possible, based on this ticket: https://github.com/microsoft/vscode/issues/11396, but I have no idea what the command would be called, and the docs say that a command must return a string. Any ideas?
Thank you to #rioV8 I was able to find the commandID from the key binding GUI. This is the resulting task setup:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "run integration tests and cover",
"type": "shell",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "shared"
},
"command": [
"go test ./internal/... --tags=dynamic,integration -coverpkg=./... -count=1 -coverprofile ./cover.out",
"gcov2lcov -infile=cover.out -outfile=cover.lcov"
]
},
{
"label": "Regen and Watch Coverage",
"dependsOn": [
"run integration tests and cover"
],
"presentation": {
"reveal": "always",
"panel": "shared"
},
"command": [
"${command:coverage-gutters.watchCoverageAndVisibleEditors}"
],
"problemMatcher": []
}
]
}

Open terminal by default

Is there a way to open terminal by default in VSCode? I saw the questions here four years ago - VS Code - How to have Terminal pane open by default? not sure if there is any update.
You can use the tasks.json file to "auto-run" terminals with Tasks. See documentation at https://code.visualstudio.com/docs/editor/integrated-terminal#_automating-launching-of-terminals
tasks.json template:
{
"version": "2.0.0",
"presentation": {
"echo": false,
"reveal": "always",
"focus": false,
"panel": "dedicated",
"showReuseMessage": true
},
"tasks": [
{
"label": "Open terminal by default",
"dependsOn": [
"Terminal"
],
"runOptions": {
"runOn": "folderOpen"
}
},
{
// The name that shows up in terminal tab
"label": "PowerShell",
// The task will launch a shell
"type": "shell",
"command": "",
// Set the shell type
"options": {
"shell": {
"executable": "powershell.exe",
"args": []
}
},
// Mark as a background task to avoid the spinner animation on the terminal tab
"isBackground": true,
"problemMatcher": []
}
]
}

How to have task in VS Code run in current terminal not a new pane?

I have this task,
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Run Jest on Current File",
"type": "shell",
"command": "yarn",
"options": {
"env": {
"CI": "true"
}
},
"args": ["test", "${file}"],
"problemMatcher": [],
"presentation": {
"echo": false,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": false,
"clear": false
}
}
]
}
This shows what I mean by second terminal pane
I would like for this to run in the current terminal I have open instead of opening a new one that would close when I press any other button. It would be nice to not have to worry about a different terminal pane specifically for tasks. The task pane doesn't use my color theme, therefore it's harder to see what has passed or failed as well.

How can I see compiler's output when running task in Visual studio code?

Everytime I try to run the build task, I can't see anything except from The terminal process terminated with exit code: 1, how can I configure VSCode so I can see compiler's output?
Below is my task file and what I've got from running build task:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "C:/Program Files (x86)/Dev-Cpp/MinGW32/bin/g++",
"args": [
"-g",
"mysockets.cpp"
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true
},
"problemMatcher": "$gcc",
},
]
}

How to configure a VSCode Task to run a powershell script in the integrated terminal

In such a way that it is not in a sub shell. I need it to be able to prepare the environment...set environment variable.
"version": "0.1.0",
"command": "${workspaceFolder}/Invoke-Task.ps1",
/*"command": "powershell", creates subshell so doesn't work*/
"isShellCommand": false,
"args": [],
"showOutput": "always",
"echoCommand": true,
"suppressTaskName": true,
"tasks": [
{
"taskName": "task1",
"args": ["task1"]
},
{
"taskName": "task2",
"args": ["task2"]
}
]
I am sorry, but you are not correctly editing the .vscode/tasks.json file.
In your scenario, lets say you have some powershell script ./scripts/mycustomPSscript.ps1 you want to run as a vscode task. For such goal, edit the tasks file so it follows the below example:
{
"version": "2.0.0",
"tasks": [
{
"label": "Run-some-custom-script",
"detail": "Prepare some ENV variables for the deployment",
"type": "shell",
"command": "./../scripts/mycustomPSscript.ps1",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
}
}
]
}
This has been doable since 2017, if I get your ask correctly.
integrated-terminal-tasks README This extension allows a workspace to
define specific tasks that should be ran in VSCode's interactive
terminal
https://marketplace.visualstudio.com/items?itemName=ntc.integrated-terminal-tasks
Also, your post / query, could been seen as a duplicate of this...
Run Code on integrated terminal Visual Studio Code
Adding this configuration in the launch.json file did the trick for me
"version": "0.2.0",
"configurations": [
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Launch Current File",
"script": "put full path here\\launch.ps1",
"args": ["${file}"],
"cwd": "${file}"
},...
Not sure what you mean by 'integrated terminal' but the output does show up in the VSC terminal if this is what you're referring to.