Running multiple projects but separate output in Visual Code - visual-studio-code

Visual Code provides the concept of compounds configuration to launch multiple processes. However, all this processes log into one single Debug Console. This is quite ugly. Is there any possibility to have one output windows/tab/something for each process that is running?

If you are using task configuration (tasks.json) you can set the panel attribute inside presentation. It controls if the panel is shared between tasks, dedicated to this task or a new one is created on every run:
{
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "echo Hello",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
}
}
]
}
By default is shared but you can have dedicated or new

Related

VSCode launch configuration to run python file without debugging

I understand that by creating a launch.json file in VSCode (slightly more cumbersome than pycharm), I can set debug configurations to launch individual python files.
For example:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Poker",
"type": "python",
"request": "launch",
"cwd": "C:/Users/dickr/git/poker",
"program": "C:/Users/dickr/git/Poker/poker/main.py",
"console": "integratedTerminal",
"env": {"PYTHONPATH": "${workspaceFolder}${pathSeparator}${env:PYTHONPATH}"}
},
]
}
That all works great, but how can I run it in normal mode, without a debugger? How can this be defined in the launch configuration so I can select this in the dropdown, and potentially pass arguments with it?
Write a task in a tasks.json file. For documentation on tasks, see https://code.visualstudio.com/docs/editor/tasks#_custom-tasks.
Your might look something like this:
{
"version": "2.0.0",
"tasks": [
{
"label": "run python script",
"type": "process",
"command": "python3 C:/Users/dickr/git/Poker/poker/main.py",
"options": {
"cwd": "C:/Users/dickr/git/poker"
},
"presentation": {
"reveal": "always",
}
}
]
}
though I'd suggest that if possible, you use variable substitution to try to get rid of the absolute paths that won't be the same on other machines (assuming you want to be able to run the task on other machines).
Then run the task. You can even bind the task to keyboard shortcuts. See the documentation for more info: https://code.visualstudio.com/docs/editor/tasks.

Terminate another task from within a postDebugTask - VS Code

