VSCode Run Launch configuration via Tasks.json - visual-studio-code

I currently have a tasks.json that runs 3 tasks together, grouped in a panel on folder startup:
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Run dev",
"type": "shell",
"command": "npm run dev",
"presentation": {
"reveal": "always",
"panel": "new",
"group": "develop",
},
"runOptions": { "runOn": "folderOpen" }
},
{
"label": "Run css",
"type": "shell",
"command": "npm run watch:css",
"presentation": {
"reveal": "always",
"panel": "new",
"group": "develop",
},
"runOptions": { "runOn": "folderOpen" }
},
{
"label": "Run tunnel",
"type": "shell",
"command": "npm run tunnel",
"presentation": {
"reveal": "always",
"panel": "new",
"group": "develop",
},
"runOptions": { "runOn": "folderOpen" }
},
{
"label": "Start Dev",
"dependsOn": [
"Run dev",
"Run css",
"Run tunnel"
],
"problemMatcher": []
}
]
}
What i'm looking to do is move Run dev to a launch configuration instead so I have debugging enabled by default, and keep the same terminal experience:
launch.json
{
"configurations": [
{
"name": "Run npm run dev",
"command": "npm run dev",
"request": "launch",
"type": "node-terminal",
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"serverReadyAction": {
"pattern": "started at http://localhost:([0-9]+)",
"uriFormat": "http://localhost:%s",
"action": "openExternally"
}
}
]
}
So the end result will be the same 3 tasks, but "Run dev" triggers the "Run npm run dev" launch configuration in it's terminal window
The problem is there doesn't seem to be a way to do this?

Related

How can I run one task without finishing another in vscode?

