How to open zsh terminals using vs code tasks in wsl? - visual-studio-code

My aim is to open zsh terminals in the current working directory of wsl whenever I open a workspace. For example, if I open a workspace with three folders, viz. test1, test2, and test3, I want one terminal in each directory to open in vs code.
For that purpose, I created .vscode/tasks.json file in each directory and input the following code:
{
"version": "2.0.0",
"tasks": [
{
"label": "test1",
"type": "shell",
"command": "",
"isBackground": true,
"problemMatcher": [],
"options": {
"shell": {
"executable": "zsh",
"args": []
}
},
"runOptions": {
"runOn": "folderOpen"
},
"presentation": {
"reveal": "always",
"panel": "dedicated"
}
}
]
}
But in the terminal, I get this error:
* Executing task in folder illuminate:
/usr/bin/zsh: can't open input file:
* The terminal process "zsh ''" failed to launch (exit code: 127).
* Terminal will be reused by tasks, press any key to close it.
I checked the docs, but it includes settings only useful for windows, not linux. Is there any way to achieve this?

Related

Open Integrated WSL terminal at specific directory with VSCode Task

I'm working with VSCode Tasks and my goal is to simply open a set of integrated WSL terminals and navigate each to a specified directory when the workspace launches.
I'm using the workspace's tasks object and currently can launch WSL terminals, but can't seem to specify the directory.
Below is my current configuration. It launches my wsl terminals no problem, but with cwd does not correctly map. When using cmd.exe instead of wsl, it works fine.
"tasks": {
"version": "2.0.0",
"tasks": [
//Worker Task to Open Each Terminal.
{
"label": "Create terminals",
"dependsOn": [
"WSL_Terminal_1",
"WSL_Terminal_2",...
],
"group": {
"kind": "build",
"isDefault": true
},
//Run When Workspace Opens.
"runOptions": {
"runOn": "folderOpen"
}
},
//Single Terminal Task.
{
"label": "WSL_Terminal_1",
"type": "shell",
"command": "",
"options": {
"cwd":"/mnt/c/my/working/dir",
"shell": {
"executable": "C:\\...\\ubuntu2004.exe",
}
},
"icon": {"color": "terminal.ansiMagenta", "id": "terminal-linux" },
"isBackground": true,
"problemMatcher": [],
"presentation": {
"echo": false,
"focus": false,
"reveal": "always",
"panel": "new",
}
},
...
I've tried some combination of args with no success
//tested 1
"shell": {
"executable": "C:\\...\\ubuntu2004.exe",
"args": ["-c 'cd /mnt/c/my/working/dir'"],
}
//tested 2
"shell": {
"executable": "C:\\...\\ubuntu2004.exe",
"args": ["-c",
"cd /mnt/c/my/working/dir"
],
}
I tried several versions of cwd as well with no positive results
//Throws Error on running task
"cwd":"/mnt/c/my/working/dir"
//No error but WSL terminal still opens at /home/user
"cwd":"C:\\my\\working\\dir"
//Linux Side Paths also not working
"cwd":"/home/user/test_dir"
"cwd":"test_dir"

Configuring a customized build task in vs code for c++

I had a custom build system in sublime for C++ which takes input from one file and redirects the output to another.
My sublime build system is as follows :
{
"cmd": ["g++.exe","-std=c++14", "${file}", "-o", "${file_base_name}.exe", "&&" , "${file_base_name}.exe<inputf.in>outputf.in"],
"selector":"source.cpp",
"shell":true,
"working_dir":"$file_path"
}
this helped in taking input from "inputf.in" file and print the output in "outputf.in" whenever i ran my program.
I want similar functionality in vs code but dont know how to configure build-task.
Here is a build task for a Node addon, you have to put it in .vscode/tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"type": "shell",
"command": "npx node-pre-gyp build -j max",
"problemMatcher": {
"base": "$gcc",
"fileLocation": [
"relative",
"${workspaceFolder}/build"
]
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
You can press Ctrl-Shift-P to bring the command palette and select configure build task.
The problem matcher allows VSCode to highlight the compiler errors in your editor.
The default build task is the one you launch with a hotkey.

How to simultaneously create a new folder and multiple files in VSCode?

I discovered recently in Visual Studio Code that I can create a new folder and a new file simultaneously by using the following patten: Test/Test.jsx
eg.
1: Right click and select 'New File'.
2: Enter desired folder and file name.
3: The result from step 1 & 2.
Anyone know if it's possible to create a folder with multiple files using a similar pattern? This is what I'd like to be able to do.
I don't think you can do it the way you showed, but it is pretty easy to do it with a task. In your tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "new react folder and files",
"command": "mkdir ${input:dirName} && touch '${input:dirName}/${input:dirName}.component.jsx' '${input:dirName}/${input:dirName}.styles.jsx'",
"type": "shell",
"problemMatcher": [],
"presentation": {
"echo": false,
"reveal": "silent",
"focus": false,
"panel": "shared",
"showReuseMessage": false,
"clear": true
},
}
],
// ........................................................................................
"inputs": [
{
"type": "promptString",
"id": "dirName",
"description": "Complete my folder name",
"default": "jsx folder to create"
}
]
}
And some keybinding to trigger the task (in your keybindings.json):
[
{
"key": "alt+j",
"command": "workbench.action.tasks.runTask",
"args": "new react folder and files",
}
]
This will prompt for the directory name and then create the folder and two files within it.
[I used bash commands mkdir and touch to create the folder and files, if you are using a shell without those commands swap out the ones you have.]