I have a debug launch configuration (launch.json) like below.
{
"version": "0.2.0",
"configurations": [
{
"name": "Current TS File",
"type": "node",
"request": "launch",
"preLaunchTask": "Pre Debug Task",
"postDebugTask": "Stop Watch Styles",
"args": ["${relativeFile}"],
"runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
"sourceMaps": true,
"cwd": "${workspaceRoot}",
"protocol": "inspector",
}
]
}
My tasks configuration (tasks.json) is like
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "next:copy",
"label": "Copy Files",
"problemMatcher": []
},
{
"type": "npm",
"script": "styles:tsw",
"label": "Watch Styles",
"problemMatcher": [],
},
{
"label": "Pre Debug Task",
"isBackground": true,
"dependsOn" :[
"Copy Files",
"Watch Styles"
]
},
{
"label": "Stop Watch Styles",
// No sure what should come here
}
]
}
Trying to stop watch process in postDebugTask, is there a way to Terminate Task by providing name (Watch Styles) as parameter in tasks.json. Watch styles is a continuously running process, please suggest if there is any other approach to terminate a task after debugging is complete.
This will terminate all tasks after debug stops
Or in this case, just build_runner watch...
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Flutter",
"request": "launch",
"type": "dart",
"flutterMode": "debug",
"preLaunchTask": "flutter: build_runner watch",
"postDebugTask": "Terminate All Tasks"
}
]
}
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Terminate All Tasks",
"command": "echo ${input:terminate}",
"type": "shell",
"problemMatcher": []
}
],
"inputs": [
{
"id": "terminate",
"type": "command",
"command": "workbench.action.tasks.terminate",
"args": "terminateAll"
}
]
}
More info here: https://code.visualstudio.com/docs/editor/variables-reference#_command-variables
This works for me:
{
"label": "postdebugKill",
"type": "process",
"command":[
"${command:workbench.action.tasks.terminate}",
"${command:workbench.action.acceptSelectedQuickOpenItem}",
],
},
The first "${command:workbench.action.tasks.terminate}" will bring up a panel asking you to select which task to terminate. So if you had multiple running tasks and wanted to choose one you would use this command only.
The second "${command:workbench.action.acceptSelectedQuickOpenItem}" will terminate the selected task in that panel mentioned above. (So you will not even see the terminate panel.) If you have only one running task when you call the postdebugKill task, it will be selected automatically and thus terminated. Otherwise whichever task is listed first will be terminated. Again, if you have more than one other task running and you want to select which to terminate don't include this second command.
I don't know of any way to list, perhaps via an args option a label name for which task to terminate if running. It would be nice to have this functionality.
[postdebugKill name can be whatever you want.]
To call a postdebug task your launch.json config might look like this:
{
"type": "node",
"request": "launch",
"name": "Gulp: serve",
"program": "${workspaceFolder}/node_modules/gulp/bin/gulp.js",
"args": [
"serve"
],
// "preLaunchTask": "someTask",
"postDebugTask": "postdebugKill"
},
Upon stopping that "Gulp: serve" debugging session, the task "postdebugKill" will be triggered. And so if I had used the "preLaunchTask" to start a task or had simply started a task running before launching the "Gulp: serve" debugging session - that preLaunchTask would be terminated.
The ability to run vscode commands in a task was recently added to vscode. Some minimal info here: using a command in a task doc.
[I'll add another answer because the new additional info is extensive.]
It seems there is an issue with running a preLaunchTask and then launching a debugging session. See particularly debugging does not work on first try. It appears to be fixed though in an insiders edition and may be released in early February 2019. terminal not sending onLineData.
In the meantime there is a suggested fix within one of the issues that I have lost the link to and that is a problemMatcher which will signal to the debugger that the dependent tasks have completed.
In my watching task I used this:
"problemMatcher": [
{
"base": "$tsc-watch",
"background": {
"activeOnStart": true,
"beginsPattern": "Using gulpfile ~\\OneDrive\\experimental\\gulpfile.js",
"endsPattern": "Starting 'watch'..."
}
}
],
I chose that because when I manually start the gulp:watch task I get this in the terminal:
[22:27:48] Using gulpfile ~\OneDrive\experimental\gulpfile.js
[22:27:48] Starting 'watch'...
[22:27:48] Starting 'watch'...
So you see the start and end pattern there that I copied (with extra escaping).
I suggest that you separately run your "Pre Debug Task" and copy the start and ending output into your "Pre Debug Task" problemMatcher and see if it now works.
My code in the first answer I believe is correct, I just wasn't using "isBackground" and "dependsOn" as you are. But I have added "isBackground" to mine and the problemMatcher option and it works flawlessly now.
Hopefully, this will be fixed in the next February 2019 release and there will be no need for this workaround.
If on Linux or macOS, a less hacky solution that just works is using pkill. If on Windows, see below.
First run the task and find the full command vscode runs for that task, with $ ps -af
Then use pkill + the full command in a postDebugTask.
I have an entry in tasks.json like this:
{
"label": "stop npm:watch",
"type": "shell",
"command": "pkill -f 'sh -c node ./scripts/watch.js'",
"presentation": {
"reveal": "silent",
"panel": "dedicated",
"close": true,
}
}
'sh -c node ./scripts/watch.js' is how vscode runs my npm:watch task.
The presentation properties prevent this task from taking focus or taking up terminal space after it finishes successfully.
Then you reference that task in a launch.json configuration:
{
...
"preLaunchTask": "npm:watch",
"postDebugTask": "stop npm:watch",
...
}
If the full command contains changing parameters (paths, port numbers, etc.), you can use a part of the command or regex.
e.g.: I could have probably matched with './scripts/watch.js' or 'node.*watch'.
For Windows: you can make this work by substituting pkill for taskkill.
If you want to support both Unix and Windows, you can make a script that does one or the other depending on the underlying OS.

How do I automatically clear VS Code terminal when starting a build?

I press Ctrl+Shift+B to start a build in Visual Studio Code (it's configured to just run GNU Make), and the build tool output is written to the Terminal window.
However, it's appended to the output from the previous build, which is confusing.
How do I configure VS Code to clear the terminal window before starting a new build?
November 2018 Update
As of this commit (and a few subsequent follow-ups), you can now add a clear presentation option to your task to have it clear the terminal before each task run.
Working example (on fresh clone+build):
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "[gcc] Build",
"type": "shell",
"command": "g++",
"args": [
"source.h",
"-Wall",
"-o",
"a.out"
],
"presentation": {
"clear": true // <-- this line
}
}
]
}
(Note: the linked commit diff has the key being named clearBeforeExecuting but it's apparently since been changed to just clear).
Prior to this, I created a clear_g++ script on my path with just:
#!/bin/bash
clear
exec g++ $*
And changed my command from g++ to clear_g++.
Since I liked the idea of this approach but it didn't end up working out.
You can change from settings menu (at least from version 1.30.2 and above)...
On Mac, just hit Code > Preferences > Settings.
Then just search for "clear" and check Clear Previous Output.
I tried to find a solution but can't. Simple hack I tried is to open new build in new tab. Add this presentation key to your task in tasks.json
"presentation": {
"echo": true,
"reveal": "never",
"focus": false,
"panel": "new"
}
panel:new will open in new terminal.
Add this user setting to clear the OUTPUT tab on clicking run (▶)
"code-runner.clearPreviousOutput": true,
This is not the same as clearing the terminal but it might be what someone wants.
[Edit] This requires the Runner extension, which I'd recommend for testing/running scripts directly within VS Code.
New Visual Studio code 1.56. This works in windows.
You simply go to Preferences:Open Settings(UI), search "Clear" and check option as below:
This will make sure that terminal remain clear on every run, thus ensuring only 1 file run is visible at a time.
Update Visual Code 1.54 +
To clean terminal when click Run.
Install Code-runner extension.
Setting > search "clear" -> Check on "Clear Previous Output"
If you control the build task yourself, it's easy to prepend a clear command:
"tasks": [
{
"label": "build",
"type": "shell",
"command": "clear && make",
....
In Visual Studio Code version 1.52.1, the clearing by default of the terminal is achieved with the clear: true property (=Controls whether the terminal is cleared before executing the task.). Unfortunately it does not do the job, I still see the terminal with older messages. I have to manually enter "clear" in the terminal to clear it completely.
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": true
}
This is added in tasks.json which looks like this under OSX:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++11",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/clang++",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": true
}
}
]
}

