Is it possible to open a new terminal inside VSCode from inside a script? - visual-studio-code

I want to start 3 servers from a single command.
I have package.json scripts like so:
"serve_auth": "cd dev/mock/auth && nodemon --exec babel-node ./server.js --presets #babel/env",
"serve_db": "cd dev/mock/db && nodemon --exec babel-node ./server.js --presets #babel/env",
"start": "react-scripts start",
"develop": "./launch_script.sh"
and I have a script launch_script.sh like so:
#!/bin/bash
( yarn serve_db ) & ( yarn serve_auth ) & ( yarn start )
but this opens them all in a single terminal window, and they end up tripping all over each other.
I know you can open new terminals from the VSCode GUI, but is it possible to open a new terminal from within one? Or to tell VSCode to open 3 terminals each with a separate command ?

I think this could be something for compound tasks
{
"version": "2.0.0",
"tasks": [
{
"label": "Client Build",
"command": "gulp",
"args": ["build"],
"options": {
"cwd": "${workspaceRoot}/client"
}
},
{
"label": "Server Build",
"command": "gulp",
"args": ["build"],
"options": {
"cwd": "${workspaceRoot}/server"
}
},
{
"label": "Build",
"dependsOn": ["Client Build", "Server Build"]
}
]
}
Compound tasks
You can also compose tasks out of simpler tasks with
the dependsOn property. For example, if you have a workspace with a
client and server folder and both contain a build script, you can
create a task that starts both build scripts in separate terminals. If
you list more than one task in the dependsOn property, they are
executed in parallel by default.
Also compound launch configurations may be interesting to you as it seems like your scripts are for starting a frontend and backend app.
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Server",
"program": "${workspaceFolder}/server.js",
"cwd": "${workspaceFolder}"
},
{
"type": "node",
"request": "launch",
"name": "Client",
"program": "${workspaceFolder}/client.js",
"cwd": "${workspaceFolder}"
}
],
"compounds": [
{
"name": "Server/Client",
"configurations": ["Server", "Client"]
}
]
}
Both are examples from the corresponding docs page but adjusting them to your scripts should be straightforward.

Related

How to attach debugger to yarn start command

I have the following command yarn start-package with package being the project for package
I have configured the following launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Package Debuger",
"preLaunchTask": "yarn start-package"
}
]
}
I have configured the following tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "yarn start-package",
"type": "npm",
"script": "start",
"path": "packages/package",
"problemMatcher": []
}
]
}
Now whenever i start debugger, it will never attach because the prelaunch task is starting the developing server and never finishes. How can i attach the debugger to the yarn start-package command correctly?
Note: I am using wsl for running the yarn command if it matters

Stuck waiting for prelaunch task

I'm trying to run my debug environment as a Poetry session, so I can properly debug encapsulated.
So, my launch.json is straightforward:
{
"name": "Poetry",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5710
},
"preLaunchTask": "poetryDebugSession",
"localRoot": "${workspaceFolder}"
}
and I adapted my tasks.json to try to launch debugpy in Poetry first (this works if I manually run some code in the terminal)
{
"label": "poetryDebugSession",
"type": "shell",
"command": "poetry",
"args": [
"run",
"python",
"-m",
"debugpy",
"--log-to-stderr",
"--wait-for-client",
"--listen",
"5710",
"${relativeFile}",
"&"
],
"presentation": {
"panel": "dedicated",
"clear": true
},
"group": "test",
"isBackground": true,
"runOptions":{
"instanceLimit": 1
},
// This task is run before the launch.json task. Since it needs to run in the
// background and not wait for completion, though, we need to jump through hoops
"problemMatcher": [
{
"owner": "python",
"fileLocation": "absolute",
"pattern": [
{
"regexp": "^\\s+File \"(.*)\", line (\\d+), in (.*)$",
"file": 1,
"line": 2
},
{
"regexp": "^\\s+(.*)$",
"message": 1
}
],
"background": {
"activeOnStart": true,
"beginsPattern": "^D[0-9\\.: \\+]+wait_for_client",
"endsPattern": ".*",
}
}
]
}
When I start debugging, the task is properly launched, and debugpy gets all the way to the message I am waiting for that I want the preluanch task to be marked as "ready":
> Executing task: poetry run python -m debugpy --log-to-stderr --wait-for-client --listen 5710 d:\path\to\myfile.py <
# stuff
I+00000.344: pydevd is connected to adapter at 127.0.0.1:61443
D+00000.344: wait_for_client()
I could have sworn I had this working last week but as of 1.58.2 after a restart this morning it doesn't progress past wait_for_client() display, so the debugger never attaches. I'm also a little suspicious that ${relativeFile} includes a full path in my output but that probably doesn't matter.
It should be clear from the code above that I derived my initial implementation from https://stackoverflow.com/a/54017304/1877527 , but still no dice.
The debugpy team helped me find an answer:
https://github.com/microsoft/debugpy/issues/676#issuecomment-886041838
{
"name": "Python: Poetry current file",
"type": "python",
"request": "launch",
"program": "${env:USERPROFILE}/.poetry/bin/poetry",
"python": "<path/to/bare/bones/python>",
"args": ["run", "python", "${file}"],
"console": "integratedTerminal",
}
It worked for me even without the python argument, since my primary dev machine just has one active Python installation.

