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"
Related
Is there a way to open terminal by default in VSCode? I saw the questions here four years ago - VS Code - How to have Terminal pane open by default? not sure if there is any update.
You can use the tasks.json file to "auto-run" terminals with Tasks. See documentation at https://code.visualstudio.com/docs/editor/integrated-terminal#_automating-launching-of-terminals
tasks.json template:
{
"version": "2.0.0",
"presentation": {
"echo": false,
"reveal": "always",
"focus": false,
"panel": "dedicated",
"showReuseMessage": true
},
"tasks": [
{
"label": "Open terminal by default",
"dependsOn": [
"Terminal"
],
"runOptions": {
"runOn": "folderOpen"
}
},
{
// The name that shows up in terminal tab
"label": "PowerShell",
// The task will launch a shell
"type": "shell",
"command": "",
// Set the shell type
"options": {
"shell": {
"executable": "powershell.exe",
"args": []
}
},
// Mark as a background task to avoid the spinner animation on the terminal tab
"isBackground": true,
"problemMatcher": []
}
]
}
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" }
}
]
}
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"
I have a problem when I try to run a make task that runs in a windows command shell. I've tried various combinations but I can't get the command to fully execute.
In tasks.json below you can see I've tried various methods (this format gives me the most info) but in all attempts the make file command fails "'make' is not recognized as an internal or external command, operable program or batch file." yet its the script setCmdEnv in the first command which sets up the make path etc.
The first two commands work fine, I can't see why the last command doesn't execute correctly.
Weirdly I can type make after the shell is loaded (after the error) and all is ok.
{
"version": "2.0.0",
"type":"shell",
"windows": {
"options": {
"shell": {
// "executable": "C:\\WINDOWS\\System32\\cmd.exe",
"executable": "",
// "args": [
// "/K .\\BuildEnv\\xBuildEnv\\setCmdEnv && cd .\\app && "
// ]
}
}
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"clear": false
},
"tasks": [
{
"label": "Make %1.test",
"command": "cmd.exe",
"args": [
" /K .\\BuildEnv\\xBuildEnv\\setCmdEnv && cd .\\app && make ${fileBasenameNoExtension}.test"
],
"problemMatcher": []
}
],
}
I had a similar issue and to make the Visual Studio Code task to execute multiple commands, I had to separate each elements of the args array to leave out any white space. So in this case, the task could be something like this:
{
"version": "2.0.0",
"tasks": [
{
"label": "Make your test file",
"type": "shell",
"windows": {
"command": "%comspec%",
"args": ["/C", "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\Tools\\VsDevCmd.bat",
"&&", "cd", ".\\apps", "&&", "make", "${fileBasenameNoExtension}.test"
]
},
"options": {
"cwd": "${workspaceRoot}"
},
"presentation": {
"showReuseMessage": false,
"reveal": "always",
"panel": "shared",
"group": "test",
"clear": true,
"focus": true
}
}
]
}
Note: The %comspec% variable resolves to C:\WINDOWS\system32\cmd.exe.
In such a way that it is not in a sub shell. I need it to be able to prepare the environment...set environment variable.
"version": "0.1.0",
"command": "${workspaceFolder}/Invoke-Task.ps1",
/*"command": "powershell", creates subshell so doesn't work*/
"isShellCommand": false,
"args": [],
"showOutput": "always",
"echoCommand": true,
"suppressTaskName": true,
"tasks": [
{
"taskName": "task1",
"args": ["task1"]
},
{
"taskName": "task2",
"args": ["task2"]
}
]
I am sorry, but you are not correctly editing the .vscode/tasks.json file.
In your scenario, lets say you have some powershell script ./scripts/mycustomPSscript.ps1 you want to run as a vscode task. For such goal, edit the tasks file so it follows the below example:
{
"version": "2.0.0",
"tasks": [
{
"label": "Run-some-custom-script",
"detail": "Prepare some ENV variables for the deployment",
"type": "shell",
"command": "./../scripts/mycustomPSscript.ps1",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
}
}
]
}
This has been doable since 2017, if I get your ask correctly.
integrated-terminal-tasks README This extension allows a workspace to
define specific tasks that should be ran in VSCode's interactive
terminal
https://marketplace.visualstudio.com/items?itemName=ntc.integrated-terminal-tasks
Also, your post / query, could been seen as a duplicate of this...
Run Code on integrated terminal Visual Studio Code
Adding this configuration in the launch.json file did the trick for me
"version": "0.2.0",
"configurations": [
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Launch Current File",
"script": "put full path here\\launch.ps1",
"args": ["${file}"],
"cwd": "${file}"
},...
Not sure what you mean by 'integrated terminal' but the output does show up in the VSC terminal if this is what you're referring to.