Can't get simple task to run in vscode

I am ultimately trying to set up vscode to build typescript, but I first just wanted to get a simple task to run, and I can't seem to get that to work. I now want to just run the "Hello world" task from https://code.visualstudio.com/docs/editor/tasks, which is to simply echo a string to the output window.
My tasks.json is in the .vscode folder, and its content is:
{
"version": "0.1.0",
"command": "echo",
"isShellCommand": true,
"args": ["Hello World"],
"showOutput": "always"
}
When I try to run the task from the command palette and choosing "Tasks: Run Task," I see "no tasks found" when I expect to see this echo task. I don't know why I don't see this task in a task list.
What am I doing wrong?
FWIW, my vscode version is 1.11.1.
This works on current vscode:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "MyHelloTask",
"type": "shell",
"command": "echo",
"args": ["Hello\ World"],
"presentation": {
"echo": true,
"reveal": "always",
"panel": "shared"
}
}
]
}
What was wrong?
The property showOutput is deprecated. Use the reveal
property inside the presentation property instead.
See also the 1.14 release notes.
Also isShellCommand now became type, etc...
Also note the escaped space in the argument. (triggers explict complaint about it otherwise. Yes, despite the quotes around it.)

How to run several tasks in VSCode

I am trying to migrate to VSCode and having a problem with setting-up tasks. It is easy to define tasks in tasks.json but I would like to run several tasks simultaneously (which I can't).
Here is my use-case: I have two watchers in my project (one for gulp and another one for webpack). Plus I want to be able to run webpack task separately. When I run one of the watchers I cannot run anything else - VSCode requires me to terminate the running task at first.
In Visual Studio I used Task Runner where several tasks were running simultaneously. Is it possible to achieve the same in VSCode?
Using compound tasks, you can specify dependsOn and dependsOrder on a separate task, and run them in parallel like this:
{
"label": "start-tasks",
"dependsOrder": "parallel",
"dependsOn": [
"taskOne",
"taskTwo"
]
}
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"
}
]
}