How to launch specific task from input variable in VS Code?

I'm trying to create a launch configuration where an environment variable is dynamically determined by a shell script. Even though a command variable can start a task via workbench.action.tasks.runTask, it doesn't seem to be possible to specify which task to run. Input variables seem to be a little more flexible in that regard, but I can't seem to get it to work. Here is what I got:
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"env": {
"XXX": "${input:foo}"
},
"args": []
}
],
"inputs": [
{
"type": "command",
"id": "foo",
"command": "workbench.action.tasks.runTask",
"args": {
"args": "bar",
}
}
]
}
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "bar",
"type": "shell",
"command": "find /dev -name 'myspecialdevice*' -maxdepth 1"
}
]
}
The issue is that the user is still queried for which task to run. I'm most insecure about the inputs.args section of the launch.json. I don't really know what the key value should be. Perhaps the implementation helps to figure this out?
This answer not really relates to make use of a vscode task, but your introducting sentence offers the motivation/what is intended to be solved.
I was faced to the same question and was wondering about vscode's input type:command. It offers a way to embed a (custom) vscode command -- which looks like a powerfull mechanism to embed a (custom) extension here. But I didn't found a builtin command that simply executes a shell script and returns it stdout. Thus an extension to capture the output of a shell command is imho required todo the trick.
E.g. https://marketplace.visualstudio.com/items?itemName=augustocdias.tasks-shell-input
provides a command shellCommand.execute doing exactly this.
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"env": {
"XXX": "${input:foo}"
},
"args": []
}
],
"inputs": [
{
"id": "foo",
"type": "command",
"command": "shellCommand.execute",
"args": {
"command": "find /dev -name 'myspecialdevice*' -maxdepth 1",
"cwd": "${workspaceFolder}",
/* To prevent user from selecting output (if there is just
one line printed by command), un-comment next line */
//"useSingleResult": true
}
}
]
}
(Inspired by https://stackoverflow.com/a/58930746/1903441)
In your launch.json, try replacing
"args": {
"args": "bar",
}
with
"args": "bar"
It seems that in vscode, you cannot pass a return or even an environment variable from task.json to launch.json. But you can use a file as a intermediate.
For example, in task.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "bar",
"type": "shell",
"command": "find /dev -name 'myspecialdevice*' -maxdepth 1 > ${workspaceFolder}/.vscode/temp"
}
]
}
In launch.json you set bar as preLaunchTask, and later access the file using inputs, like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"env": {
"XXX": "${input:foo}"
},
"args": [],
"preLaunchTask": "bar",
}
],
"inputs": [
{
"id": "foo",
"type": "command",
"command": "extension.commandvariable.file.content",
"args": {
"fileName": "${workspaceFolder}/.vscode/temp",
}
}
]
}
To get the comment working, just install this extension: https://marketplace.visualstudio.com/items?itemName=rioj7.command-variable

How to run multiple tasks in VS Code on build?

Using tasks.json version 2.0.0, I have not been able to make it so that, when I build my application, multiple tasks are run at the same time. I'm using gulp for my SCSS compilation, and running my Compile/minify cms.scss task on its own works fine, so it's not a problem with the task itself, just VS Code's task runner. When I Run Build Task in VS Code, my gulp task is not being run, even though it has "group": "build" — only the dotnet one is.
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/HpsCoreWeb.csproj"
],
"problemMatcher": "$msCompile",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Compile/minify cms.scss",
"type": "gulp",
"task": "cms.scss:cms.min.css",
"problemMatcher": "$node-sass",
"group": "build"
}
]
}
According to the VS Code Tasks documentation:
group: Defines to which group the task belongs. In the example, it belongs to the test group. Tasks that belong to the test group can be executed by running Run Test Task from the Command Palette.
The dotnet build task is succeeding, so shouldn't the other task, which is also part of the build group, be run as well? What am I doing wrong?
The problem is that "Run Test Task" and "Run Build Task" do not execute all tasks in that specific group. Usually you get a drop down selection so you can choose which task to execute. Since you have specified one of the tasks as default, the selection will be skipped and instead the default task is executed.
You can work around that by adding dependencies. Take the following example:
{
"version": "2.0.0",
"tasks": [
{
"label": "Echo 1",
"command": "echo",
"type": "shell",
"args": [ "echo1" ],
"group": {
"kind": "build",
"isDefault": true
},
"dependsOn":["Echo 2"]
},
{
"label": "Echo 2",
"type": "shell",
"command": "echo",
"args": [ "echo2" ],
"group": "build"
}
]
}
As Echo 1 depends on Echo 2, Echo 2 will be executed prior to executing Echo 1. Note that the definition is a list, so more than one task can be specified. In that case the tasks are executed in parallel.
In your case adding "dependsOn":["Compile/minify cms.scss"] to your main build task should execute both tasks.
You can use Compound Tasks.
The example below executes "Client Build" and "Server Build" tasks when "Build" task is called.
{
"version": "2.0.0",
"tasks": [
{
"label": "Client Build",
"command": "gulp",
"args": ["build"],
"options": {
"cwd": "${workspaceFolder}/client"
}
},
{
"label": "Server Build",
"command": "gulp",
"args": ["build"],
"options": {
"cwd": "${workspaceFolder}/server"
}
},
{
"label": "Build",
"dependsOn": ["Client Build", "Server Build"]
}
]
}
Add 'gulp-load-plugins' plugin in package.json
var gulpLoadPlugins = require('gulp-load-plugins'),
plugins = gulpLoadPlugins();
To read more about that plugin, see How To Build And Develop Websites With Gulp
Double check your settings are enabled for Gulp auto-detection:
"gulp.autoDetect": "on"