i'm developing embedded firmware on zephyr RTOS. Zephyr env. works on WSL2. And "Vscode remote client" works in Win10 "Vscode Server" works on Ununtu 22.04.
I use Jlink debbugger for flashing and debugging firmware. I have to use remote debugging because debugger connected to Win10 via USB. So I run the JLink "JLinkRemoteServerCL.exe" in Win10 before flash or debug.
But every time I plug or unplug the USB into the PC JLink Remote Connection disconnects. This is annoying.
My goal is to automatically run "JLinkRemoteServerCL.exe" when I run the flash task and close it after the flash task is complete.
I wrote two task for it and connect the flash task to start "JLinkRemoteServerCL.exe" task with "dependsOn" property. But Flash task doesn't start because when start to "JLinkRemoteServerCL.exe" task, it waits connection and never finish.
I do not know how to kill the "Start Remote Server" task even if i achieve run the "Flash" task.
How can I flash my MCU's with run only 1 task?
{
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"command": "west",
"type": "shell",
"group": "build",
"options": {
"cwd": "${workspaceFolder}"
},
"args": [
"build",
"--pristine",
"-b",
"${input:board}",
"-d",
"builds/build_${input:board}",
"--",
"-DBOARD_ROOT=../Common",
"-DCONF_FILE=prj.conf",
"-DOVERLAY_CONFIG=configs/prj_${input:board}.conf",
"-DDTC_OVERLAY_FILE=boards/${input:board}.overlay"
],
"problemMatcher": [
"$gcc"
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
}
},
{
"label": "Build MCUBOOT",
"command": "west",
"type": "shell",
"options": {
"cwd": "${workspaceFolder}"
},
"args": [
"build",
"$ZEPHYR_BASE/../bootloader/mcuboot/boot/zephyr",
"--pristine",
"-b",
"${input:board}",
"-d",
"builds/build_${input:board}/bootloader/mcuboot",
"--",
"-DBOARD_ROOT=${cwd}/../Common",
"-DOVERLAY_CONFIG=${cwd}/bootloader_configs/prj_mcuboot_${input:board}.conf",
"-DDTC_OVERLAY_FILE=${cwd}/boards/${input:board}.overlay",
"-DCONFIG_BOOT_SIGNATURE_KEY_FILE=\\\"${cwd}/../Common/keys/${input:key}\\\""
],
"problemMatcher": [
"$gcc"
],
"presentation": {
"reveal": "always"
},
},
{
"label": "Flash",
"command": "west",
"type": "shell",
"group": "build",
"options": {
"cwd": "${workspaceFolder}"
},
"args": [
"flash",
"-d",
"builds/build_${input:board}",
"--hex-file",
"builds/build_${input:board}/zephyr/zephyr.bin",
"-r",
"jlink",
"--tool-opt=ip 192.168.0.21"
],
"dependsOn" : "Start JLink Remote Server"
},
{
"label": "Flash with MCUBOOT",
"command": "west",
"type": "shell",
"group": "build",
"options": {
"cwd": "${workspaceFolder}"
},
"args": [
"flash",
"-d",
"builds/build_${input:board}",
"--hex-file",
"builds/build_${input:board}/merged.hex",
"-r",
"jlink",
"--tool-opt=ip 192.168.0.21"
],
"dependsOn" : "Start JLink Remote Server"
},
{
"label": "Sign",
"command": "west",
"type": "shell",
"options": {
"cwd": "${workspaceFolder}"
},
"args": [
"sign",
"-t",
"imgtool",
"-d",
"builds/build_${input:board}",
"--",
"--version",
"${input:version_num}",
"--key",
"${workspaceFolder}/../Common/keys/${input:key}"
]
},
{
"label": "Build With MCUBOOT",
"command": "python3",
"type": "shell",
"group": "build",
"args": [
"$ZEPHYR_BASE/scripts/build/mergehex.py",
"builds/build_${input:board}/zephyr/zephyr.signed.hex",
"builds/build_${input:board}/bootloader/mcuboot/zephyr/zephyr.hex",
"-o",
"builds/build_${input:board}/merged.hex"
],
"dependsOn":["Get Inputs", "Build", "Build MCUBOOT", "Sign"],
"dependsOrder": "sequence"
},
{
"label": "Start JLink Remote Server",
"type": "shell",
"command": "/mnt/c/Program\\ Files/SEGGER/JLink/JLinkRemoteServerCL.exe",
},
{
"label": "Get Inputs",
"type": "shell",
"command": "echo",
"args": [
"Board:",
"\b${input:board}",
"Key:",
"\b${input:key}",
"Version:",
"\b${input:version_num}"
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": true
}
}
],
"inputs": [
{
"id": "board",
"description": "Zephyr Target Board",
"default": "witag_litum",
"type": "pickString",
"options":[
"witag_litum",
"nucleo_l073rz",
"witag_bdcip",
"witag",
"tsg001",
"ft06dch",
"eagleeyesense",
"lowcost",
"nrf52833dk_nrf52833",
"nrf52840dongle_nrf52840",
]
},
{
"id": "version_num",
"description": "Version Number:",
"default": "100.100.1+0",
"type": "promptString"
},
{
"id": "key",
"description": "MCUBOOT KEY file located in Common/keys",
"default": "witag_litum",
"type": "pickString",
"options":[
"WiTag.pem",
"WiTag_Litum.pem",
"root-rsa-2048.pem"
]
}
]
}

VS Code cannot find similar task, only the last one

