How can I trigger a command palette action from a task? - visual-studio-code

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": []
}
]
}

Related

Devcontainer: how to open terminal with custom commands when opening the container?

When starting my devcontainer, I need to run multiple commands to be able to work on my project. For example for my Django project, I need to run:
python manage.py runserver
celery -A proj worker
celery -A proj beat
tailwindcss -w -i input.css -o output.css
Currently, I do this manually and this is a little bit painful.
I know there is a postStartCommand that I could use, but I'd be forced to group all my commands with && and I would not be able to follow each command logs individually.
What I'd like to do is to automate what I'm currently doing by hand: open different terminal windows and run a single command in each of them, to have this result:
Is there a way to do that? Or any workaround? Thanks.
You can automate things in VSCode using custom tasks.
Your scenario is a bit tricky though, so, I'd suggest:
Create 3 separate tasks that open a terminal and execute one of the commands that you need. Check this answer for an example of how it's done.
Create a compound task that runs the 3 tasks above.
As a complement to #Dorin Botan good answer, here is my configuration in tasks.json, using 2 groups of terminals like this:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// See https://code.visualstudio.com/docs/editor/tasks-appendix
"version": "2.0.0",
"tasks": [
{
"label": "Term 1",
"type": "process",
"command": "/bin/bash",
"isBackground": true, // removes the infinite spinner
"problemMatcher": [],
"presentation": {
"group": "main_tasks",
"reveal": "always",
"panel": "new",
"echo": false, // silence "Executing task ..."
}
},
{
"label": "Django server",
"type": "shell",
"command": "./manage.py runserver 127.0.0.1:8000",
"isBackground": true,
"presentation": {
"group": "main_tasks",
"reveal": "always",
"panel": "new",
}
},
{
"label": "Celery worker",
"type": "shell",
"command": "celery --app dj_config worker --uid=nobody --gid=nogroup --loglevel INFO",
"isBackground": true,
"presentation": {
"group": "background_tasks",
"reveal": "never",
"panel": "new",
"showReuseMessage": false,
}
},
{
"label": "Celery beat",
"type": "shell",
"command": "celery --app dj_config beat --uid=nobody --gid=nogroup --loglevel INFO",
"isBackground": true,
"presentation": {
"group": "background_tasks",
"reveal": "never",
"panel": "new",
"showReuseMessage": false,
},
},
{
"label": "Tailwind watcher",
"type": "shell",
"command": "sleep infinity", // FIXME with real command
"isBackground": true,
"presentation": {
"group": "background_tasks",
"reveal": "never",
"panel": "new",
"showReuseMessage": false,
}
},
{
// This is a compound task to run them all
"label": "Mango Terminals (David)",
"dependsOn": [
"Term 1",
"Django server",
"Tailwind watcher",
"Celery worker",
"Celery beat",
],
"dependsOrder": "parallel", // no dependencies between tasks
"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 re-use a VS Code task window, without closing it

My goal is to re-use a task window in VS Code. However, when I enter ctrl + c, the task stops, but then writes: "Terminal will be reused by tasks, press any key to close it.".
I don't want to close the window. It's frustrating because it forces me to open a new window and navigate to the correct directory.
I recorded a gif of the problem (It's the window on the right):
My task config look like this:
{
"label": "some label",
"type": "npm",
"script": "build",
"path": "some-path/",
"problemMatcher": [],
"runOptions": { "runOn": "folderOpen" },
"group": "build",
"presentation": {
"echo": true,
"reveal": "silent",
"focus": false,
"panel": "shared",
"showReuseMessage": false,
"clear": false,
"group": "build"
}
}
I tried various combination of the presentation properties, but to no help.
Related feature request on VS code is here.
I don't think this is possible and it may be by design.
If you look at the schema of tasks.json, you see:
/**
* The description of a task.
*/
interface TaskDescription {
/**
* The task's name
*/
label: string;
/**
* The type of a custom task. Tasks of type "shell" are executed
* inside a shell (e.g. bash, cmd, powershell, ...)
*/
type: 'shell' | 'process';
//...
}
The type of a custom task. Tasks of type "shell" are executed inside a shell
So to me this implies that if you have a countdown task of type "shell" running this command seq 10 1, behind the scene it would do:
devbox:~ dev$ bash -c "seq 10 1"
10
9
8
7
6
5
4
3
2
1
devbox:~ dev$
As you can see it immediately exits and I'm not sure you can do anything about it. (I may be wrong though)
Even if you set a task of type "process" (command being the path to an executable), it doesn't allow you to reuse the terminal.
Having said that you can force it but VS Code wouldn't be too happy about it: (notice the && sh at the end of the command)
{
"version": "2.0.0",
"tasks": [
{
"label": "10 9 8 ...",
"type": "shell",
"command": "seq 10 1 && sh",
"presentation": {
"echo": true,
"focus": true,
"reveal": "always",
"panel": "shared",
},
"problemMatcher": [],
}
]
}
When you run the task, you do get another shell immediately:
However if you re-run the same task, then VS Code gets grumpy:
The fact that I couldn't see an option in .vscode/settings.json to support your use case makes me think that it really is a by design choice:
I found yet another solution for this that works great for me:
using bash:
"tasks": [
{
"label": "start server",
"type": "shell",
"command": "bash -c 'cd backend && npm run dev; exec bash'",
"isBackground": false,
"presentation": {
"panel": "new",
"close": true
},
"runOptions": {
"runOn": "folderOpen"
}
}
]
or if you use fish (like me):
"tasks": [
{
"label": "start server",
"type": "shell",
"command": "fish -C 'cd backend && npm run dev'",
"isBackground": false,
"presentation": {
"panel": "new",
"close": true
},
"runOptions": {
"runOn": "folderOpen"
}
}
]
It sounds like you want to launch a shell in the right folder after the task is complete. I'm not sure if this is the best way to do it, but I do something similar with compound tasks.
{
"label": "some label",
"type": "npm",
"script": "build",
"path": "some-path/",
"problemMatcher": [],
"runOptions": { "runOn": "folderOpen" },
"group": "build",
"presentation": {
"echo": true,
"reveal": "silent",
"focus": false,
"panel": "shared",
"showReuseMessage": false,
"clear": false,
"group": "build"
}
},
{
"label": "shell",
"type": "shell",
"command": "cd app; bash",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Task and Shell",
"group": "build",
"dependsOn": ["some label", "shell"],
"dependsOrder": "sequence",
}
This configuration runs bash in the right folder after the task (in the same window). Replace bash with whatever shell you use if necessary.
I found a solution for this, my task looks something like this
"tasks": [
{
"label": "start server",
"type": "shell",
"command": "RUN='cd backend && npm run dev' bash",
"problemMatcher": [],
},
]
and at the end of my .bashrc I have eval "$RUN"

VSCode not running task on folder open

I'm using TypeScript and constantly having to start the tsc-watch task manually. According to to the Blog VSCode v1.30+ can automatically run tasks when a folder is opened but this is not working for me (v1.33.1) - I open my folder and no task is running.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"isBackground": true,
"problemMatcher": [
"$tsc"
]
},
{
"type": "typescript",
"label": "TypeScript Compiler Watcher Thingy...",
"tsconfig": "tsconfig.json",
"option": "watch",
"problemMatcher": [
"$tsc-watch"
],
"runOptions": {
"runOn": "folderOpen"
}
}
]
}
BTW: Also tried this extension and it also did not start the task.
This work in VSCode v. 1.42.1 my tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"label": "TypeScript Compiler Watching...",
"option": "watch",
"presentation": {
"echo": true,
"reveal": "silent",
"focus": false,
"panel": "shared"
},
"isBackground": true,
"runOptions": {"runOn": "folderOpen"},
"problemMatcher": [
"$tsc-watch"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
If this still not work on project folder open try Ctrl+shift+P and Tasks: Manage Automatic Tasks in Folder and choose "Allow Automatic Tasks in folder" on main project folder or running folder.

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

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