VScode Code custom task: "terminal process terminated with exit code: 1." - visual-studio-code

I have configured my first custom task in Visual Studio code as follows to run bun.js (a javascript runtime alternative to Node.js or Deno):
{
"version": "2.0.0",
"tasks": [
{
"label": "my task",
"type": "shell",
"command": "bun",
"args": [
"run",
"dev"
],
"presentation": {
"reveal": "always",
"focus": true,
"panel": "new",
},
}
]
}
When I run the task I get the following error message:
The terminal process "C:\Windows\System32\wsl.exe -e bun run main.ts" terminated with exit code: 1.
I am running Ubuntu Linux on a windows machine and I have verified that the wsl executable is in the above path.
What is causing this process to fail?

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"

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

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?

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.

What are the correct beginsPattern and endsPattern for a background Task in VSCode?

I have a static website (i.e. just html and client side JavaScript) that I serve with python while debugging locally. I have a VSCode task that will start python correctly and am trying to set that task as the preLaunchTask on a Debugger for Chrome launch task. The desired behavior is that whenever I start debugging the serve task below ensures the site is being served.
If I understand background tasks correctly one can set a beginsPattern and endsPattern to signal state changes.
I am expecting that when python echos
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
to stdout that the problemMatcher below would signal to the launch task that it had started. Instead, the launch task waits forever, and doesn't proceed until the task's shell command is terminated.
Can tasks be configured to achieve this sort of behavior?
Launch Configuration
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8000",
"webRoot": "${workspaceFolder}/webroot",
"preLaunchTask": "serve"
}
]
}
Serve Task
{
"version": "2.0.0",
"tasks": [
{
"label": "serve",
"type": "shell",
"command": "python3 -m http.server",
"windows": {
"command": "py -m http.server"
},
"isBackground": true,
"options": {
"cwd": "${workspaceFolder}/webroot"
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "dedicated"
},
"problemMatcher": {
"owner": "custom",
"pattern":[
{
"regexp": "^([^\\s].*)$",
"file": 1,
"location": 2,
"message": 3
}
],
"background": {
"activeOnStart": true,
"beginsPattern":"^Serving HTTP (.*)$",
"endsPattern":"^Keyboard interrupt received, exiting(.*)$"
}
}
}
]
}
So we also had a similar problem: wanted to set up a Debugger on a Django app running inside Docker. On my setup, the debugger launched a preLaunchTask which starts the remote interpreter debugger, among other things (like installing ptvsd.
Original Steps:
preLaunchTask calls a script (./run-debug.sh).
This script calls remote debugger with this command:
docker container exec -it my_app python debug.py runserver --noreload --nothreading 0.0.0.0:8000
On the debug.py file, there's a print statement to know that the debugger started.
That didn't work, apparently, VSCode doesn't catch the output of the debugger. Instead, on the run-debug.sh file I added an echo statement: Starting debugger session: which VSCode caught ^_^. That fixed the issue for me.
tasks.json, relevant problem matcher:
"problemMatcher": {
"pattern": [
{
"regexp": ".",
"file": 1,
"location": 2,
"message": 3
}
],
"background": {
"beginsPattern": "^Starting debugger session:",
"endsPattern": ".",
}
}
run-debug.sh script, relevant part:
# Start remote process
echo 'Starting debugger session:' #VSCode beginsPattern will catch this!
docker container exec -it my_app python debug.py runserver --noreload --nothreading 0.0.0.0:8000

Running background services in Visual Studio Code

I would like to run a background service like MongoDB from Visual Studio Code. I tried to run it through the task runner like this:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "mongod",
"isShellCommand": false,
"args": ["--dbpath", "data\\db"],
"showOutput": "always"
}
But this will only run it inside VS with no control to stop the server, e.g. by pressing Ctrl+C.
The normal way would be to run a cmd.exe and run the mongod command from there. But I would love to nicely integrate it into VS.
Found a solution to the problem by using one command with subtasks.
This will run "mongod" in a separate cmd.exe shell.
Once started (in this case minimized via /MIN), I can stop the MongoDB by opening the cmd windows and pressing ctrl+C to properly shutdown the database.
It would still be nicer to have the shell running inside vscode, but maybe this will come in an update someday.
{
"version": "0.1.0",
"command": "start",
"isShellCommand": true,
"showOutput": "never",
"args": [
"/MIN"
],
"tasks": [
{
"taskName": "Start MongoDB",
"args": [
"\"MongoDB # localhost:27017\"",
"mongod",
"--dbpath",
"${workspaceRoot}/data/db"
],
"suppressTaskName": true
}
]
}
You can define a watching task like in this webpack example:
{
"version": "0.1.0",
"command": "npm",
"isShellCommand": true,
"echoCommand": false,
"suppressTaskName": true,
"showOutput": "always",
"tasks": [
{
"args": [
"run",
"start",
"--silent"
],
"problemMatcher": [
{
"owner": "custom",
"pattern": [],
"watching": {
"activeOnStart": true,
"beginsPattern": "webpack: bundle is now INVALID",
"endsPattern": "webpack: bundle is now VALID"
}
}
],
"isWatching": true,
"taskName": "development"
}
]
}
The (optional) problem Matcher watching beginsPattern and endsPattern define the console output of the watcher task start and end. The watching task can be terminated via command palette.
https://github.com/Microsoft/vscode/issues/6209#issuecomment-218154235