The closest to my issue is this SO question, but I think something else is going on here. I have two launch configurations each which call a similar preLaunch task, start-local and start-dev. The body of these two tasks is almost identical, except that VS Code can only find whichever of these is declared last in the task.json file. I tested this by duplicating the first task and simply changing the label, and none but the last task can ever be found. Is this a bug or am I doing something wrong? Pasting my configs for reference:
VS Code Version: 1.72.2
OS Version: MacOS 12.6
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Start Dev",
"type": "chrome",
"request": "launch",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}",
"preLaunchTask": "start-dev",
"postDebugTask": "Terminate All Tasks"
},
{
"name": "Start Local",
"type": "chrome",
"request": "launch",
"url": "http://localhost:3001",
"webRoot": "${workspaceFolder}",
"preLaunchTask": "start-local",
"postDebugTask": "Terminate All Tasks"
},
]
}
task.json
{
"version": "2.0.0",
"tasks": [
{
"label": "start-local",
"type": "npm",
"script": "start",
"isBackground": true,
"problemMatcher": {
"owner": "npm",
"background": {
"activeOnStart": true,
"beginsPattern": ".*",
"endsPattern": "To ignore, add.*eslint-disable-next-line to the line before.*"
},
"pattern": {
"regexp": ""
}
},
"dependsOrder": "sequence",
"dependsOn": [
"setup-local-env"
]
},
{
"label": "setup-local-env",
"command": "echo REACT_APP_STAGE=local > ./.env; echo BROWSER=none >> ./.env",
"type": "shell",
"presentation": {
"echo": false,
"reveal": "never",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false,
"close": true
}
},
{
"label": "start-dev",
"type": "npm",
"script": "start",
"isBackground": true,
"problemMatcher": {
"owner": "npm",
"background": {
"activeOnStart": true,
"beginsPattern": ".*",
"endsPattern": "To ignore, add.*eslint-disable-next-line to the line before.*"
},
"pattern": {
"regexp": ""
}
},
"dependsOrder": "sequence",
"dependsOn": [
"setup-dev-env"
]
},
{
"label": "setup-dev-env",
"command": "echo REACT_APP_STAGE=dev > ./.env; echo BROWSER=none >> ./.env",
"type": "shell",
"presentation": {
"echo": false,
"reveal": "never",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false,
"close": true
}
},
{
"label": "Terminate All Tasks",
"command": "echo ${input:terminate}",
"type": "shell",
"problemMatcher": []
},
],
"inputs": [
{
"id": "terminate",
"type": "command",
"command": "workbench.action.tasks.terminate",
"args": "terminateAll"
}
]
}

How Do I Set Task as preLaunchTask in .code-workspace?

I have a multi-root workspace with launch configurations and tasks. The tasks run fine on their own, but they don't run when added as a preLaunchTask. VS Code throws the error "Could not find the task".
oving them to a tasks.json file is not an option for me.
Here's the relevant information from my .code-worspace file
{
"folders": [
{
"name": "App",
"path": "app"
},
{
"name": "API",
"path": "api"
},
],
"settings": {},
"launch": {
"configurations": [
{
"name": "Launch App",
"type": "chrome",
"request": "launch",
"preLaunchTask": "npm: start",
"cwd": "${workspaceFolder:App}",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder:App}/src",
"sourceMapPathOverrides": {
"webpack:///src/*": "${webRoot}/*"
}
}
]
},
"tasks": {
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "start",
"label": "npm: start",
"detail": "react-scripts start",
"options": {
"cwd": "${workspaceFolder:App}"
},
"problemMatcher": []
}
]
}
}
I don't know why, but this only happens if type = npm.
Change:
"type": "npm",
"script": "start",
to:
"type": "shell",
"command": "npm start",

How run to VSCode tasks in JavaScript Debug Terminal?

I want VSCode to run this task:
{
"label": "Start",
"type": "shell",
"command": "npm run start",
"dependsOn": ["Set Envs"],
"problemMatcher": [],
"presentation": {
"group": "Main",
"reveal": "always",
"close": false
}
In a JavaScript Debug Terminal. Is there a way to do that?

Ignore error from the preivous build task in tasks.json

I configured tasks.json to build and run the application.
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "make"
},
{
"label": "close the file",
"type": "shell",
"command": "somecommand"
},
{
"label": "Run build",
"dependsOn": [
"build",
"close the file"
],
"dependsOrder": "sequence",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
The flow is first make command will execute and after that somecommand will be executed. The problem is some times make command returns exit code other than zero, Because of that somecommand is not executing. Is there any way to ignore the previous build status and execute the somecommand always?
To ignore error code in task of type shell, simply add some nop command at the end using || operator. Valid for both bash and batch.
Like:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "make || echo 0"
},
{
"label": "close the file",
"type": "shell",
"command": "somecommand"
},
{
"label": "Run build",
"dependsOn": [
"build",
"close the file"
],
"dependsOrder": "sequence",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
In my case a simple Power Shell:
-ErrorAction Ignore
Does the job, but it depends on what you're doing.
Here's the reference for the shell.