Run same instance of terminal with build task in VS Code - visual-studio-code

I have a build.bat file for which I would like VS Code to run when I try and build my project. To do this in VS Code you setup a task.json file and I've done so:
{
// 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",
"windows": {
"command": "${workspaceRoot}\\build.bat"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
The problem I'm having is each time I run the task it seems to generate a new terminal instance. Is there a way to have VS Code keep re-using the same terminal instance for its build tasks?

You can use the :
terminal.integrated.automationShell
Setting to set the shell that will be used for all automation in VS Code, which includes Tasks

Related

Run bash script with VS Code

I want to run bash script when I hit F5 and see the results in the terminal like I can do with python scripts or whatever. I tried to do that with Bash Debug however it automatically goes to the debug mode and stops at the first step even if I do not put breakpoint. This is the launch configuration I use.
{
"type": "bashdb",
"request": "launch",
"name": "Run mysql test",
"cwd": "${workspaceFolder}",
"program": "/srv/gpf/dba/mysqlslap/run.sh",
"args": []
}
I don't know about running bash in a debug mode (doesn't seem like you need those features based on your example) but you can easily get your script to run as a Task or Build option.
Place this at .vscode/tasks.json of your project dir
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Run as Bash Script",
"type": "shell",
"command": "/bin/bash ${file}",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
You can place whatever you want in the "command" parameter.
As you can see, it's of type "kind": "build" so I can hit ctrl+shift+B and this will execute in a terminal.
Note, you can also use the command palette (F1 or ctrl+shift+P) and use Task: Run as Task too.
Source: https://code.visualstudio.com/Docs/editor/tasks

VS Code task to toggle the status bar on debug execution

I prefer to hide the status bar in VS code but one nice feature is that it changes colour when your program is ready to debug. This is good when there is a lengthy build step as part of the preLaunchTask. I would like to unhide and hide the taskbar using tasks.json. The internal command is
workbench.statusBar.visible true
But I am not sure how I can execute this in a task by vscode
{
"label": "debug with statusbar",
"type": "process", // ?
"group": "build",
"args": [ true ],
"command": "workbench.statusBar.visible",
"dependsOn":["npm: build"]
},
{
...
It appears a custom task can either execute via shell or as a process. Is there a way to call vscode via one of these methods and execute the internal command? Or is there another way to achieve what I would like?
Editing the settings file seems to do the trick since VS Code watches it in real time.
{
"label": "debug with statusbar",
"type": "shell",
"group": "build",
"args": [
"-i",
"'/\"workbench.statusBar.visible\": false/s/.*/ \"workbench.statusBar.visible\": true/'",
"/home/myuser/.config/Code/User/settings.json"
],
"command": "sed",
"dependsOn":["npm: build"]
},
Then an equivalent task can be used for postDebugTask

VScode remote development: How can I run a build task to source environment variables before running the real build task?

I want to setup VScode so I can use build tasks to build my project. The project is build using make, en I have defined a build task that runs make. However, before running make, I normally would source a script that sets my environment variables correctly. If I add a new build task with the source command, and set my main build tasks to first execute the source command, the environment variables are not propagated properly. How can I make sure the enviroment variables are kept between build tasks?
My tasks.json file:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "make",
"command": "make",
"args": [],
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"dependsOn": "Set environment"
},
{
"label": "Set environment",
"type": "shell",
"command": "source path/to/source_me.sh",
"group": "build",
]
}
Not in this way. Sourcing a file is injecting file contents into current shell session, which ends as soon as the task ends. The make task is run in a separate shell session, so these two do not interact. You may try to do a single task that executes a single line: source path/to/source_me.sh && make.
I solved my problem like this:
"tasks": [
{
"label": "all",
"type": "shell",
**"command": "source gitenv.sh && cd ${fileDirname} && alfred all"**,
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
Where alfred is a macro for make command with extra args.
${fileDirname} returns a current directory in which you have open file.
So if you open a file and in the same directory you have Makefile you can CTRL + SHIFT + B to execute this default task.

Visual Studio Code Task is Prepending the working directory to the command

I'm trying to setup visual studio code for rust to do debugging. In my launch.json file I have
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceRoot}/target/debug/vscode-rust-debug-example.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"preLaunchTask": "localcargobuildtask"
}
]
}
and in my tasks.json I have
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "localcargobuildtask",
"command": "echo hi",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Where "echo hi" is in my tasks I eventually want to have something like "cargo build" or "cargo test" or whatever. But this is just a starting step.
But when I press F5 the output I get is
> Executing task: C:\programs\VSCodeWorkspaces\RustProjects\vscode-rust-debug-example-debug-config-included\echo hi <
The terminal process terminated with exit code: 2
Terminal will be reused by tasks, press any key to close it.
Rather than running "echo" from where ever the terminal finds it (working directory first, then check global path variable like you would expect) it is actually looking for a program named "echo" in my root folder of the workspace.
How do I tell visual studio code "No I don't want you to run workspace/echo, I want you to run echo in workspace" ? Or is there another more direct way to tell visual studio code "compile this before you debug it" ?
The answer to this question How to debug Rust unit tests on Windows?
suggests that changing
"command": "cargo build",
into
"command": "cargo",
"args": [
"build"
],
in the tasks.json file works and somehow it does. Maybe the editor is configured to search the %path% for single word commands, or maybe some plugin I installed overwrote the "cargo" command. Maybe the reason echo wasn't found is because it is a terminal command, not a program. Maybe the error message was a lie and it was only reporting that it couldn't find workspacefolder\command despite checking %path%? Or maybe none of that. I have no idea. It is some pretty idiosyncratic behavior.

Visual Studio Code 1.17.1 Open in Browser without opening terminal

Looks like an old question, but no proper answers found.
I've looked at here and here.
Right now, I can open Chrome by doing this:
task.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "echo",
"type": "shell",
"command": "echo Hello"
},
{
"taskName": "Open in Chrome",
// "type": "process",
"windows": {
"command": "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
},
"args": ["${file}"],
}
]
}
keybindings.json:
[{
"key": "ctrl+alt+g",
"command": "workbench.action.tasks.runTask",
"args": "Open in Chrome"
},]
Note that I don't even need type: process to make it run and can only run it using my own key binding. If I use ctrl+shift+B (Windows), it'll allow one task only.
However, every time I run the task, the terminal is also opened with: Terminal will be reused by tasks, press any key to close it. which is repetitive and not really helpful for front-end work.
Is there a way to turn that off?
I've tried adding:
"presentation": {
"reveal": "never" //same with "silent"
}
to the task in task.json but it doesn't work.
Answer to close question:
The easiest way is to use an extension like this one: https://marketplace.visualstudio.com/items?itemName=techer.open-in-browser