How to run a command in Visual Studio Code with launch.json

Is there a way to execute an ssh command when debugging a project with .vscode/launch.json?
For example: ssh -i xxxxx.
Or is it possible to create a command that you can run from the F1 command palette pop-up? Something like RunCustomCommandxx.
You can define a task in your tasks.json file and specify that as the preLaunchTask in your launch.json and the task will be executed before debugging begins.
Example:
In tasks.json:
For version 0.1.0:
{
"version": "0.1.0",
"tasks": [{
"taskName": "echotest",
"command": "echo", // Could be any other shell command
"args": ["test"],
"isShellCommand": true
}]
}
For version 2.0.0 (newer and recommended):
{
"version": "2.0.0",
"tasks": [{
"label": "echotest",
"command": "echo", // Could be any other shell command
"args": ["test"],
"type": "shell"
}]
}
In launch.json:
{
"configurations": [
{
// ...
"preLaunchTask": "echotest", // The name of the task defined above
// ...
}
]
}
Tasks documentation: https://code.visualstudio.com/docs/editor/tasks
Launch configuration: https://code.visualstudio.com/docs/editor/debugging#_launch-configurations
The format changed. Visual Studio Code can create the tasks.json file for you. Inside your launch.json file and inside any configurations object, just define preLaunchTask to force auto-creation of the tasks.json template file:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "launch program",
"skipFiles": [ "<node_internals>/**"],
"preLaunchTask": "myShellCommand",
"program": "${workspaceFolder}/test.js"
}
]
}
If you do not have file tasks.json configured:
launch the debugger. Visual Studio Code will inform you: "could not find the task
myShellCommand"
select Configure Task → Create tasks.json file
from template → Others
Your tasks.json template file will then be created for you. Add your command to command and the name you put in preLaunchTask to label:
{
"version": "2.0.0",
"tasks": [
{
"label": "myShellCommand",
"type": "shell",
"command": "echo goodfood"
}
]
}
For me, I just needed an environment variable, which is different. You don't want a task for this because (at least for me) it doesn't work when the launcher is run.
Thanks to here, I got it working like this, inside my launcher (launch.json) entry:
"environment": [{
"name": "ENVIRONMENT_VARIABLE_NAME",
"value": "${workspaceFolder}/lib" //Set to whatever value you want.
}],
My version of the configuration allows to just run a defined task and carried on (in my case, the task is to run the currently open Groovy file):
"configurations": [
{
"name": "Launch groovy",
"request": "launch",
"type": "coreclr",
"preLaunchTask": "groovy",
"program": "cmd.exe",
"args": ["/c"],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"internalConsoleOptions": "neverOpen"
}
]
And the task:
"tasks": [
{
"label": "groovy",
"type": "shell",
"command": "groovy ${file}"
}
]
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch app",
"program": "lib/main.dart",
"request": "launch",
"type": "dart"
},
{
"name": "Build an APK Release",
"command": "flutter build apk --release",
"request": "launch",
"type": "node-terminal"
},
{
"name": "Install an APK on a device",
"command": "flutter install",
"request": "launch",
"type": "node-terminal"
}
]
}
for flutter developers who is searching how to run flutter build commands.
in tasks.json add
"tasks": [
{
"type": "flutter",
"command": "flutter",
"args": [
"pub",
"run",
"build_runner",
"build",
"--delete-conflicting-outputs"
],
"problemMatcher": [
"$dart-build_runner"
],
"group": "build",
"label": "flutter: flutter pub run build_runner build --delete-conflicting-outputs"
},
]
then from vscode you can try "run task" it will show you flutter: flutter pub run build_runner build --delete-conflicting-outputs
this way you don't need to memorize and type to terminal source code generation/build commands