How to translate paths from C:\... to /c/... in vs-code tasks?

I would like to build my program using Git Bash or MinGW so I have this build task:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "gcc.exe build active file",
"command": "gcc",
"args": ["-g", "${relativeFile}", "-o", "${fileBasenameNoExtension}"],
"options": {
"shell": {
"executable": "C:\\Program Files\\Git\\bin\\bash.exe",
"args": ["-l"]
},
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
},
]
}
I have two issues:
The cwd command does not work because ${fileDirname} is in the Windows form C:\Foo\Bar and since I am on bash I would like the POSIX form /c/Foo/Bar.
My task is not executed in the current terminal, but in a weird Window that display no output:
/usr/bin/bash: gcc -g .vscode\tasks.json -o tasks: No such file or directory
The terminal process terminated with exit code: 127
Terminal will be reused by tasks, press any key to close it.
With the extension Command Variable (v0.2.0) you can add Posix versions of the directory variables.
"cwd": "${command:extension.commandvariable.file.fileDirnamePosix}"

VSCode: Open new terminal as part of task?

Visual Studio Code was just updated to allow running a task and having them open in a split terminal. This is great, however I'm looking for one more thing to make this perfect.
I would like to be able to open a total of 3 terminals via a task. One for my NPM build, one for my backend MAVEN build, and a third that is just a blank new terminal I can use for git commands when needed.
I can't seem to find a way to tell VSC to run a task that just opens a new terminal ready to use without providing it a command. I would even settle with giving it a simple command like "node -v" just to start it out, as long as that panel is still usable after. Right now it wants to close it after it has ran.
Here is my task setup: I have one task setup as the build task that depends on two others. I envision adding a third one to that which would just open the new terminal:
{
"version": "2.0.0",
"tasks": [
{
"label": "Run Maven and NPM",
"dependsOn": [ "maven", "npm" ],
"group": {
"kind": "build",
"isDefault": true,
},
},
{
"label": "maven",
"command": "...",
"type": "shell",
"presentation": {
"reveal": "always",
"group": "build"
},
"options": {
"cwd": "${workspaceRoot}/server"
}
},
{
"label": "npm",
"type": "shell",
"command": "ng serve --port 4203 --proxy-config proxy.conf.json",
"presentation": {
"reveal": "always",
"group": "build"
},
"options": {
"cwd": "${workspaceRoot}/client-APS"
}
}
]
}
The following should work:
{
"type": "process",
"label": "terminal",
"command": "/bin/bash", // <-- your shell here
"args": [
"-l" // login shell for bash
],
"problemMatcher": [],
"presentation": {
"echo": false, // silence "Executing task ..."
"focus": true,
"group": "build", // some arbitrary name for the group
"panel": "dedicated"
},
"runOptions": {
"runOn": "folderOpen"
}
}
I was trying to achieve something very similar when I stumbled my way into this solution: Here, I'm auto-launching (and setting the focus on) the terminal when the folder is opened in vscode -- and further tasks that share the same presentation.group gets placed in split terminals when they're run (with new vs. reused splits depending on their presentation.panel)
(The runOptions bit is superfluous for your case, but I'm keeping it in case it is helpful for someone)
Note: For this example, you may or may not need the -l option depending on your settings for terminal.integrated.shell*, terminal.integrated.automationShell* and terminal.integrated.inheritEnv -- this issue has some discussion on what is involved in setting up the shell environment.