Devcontainer: how to open terminal with custom commands when opening the container? - visual-studio-code

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

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

Visual Studio Code and starting tasks from /.vscode/task,json: How to force them staring from CMD and not Powershell?

I have the below tasks.json and it works fine, except that they are run from Powershell. And the browser-sync task gives an error: "browser-sync : File C:\Users\marti\AppData\Roaming\npm\browser-sync.ps1 cannot be loaded because running scripts is disabled on this system."
It works fine if tasks are run from Cmd terminal instead. How can I make them start from there? I have set Cmd as default terminal in VS.
{
"version": "2.0.0",
"tasks": [
{
"label": "Start watching scss and js",
"type": "cmd",
"command": "npm start",
"presentation": {
"reveal": "always",
"panel": "new"
},
"runOptions": { "runOn": "folderOpen" }
},
{
"label": "Start browser-sync",
"type": "shell",
"command": "browser-sync start --proxy \"localhost:5829\" --files \"css/*.css\"",
"presentation": {
"reveal": "always",
"panel": "new"
},
"runOptions": { "runOn": "folderOpen" }
}
]
}

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"

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",
},
]
}

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